Advertisement
Guest User

View

a guest
Jan 3rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class viewcustomer{
  4. Connection connection;
  5.  
  6.     private void displaySQLErrors(SQLException e){
  7.         System.out.println("SQLException: " + e.getMessage());
  8.         System.out.println("SQLState: " + e.getSQLState());
  9.         System.out.println("VendorError: " + e.getErrorCode());
  10.     }
  11.  
  12.     public void connectToDB(){
  13.         try{
  14.             connection = DriverManager.getConnection("jdbc:mysql://localhost/javaday2?user=root&password=");
  15.         } catch(SQLException e){
  16.             displaySQLErrors(e);
  17.         }
  18.     }
  19.  
  20. public void showAllCustomer(){
  21.         try{
  22.             Statement statement = connection.createStatement();
  23.  
  24.             ResultSet rs = statement.executeQuery("SELECT individual.FIRST_NAME, customer.ADDRESS, customer.CITY from customer INNER JOIN individual on customer.CUST_ID=individual.CUST_ID");
  25.  
  26.             System.out.println("   Customer View:  ");
  27.             System.out.println("===================");
  28.             while(rs.next()){
  29.                 System.out.println("{First name="+rs.getString(1)+", Address="+rs.getString(2)+", City="+rs.getString(3)+"}");
  30.             }
  31.  
  32.             rs.close();
  33.             statement.close();
  34.             // connection.close();
  35.  
  36.         } catch(SQLException e){
  37.             displaySQLErrors(e);
  38.         }
  39.     }
  40.     public static void main(String[] args){
  41.         viewcustomer viewcustomer = new viewcustomer();
  42.         viewcustomer.connectToDB();
  43.         viewcustomer.showAllCustomer();
  44.  
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement