Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package Control;
  2.  
  3.  
  4. import Model.Customer;
  5. import java.sql.*;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. /*
  10.  * To change this license header, choose License Headers in Project Properties.
  11.  * To change this template file, choose Tools | Templates
  12.  * and open the template in the editor.
  13.  */
  14.  
  15. /**
  16.  *
  17.  * @author Leconize
  18.  */
  19. public class CustomerUtil {
  20.    
  21.     Connection conn;
  22.     Statement state;
  23.        
  24.    
  25.      public void init() {
  26.         try {
  27.             Class.forName("com.mysql.jdbc.Driver");
  28.             String url = "jdbc:mysql://database.it.kmitl.ac.th:3306/it_33";
  29.             String user = "it_33";
  30.             String pwd = "uZf2Vnw3";
  31.             conn = DriverManager.getConnection(url, user, pwd);
  32.             state = conn.createStatement();
  33.         } catch (ClassNotFoundException ex) {
  34.             Logger.getLogger(CustomerUtil.class.getName()).log(Level.SEVERE, null, ex);
  35.         } catch (SQLException ex) {
  36.             Logger.getLogger(CustomerUtil.class.getName()).log(Level.SEVERE, null, ex);
  37.         }
  38.          
  39.     }
  40.      
  41.     public int addCustomer(Customer customer){
  42.         String fname = customer.getFname();
  43.         String lname = customer.getLname();
  44.         String company = customer.getCompany();
  45.         String mobile = customer.getMobile();
  46.         String address = customer.getAddress();
  47.         String email = customer.getEmail();
  48.        
  49.         String sqlCmd = "insert into Customers values('"+ fname + "','" + lname
  50.                 + "' , '" + company + "' , '" + mobile  + "' , '" + email + "' , '" + address + "')";
  51.         System.out.println(sqlCmd);
  52.         int numberOfRowEffected = 0;
  53.         try {
  54.             numberOfRowEffected = state.executeUpdate(sqlCmd);
  55.         } catch (SQLException ex) {
  56.             Logger.getLogger(CustomerUtil.class.getName()).log(Level.SEVERE, null, ex);
  57.         }
  58.         return numberOfRowEffected;
  59.     }
  60.    
  61.     public ResultSet loadData(){
  62.         String sqlCmd = "select * from Customers;";
  63.         ResultSet rs = null;
  64.         try {
  65.             rs = state.executeQuery(sqlCmd);
  66.         } catch (SQLException ex) {
  67.             Logger.getLogger(CustomerUtil.class.getName()).log(Level.SEVERE, null, ex);
  68.         }
  69.         return rs;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement