Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class Hello{
  5.     Connection connection;
  6.  
  7.     private void displaySQLErrors(SQLException e){
  8.         System.out.println("SQLException: " + e.getMessage());
  9.         System.out.println("SQLState: " + e.getSQLState());
  10.         System.out.println("VendorError: " + e.getErrorCode());
  11.     }
  12.  
  13.     public Hello(){
  14.         try{
  15.             Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  16.         } catch (ClassNotFoundException e){
  17.             System.err.println("Unable to find library");
  18.             System.exit(1);
  19.         } catch (InstantiationException e){
  20.             System.err.println("Unable to instantiate object");
  21.             System.exit(1);
  22.         } catch(IllegalAccessException e){
  23.             System.err.println("Illegal access");
  24.             System.exit(1);
  25.         }
  26.     }
  27.  
  28.     public void connectToDB(){
  29.         try{
  30.             connection = DriverManager.getConnection("jdbc:mysql://localhost/javaday2?user=root&password=");
  31.         } catch(SQLException e){
  32.             displaySQLErrors(e);
  33.         }
  34.     }
  35.  
  36.     public void executeSQL(){
  37.         try{
  38.             Statement statement = connection.createStatement();
  39.  
  40.             ResultSet rs = statement.executeQuery("SELECT * FROM individual");
  41.  
  42.             while(rs.next()){
  43.                 System.out.println("Birtday="+rs.getString(1)+"| First Name=" +rs.getString(2));
  44.             }
  45.  
  46.             rs.close();
  47.             statement.close();
  48.             connection.close();
  49.         } catch(SQLException e){
  50.             displaySQLErrors(e);
  51.         }
  52.     }
  53.  
  54.     public void insertQuerryExercise(){
  55.         Scanner scanner = new Scanner(System.in);
  56.  
  57.         System.out.println("Input data baru");
  58.         System.out.println("Firts name?");
  59.         String fname = scanner.nextLine();
  60.  
  61.         System.out.println("Last name?");
  62.         String lname = scanner.nextLine();
  63.  
  64.         System.out.println("Birthday?");
  65.         String birthday = scanner.nextLine();
  66.  
  67.         System.out.println("Address?");
  68.         String address = scanner.nextLine();
  69.  
  70.         System.out.println("City?");
  71.         String city = scanner.nextLine();
  72.  
  73.         System.out.println("Customer Type I or B?");
  74.         String custtype = scanner.nextLine();
  75.  
  76.         System.out.println("FED ID?");
  77.         String fedid = scanner.nextLine();
  78.  
  79.         System.out.println("Postal Code?");
  80.         String postal_code = scanner.nextLine();
  81.  
  82.         System.out.println("State?");
  83.         String state = scanner.nextLine();
  84.  
  85.         try{
  86.             Statement statement = connection.createStatement();
  87.             String myQuerrycust = "INSERT INTO customer (CUST_ID, ADDRESS, CITY, CUST_TYPE_CD, FED_ID, POSTAL_CODE, STATE ) VALUE ('','"+address+"','"+city+"','"+custtype+"','"+fedid+"','"+postal_code+"','"+state+"')";
  88.             statement.executeUpdate(myQuerrycust);
  89.  
  90.             //rs.close();
  91.             statement.close();
  92.  
  93.  
  94.  
  95.             //Statement statement = connection.createStatement();
  96.             String myQuerryind = "INSERT INTO individual (BIRTH_DATE, FIRST_NAME, LAST_NAME, CUST_ID ) VALUE ('"+birthday+"','"+fname+"','"+lname+"','')";
  97.             statement.executeUpdate(myQuerryind);
  98.  
  99.             //rs.close();
  100.             statement.close();
  101.             connection.close();
  102.  
  103.         }catch(SQLException e){
  104.         displaySQLErrors(e);
  105.  
  106.         }
  107.     }
  108.  
  109.  
  110.     public static void main(String[] args){
  111.         Hello hello = new Hello();
  112.  
  113.         hello.connectToDB();
  114.         hello.insertQuerryExercise();
  115.         hello.executeSQL();
  116.    
  117.        
  118.  
  119.         // System.out.print(" " + fname + "");
  120.         // System.out.println(" " + lname + "");
  121.         // System.out.println(" " + birthday + "");
  122.  
  123.  
  124.  
  125.        
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement