Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- public class Hello{
- Connection connection;
- private void displaySQLErrors(SQLException e){
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- System.out.println("VendorError: " + e.getErrorCode());
- }
- public Hello(){
- try{
- Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
- } catch (ClassNotFoundException e){
- System.err.println("Unable to find library");
- System.exit(1);
- } catch (InstantiationException e){
- System.err.println("Unable to instantiate object");
- System.exit(1);
- } catch(IllegalAccessException e){
- System.err.println("Illegal access");
- System.exit(1);
- }
- }
- public void connectToDB(){
- try{
- connection = DriverManager.getConnection("jdbc:mysql://localhost/javaday2?user=root&password=");
- } catch(SQLException e){
- displaySQLErrors(e);
- }
- }
- public void executeSQL(){
- try{
- Statement statement = connection.createStatement();
- ResultSet rs = statement.executeQuery("SELECT * FROM individual");
- while(rs.next()){
- System.out.println("Birtday="+rs.getString(1)+"| First Name=" +rs.getString(2));
- }
- rs.close();
- statement.close();
- connection.close();
- } catch(SQLException e){
- displaySQLErrors(e);
- }
- }
- public void insertQuerryExercise(){
- Scanner scanner = new Scanner(System.in);
- System.out.println("Input data baru");
- System.out.println("Firts name?");
- String fname = scanner.nextLine();
- System.out.println("Last name?");
- String lname = scanner.nextLine();
- System.out.println("Birthday?");
- String birthday = scanner.nextLine();
- System.out.println("Address?");
- String address = scanner.nextLine();
- System.out.println("City?");
- String city = scanner.nextLine();
- System.out.println("Customer Type I or B?");
- String custtype = scanner.nextLine();
- System.out.println("FED ID?");
- String fedid = scanner.nextLine();
- System.out.println("Postal Code?");
- String postal_code = scanner.nextLine();
- System.out.println("State?");
- String state = scanner.nextLine();
- try{
- Statement statement = connection.createStatement();
- 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+"')";
- statement.executeUpdate(myQuerrycust);
- //rs.close();
- statement.close();
- //Statement statement = connection.createStatement();
- String myQuerryind = "INSERT INTO individual (BIRTH_DATE, FIRST_NAME, LAST_NAME, CUST_ID ) VALUE ('"+birthday+"','"+fname+"','"+lname+"','')";
- statement.executeUpdate(myQuerryind);
- //rs.close();
- statement.close();
- connection.close();
- }catch(SQLException e){
- displaySQLErrors(e);
- }
- }
- public static void main(String[] args){
- Hello hello = new Hello();
- hello.connectToDB();
- hello.insertQuerryExercise();
- hello.executeSQL();
- // System.out.print(" " + fname + "");
- // System.out.println(" " + lname + "");
- // System.out.println(" " + birthday + "");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement