Advertisement
Guest User

most updated

a guest
Oct 7th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 9.40 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.                         + " and P.PathID = PC.PathID "
  133.                         + "and P.PathStart = " + "'" +sloc + "'"
  134.                         + " and PC.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.PathID = PC.PathID "
  145.                         + "and P.PathEnd = " + "'" + eloc +"'"
  146.                         + " and PC.EndPointType = 'End'";
  147.                 ResultSet rend = stmt.executeQuery(qend);
  148.                 String epoint="";
  149.                 while(rend.next()) {
  150.                     epoint = rend.getString("EndPointType");
  151.                 }
  152.                 //for testing
  153.                 System.out.println("Should be start:" + spoint);
  154.                 System.out.println("Should be end:" + epoint);
  155.                
  156.                 //throw error message if the given starting location is not a starting location
  157.                 // or if the ending location is not a valid ending location in PathContains
  158.                 if(spoint.equals("Start") && epoint.equals("End")) {
  159.                     System.out.println("Path ID for this given path is: "+ pathID);
  160.                     String query4 = "SELECT L.LocationName, PC.PathOrder, L.FloorID "
  161.                             + "from Location L, PathContains PC, Path P "
  162.                             + "where PC.PathID = P.PathID "
  163.                             + "and P.PathStart = L.LocationName "
  164.                             + " and PC.PathOrder = " + pathID
  165.                             + " and PC.PathID = " + pathID;
  166.                     String pOrder = "";
  167.                     String locName = "";
  168.                     String fID = "";
  169.                     stmt = connection.createStatement();
  170.                     ResultSet rset4 = stmt.executeQuery(query4);
  171.      
  172.                     while(rset4.next()) {
  173.                         pOrder = rset4.getString("PathOrder");
  174.                         locName = rset4.getString("LocationName");
  175.                         fID = rset4.getString("FloorID");
  176.                         System.out.println("     " + pOrder + "     " + locName + "     " + fID + "     ");
  177.      
  178.                     }
  179.                    
  180.                 }
  181.                 else {
  182.                     System.out.println("Error: The given locations must be valid starting and ending points");
  183.                 }
  184.                 break;
  185.  
  186.  
  187.             case 4:
  188.                 System.out.println ( "Option 4 picked" );
  189.                 String servname="", locID="";
  190.                 System.out.println("Enter Health Service Name WITHIN SINGLE QUOTES:");
  191.                 servname = scanner.nextLine();
  192.                 System.out.println("Enter the new Location ID WITHIN SINGLE QUOTES:");
  193.                 locID = scanner.nextLine();
  194.                 query = "update ResidesIn "
  195.                         + "set LocationID =" +locID
  196.                         + " where ServiceName=" + servname;
  197.                 stmt = connection.createStatement();
  198.                 stmt.executeQuery(query);
  199.                 break;
  200.             default:
  201.                 System.err.println ( "Unrecognized option" );
  202.                 break;
  203.         }
  204.     }
  205.  
  206.     public static void main(String[] args) throws SQLException {
  207.         new p3();
  208.  
  209.  
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement