Advertisement
Guest User

Update Proj

a guest
Oct 7th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 9.09 KB | None | 0 0
  1. /**
  2.  * Created by ksbugbee & aGelinas on 10/5/16.
  3.  */
  4.  
  5. import java.SQL.*;
  6. import java.util.*;
  7.  
  8. public class p3 {
  9.     // REPLACE the "USERID" AND "PASSWORD" WITH your username AND password TO GET this TO WORK.
  10.     // Note: Remember that your Oracle USERID FOR many OF you IS different than your regular login name
  11.     private static final String USERID = "ksbugbee";
  12.     private static final String PASSWORD = "KSBUGBEE";
  13.     static Statement stmt;
  14.  
  15.  
  16.     public void display_menu() {
  17.         System.OUT.println( "Please type your choice and press enter: \n" +
  18.                 "1 – Report Health Provider Information\n" +
  19.                 "2 – Report Health Service Information \n" +
  20.                 "3 – Report Path Information \n" +
  21.                 "4 – Update Health Service Information");
  22.         System.OUT.print ( "Selection: " );
  23.     }
  24.  
  25.     public p3() throws SQLException {
  26.         Scanner scanner = NEW Scanner(System.IN);
  27.         System.OUT.println("-------Oracle JDBC COnnection Testing ---------");
  28.  
  29.         try {
  30.             Class.forName("oracle.jdbc.driver.OracleDriver");
  31.  
  32.         } catch (ClassNotFoundException e){
  33.             System.OUT.println("Where is your Oracle JDBC Driver?");
  34.             e.printStackTrace();
  35.             RETURN;
  36.         }
  37.  
  38.         System.OUT.println("Oracle JDBC Driver Registered!");
  39.         Connection connection = NULL;
  40.  
  41.         try {
  42.             connection = DriverManager.getConnection(
  43.                     "jdbc:oracle:thin:@oracle.wpi.edu:1521:orcl", USERID, PASSWORD);
  44.         } catch (SQLException e) {
  45.             System.OUT.println("Connection Failed! Check output console");
  46.             e.printStackTrace();
  47.             RETURN;
  48.         }
  49.  
  50.         System.OUT.println("Oracle JDBC Driver Connected!");
  51.         Scanner IN = NEW Scanner(System.IN);
  52.         display_menu();
  53.         switch(IN.nextInt()) {
  54.             CASE 1:
  55.                 System.OUT.println("Option 1 picked");
  56.                 System.OUT.println("Enter Provider ID:");
  57.                 INT custID = scanner.nextInt();
  58.                 //creates query based ON the given customer ID
  59.                 String query = "SELECT P.ProviderID, P.FirstName, P.LastName, Prov.Acronym, L.LocationName "
  60.                         + "from Office O, Provider P, ProviderTitle Prov, Location L "
  61.                         + "where O.ProviderID = P.ProviderID "
  62.                         + "and P.ProviderID = Prov.ProviderID "
  63.                         + "and O.LocationID = L.LocationID "
  64.                         + "and P.ProviderID =" + custID ;
  65.                 stmt = connection.createStatement();
  66.                 ResultSet rset = stmt.executeQuery(query);
  67.                 INT provID=0;
  68.                 String fname ="", lname="", title = "", officeLoc = "";
  69.                 //Below processes the results
  70.                 while(rset.NEXT()) {
  71.                     provID = rset.getInt("ProviderID");
  72.                     fname = rset.getString("FirstName");
  73.                     lname = rset.getString("LastName");
  74.                     title = rset.getString("Acronym");
  75.                     officeLoc = rset.getString("LocationName");
  76.                     System.OUT.println("ID:" + provID + "First Name:" + fname + "Last Name:" + lname
  77.                             + "Title:" + title + "Office Location:" + officeLoc);
  78.                 }
  79.                 break;
  80.  
  81.  
  82.             CASE 2:
  83.                 System.OUT.println("Option 2 picked");
  84.                 System.OUT.println("Enter Health Service Name WITHIN SINGLE QUOTES:");
  85.                 String sname = scanner.nextLine();
  86.                 String servicename ="", htype ="", locname="";
  87.                 String floorid;
  88.                 //below creates query
  89.                 String query2 = "select S.ServiceName, S.HealthType, L.LocationName, L.FloorID "
  90.                         + "from Services S, Location L, ResidesIn R "
  91.                         + "where S.ServiceName = R.ServiceName "
  92.                         + "and R.LocationID = L.LocationID "
  93.                         + "and S.ServiceName =" + sname;
  94.                 stmt = connection.createStatement();
  95.                 ResultSet rset2 = stmt.executeQuery(query2);
  96.                 while(rset2.NEXT()) {
  97.                     servicename = rset2.getString("ServiceName");
  98.                     htype = rset2.getString("HealthType");
  99.                     locname = rset2.getString("LocationName");
  100.                     floorid = rset2.getString("FloorID");
  101.                     System.OUT.println("Service Name: " + servicename);
  102.                     System.OUT.println("Health Type: " + htype);
  103.                     System.OUT.println("Location Name: " + locname);
  104.                     System.OUT.println("Floor ID: " + floorid);
  105.                 }
  106.                 break;
  107.  
  108.  
  109.             CASE 3:
  110.                 System.OUT.println("Option 3 picked");
  111.                 String sloc="", eloc="";
  112.                 System.OUT.println("Enter Starting Location:");
  113.                 sloc = scanner.nextLine();
  114.                 System.OUT.println("Enter Ending Location:");
  115.                 eloc = scanner.nextLine();
  116.  
  117.                 String query3 = "SELECT P.PathID " +
  118.                         "from Path P, PathContains PC " +
  119.                         "where P.PathID = PC.PathID " +
  120.                         "and P.PathStart=" + "'" + sloc + "'" +
  121.                         " and P.PathEnd=" + "'" + eloc + "'";
  122.                 String pathID = "";
  123.                 stmt = connection.createStatement();
  124.                 ResultSet rset3 = stmt.executeQuery(query3);
  125.                 while(rset3.NEXT()) {
  126.                     pathID = rset3.getString("PathID");
  127.                 }
  128.                 //checks IF given VALUES are legit
  129.                 String qstart = "SELECT PC.EndPointType "
  130.                         + "from PathContains PC, Path P "
  131.                         + "where P.PathID =" + pathID
  132.                         + " where P.PathID = PC.PathID "
  133.                         + "and P.PathStart = " +sloc
  134.                         + " and P.EndPointType = 'Start'";
  135.                 ResultSet rstart = stmt.executeQuery(qstart);
  136.                 String spoint="";
  137.                 while(rstart.NEXT()) {
  138.                     spoint = rstart.getString("EndPointType");
  139.                 }
  140.                
  141.                 String qend = "SELECT PC.EndPointType "
  142.                         + "from PathContains PC, Path P "
  143.                         + "where P.PathID =" + pathID
  144.                         + " and P.LocationID = PC.LocationID "
  145.                         + "and P.PathEnd = " +eloc
  146.                         + " and P.EndPointType = 'End'";
  147.                 ResultSet rend = stmt.executeQuery(qstart);
  148.                 String epoint="";
  149.                 while(rend.NEXT()) {
  150.                     epoint = rend.getString("EndPointType");
  151.                 }
  152.                
  153.                 //throw error message IF the given starting location IS NOT a starting location
  154.                 // OR IF the ending location IS NOT a valid ending location IN PathContains
  155.                 IF(epoint == "End" && spoint == "Start") {
  156.                     System.OUT.println("Path ID for this given path is: "+ pathID);
  157.                     String query4 = "SELECT L.LocationName, PC.PathOrder, L.FloorID "
  158.                             + "from Location L, PathContains PC, Path P "
  159.                             + "where PC.PathID = P.PathID "
  160.                             + " and PC.PathOrder = " + pathID;
  161.                     String pOrder = "";
  162.                     String locName = "";
  163.                     String fID = "";
  164.                     stmt = connection.createStatement();
  165.                     ResultSet rset4 = stmt.executeQuery(query4);
  166.      
  167.                     while(rset4.NEXT()) {
  168.                         pOrder = rset4.getString("PathOrder");
  169.                         locName = rset4.getString("LocationName");
  170.                         fID = rset4.getString("FloorID");
  171.                         System.OUT.println("     " + pOrder + "     " + locName + "     " + fID + "     ");
  172.      
  173.                     }
  174.                    
  175.                 }
  176.                 ELSE {
  177.                     System.OUT.println("Error: The given locations must be valid starting and ending points");
  178.                 }
  179.                 break;
  180.  
  181.  
  182.             CASE 4:
  183.                 System.OUT.println ( "Option 4 picked" );
  184.                 String servname="", locID="";
  185.                 System.OUT.println("Enter Health Service Name WITHIN SINGLE QUOTES:");
  186.                 servname = scanner.nextLine();
  187.                 System.OUT.println("Enter the new Location ID WITHIN SINGLE QUOTES:");
  188.                 locID = scanner.nextLine();
  189.                 query = "update ResidesIn "
  190.                         + "set LocationID =" +locID
  191.                         + " where ServiceName=" + servname;
  192.                 stmt = connection.createStatement();
  193.                 stmt.executeQuery(query);
  194.                 break;
  195.             DEFAULT:
  196.                 System.err.println ( "Unrecognized option" );
  197.                 break;
  198.         }
  199.     }
  200.  
  201.     public static void main(String[] args) throws SQLException {
  202.         NEW p3();
  203.  
  204.  
  205.     }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement