Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package testtt;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10.  
  11.  
  12. public class CustomerHandler {
  13.    
  14.        // JDBC driver name and database URL
  15.        static final String JDBC_DRIVER = "org.h2.Driver";  
  16.        static final String DB_URL = "jdbc:h2:~/test";  
  17.        
  18.        //  Database credentials
  19.        static final String USER = "admin";
  20.        static final String PASS = "admin";
  21.        
  22.           static Connection conn = null;
  23.           static Statement stmt = null;
  24.        
  25.        public static void connect() {
  26.            
  27.              try {
  28.                 Class.forName(JDBC_DRIVER);
  29.                 System.out.println("Connecting to database...");
  30.                 conn = DriverManager.getConnection(DB_URL,USER,PASS);
  31.                
  32.             } catch (ClassNotFoundException |SQLException e) {
  33.                 // TODO Auto-generated catch block
  34.                 e.printStackTrace();
  35.             }
  36.                  
  37.        
  38.        }
  39.        
  40.        public static void close() {
  41.            
  42.             try {
  43.                //stmt.close();
  44.                conn.close();
  45.                System.out.println("Connection closed...");
  46.             } catch (SQLException e) {
  47.                 // TODO Auto-generated catch block
  48.                 e.printStackTrace();
  49.             }
  50.        }
  51.        
  52.        public static void addCustomer(String id, String name, String surname, String birthdate,String adress) {
  53.            
  54.              try {
  55.  
  56.                  PreparedStatement insertPreparedStatement = null;
  57.                  String InsertQuery = "INSERT INTO CUSTOMERS" + "(id, name, surname, birthdate, address,list) values" + "(?,?,?,?,?,?)";
  58.                  
  59.                 ArrayList list1 = new ArrayList<Account>();
  60.                
  61.                 Account acc = new Account("44","dspname");
  62.                 list1.add(acc);
  63.                  
  64.                  insertPreparedStatement = conn.prepareStatement(InsertQuery);
  65.                  insertPreparedStatement.setString(1, id);
  66.                  insertPreparedStatement.setString(2, name);
  67.                  insertPreparedStatement.setString(3, surname);
  68.                  insertPreparedStatement.setString(4, birthdate);
  69.                  insertPreparedStatement.setString(5, adress);
  70.                  insertPreparedStatement.setObject(6, list1);
  71.                  insertPreparedStatement.executeUpdate();
  72.                  insertPreparedStatement.close();
  73.                  
  74.                  
  75.             } catch (SQLException e) {
  76.                 // TODO Auto-generated catch block
  77.                 e.printStackTrace();
  78.             }  
  79.              
  80.            
  81.        }
  82.    
  83.        public static void getCustomerById(int input_id) {
  84.            
  85.            try {
  86.                
  87.              stmt = conn.createStatement();
  88.              String sql = "SELECT * FROM CUSTOMERS WHERE ID = " + input_id;
  89.              ResultSet rs = stmt.executeQuery(sql);
  90.              
  91.              while(rs.next()) {
  92.                 // Retrieve by column name
  93.                 String id  = rs.getString("id");
  94.                 String name = rs.getString("name");
  95.                 String surname = rs.getString("surname");
  96.                 String birthdate = rs.getString("birthdate");
  97.                 String adress = rs.getString("address");
  98.                 Object[] colArray=  (Object[]) rs.getArray("list").getArray();
  99.                
  100.                 // Display values
  101.                 System.out.print("ID: " + id);
  102.                 System.out.print(", Name: " + name);
  103.                 System.out.print(", Surname: " + surname);
  104.                 System.out.print(", Date: " + birthdate);
  105.                 System.out.print(", Adress: "+ adress);
  106.                // System.out.println("Accoutn detail: " + colArray.getClass().);
  107.              }
  108.              
  109.                 rs.close();
  110.             } catch (SQLException e) {
  111.                 // TODO Auto-generated catch block
  112.                 e.printStackTrace();
  113.             }
  114.            
  115.        }
  116.        
  117.        public static void main(String [] args) {
  118.        
  119.         connect();
  120.        
  121.         getCustomerById(3);
  122.        
  123.         close();
  124.     }
  125.    
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement