Advertisement
AnggaPrahmana

menu 3

Jan 3rd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. public void searchCustomer(){
  2.        
  3.         Scanner scan = new Scanner(System.in);
  4.  
  5.         String type="";
  6.         String name="";
  7.  
  8.         System.out.println("Search Customer");
  9.         System.out.print("[B]usiness or [I]ndividual ");
  10.         type = scan.nextLine();
  11.  
  12.         if(type.equals("B") || type.equals("b")){
  13.             System.out.print("Customer Name : ");
  14.             name = scan.nextLine();
  15.            
  16.             try{
  17.                 Statement statement = DriverManager.getConnection("jdbc:mysql://localhost/javaday2?user=root&password=").createStatement();
  18.                 String q =
  19.                     "SELECT business.NAME, customer.ADDRESS, customer.CITY from customer INNER JOIN business ON customer.CUST_ID=business.CUST_ID  WHERE NAME='"+name+"'";
  20.                 ResultSet rs = statement.executeQuery(q);
  21.  
  22.                 while(rs.next()){
  23.                     String row="";
  24.                     row = rs.getString(1) +" \t| "+ rs.getString(2) + "\t\t| " + rs.getString(3);
  25.  
  26.                     System.out.println(row);
  27.                 }
  28.  
  29.                 rs.close();
  30.                 statement.close();
  31.             }catch(SQLException e){
  32.                 System.out.println("SQLException: " + e.getMessage());
  33.                 System.out.println("SQLState: " + e.getSQLState());
  34.                 System.out.println("VendorError: " + e.getErrorCode());
  35.             }
  36.         }
  37.         else if(type.equals("I")||type.equals("i")){
  38.             System.out.print("Individual Name : ");
  39.             name = scan.nextLine();
  40.            
  41.             try{
  42.                 Statement statement = DriverManager.getConnection("jdbc:mysql://localhost/javaday2?user=root&password=").createStatement();
  43.                 String q =
  44.                     "SELECT individual.FIRST_NAME, customer.ADDRESS, customer.CITY from customer INNER JOIN individual ON customer.CUST_ID=individual.CUST_ID WHERE FIRST_NAME='"+name+"'";
  45.                 ResultSet rs = statement.executeQuery(q);
  46.  
  47.                 while(rs.next()){
  48.                     String row="";
  49.                     row = rs.getString(1) +" \t| "+ rs.getString(2) + "\t\t| " + rs.getString(3);
  50.  
  51.                     System.out.println(row);
  52.                 }
  53.  
  54.                 rs.close();
  55.                 statement.close();
  56.             }catch(SQLException e){
  57.                 System.out.println("SQLException: " + e.getMessage());
  58.                 System.out.println("SQLState: " + e.getSQLState());
  59.                 System.out.println("VendorError: " + e.getErrorCode());
  60.             }
  61.         }
  62.         else{
  63.             System.out.println("WRONG INPUT !");
  64.         }
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement