Guest User

Connect

a guest
Jun 8th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9.  
  10. public class Connect {
  11.     Connection con;
  12.     Statement st;
  13.     ResultSet rs;
  14.     ResultSetMetaData rsm;
  15.     PreparedStatement pstat;
  16.    
  17.     public Connect() {
  18.         // TODO Auto-generated constructor stub
  19.         try {
  20.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  21.             con = DriverManager
  22.                     .getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}; DBQ=Bluejack_Boutique.mdb");
  23.             st = con.createStatement();
  24.             System.out.println("Connection Successful");
  25.         } catch (Exception e) {
  26.             // TODO Auto-generated catch block
  27.             e.printStackTrace();
  28.             System.out.println("Error Connection");
  29.         }
  30.     }
  31.  
  32.     public ResultSet executeQuery(String query) {
  33.         try {
  34.             rs = st.executeQuery(query);
  35.             rsm = rs.getMetaData();
  36.         } catch (SQLException e) {
  37.             // TODO Auto-generated catch block
  38.             e.printStackTrace();
  39.             System.out.println("Error Connection Result Set");
  40.         }
  41.         return rs;
  42.     }
  43.    
  44.     public void executeUpdateProduct(String ProductName, Integer ProductPrice, Integer Stock, String ProductID){
  45.         try{
  46.             pstat = con.prepareStatement("UPDATE MsProduct SET ProductName = ? , ProductPrice = ? , Stock = ?, password = ? WHERE ProductID = ");
  47.             pstat.setString(1, ProductName);
  48.             pstat.setInt(2, ProductPrice);
  49.             pstat.setInt(3, Stock);
  50.             pstat.setString(4, ProductID);
  51.             pstat.execute();
  52.         } catch(Exception e){
  53.             // TODO : handle exception
  54.         }
  55.     }
  56.    
  57.     public void executeDeleteStaff(String id){
  58.         try {
  59.             pstat = con.prepareStatement("DELETE FROM Staff WHERE id = ?");
  60.             pstat.setString(1, id);
  61.             pstat.execute();
  62.         } catch (Exception e) {
  63.             // TODO: handle exception
  64.         }
  65.     }
  66.    
  67.     public void executeInsertUser(String UserID, String Username, String UserPass, String UserFullName, String UserEmail, String UserPhone, String UserAddress, String UserGender, String Role){
  68.         try {
  69.             pstat = con.prepareStatement("INSERT INTO MsUser(UserID, Username, UserPass, UserFullName, UserEmail, UserPhone, UserAddress, UserGender, Role) VALUES (?,?,?,?,?,?,?,?,?)");
  70.             pstat.setString(1, UserID);
  71.             pstat.setString(2, Username);
  72.             pstat.setString(3, UserPass);
  73.             pstat.setString(4, UserFullName);
  74.             pstat.setString(5, UserEmail);
  75.             pstat.setString(6, UserPhone);
  76.             pstat.setString(7, UserAddress);
  77.             pstat.setString(8, UserGender);
  78.             pstat.setString(9, Role);
  79.             pstat.execute();
  80.         } catch (Exception e) {
  81.             System.out.println("Data Tidak Masuk");
  82.         }
  83.     }
  84.    
  85.     public Boolean executeLogin(String username, String userpass){
  86.         try{
  87.             pstat = con.prepareStatement("SELECT * FROM MsUser WHERE username = ? and userpass = ?");
  88.             pstat.setString(1, username);
  89.             pstat.setString(2, userpass);
  90.             rs=pstat.executeQuery();
  91.             if(rs.next()) return true;
  92.             else return false;
  93.         } catch (Exception e){
  94.            
  95.         }
  96.             return false;
  97.     }
  98.    
  99.     public void executeBuy(String ProductName, Integer ProductPrice, Integer stock){
  100.         try {
  101.             pstat = con.prepareStatement("INSERT INTO MsProduct(ProductName, ProductPrice, Stock) VALUES (?,?,?)");
  102.             pstat.setString(1, ProductName);
  103.             pstat.setInt(2, ProductPrice);
  104.             pstat.setInt(3, stock);
  105.             pstat.execute();
  106.         } catch (Exception e) {
  107.             System.out.println("Data Tidak Masuk");
  108.         }
  109.     }
  110.    
  111.     public void executeInsertProduct(String ProductName, String ProductPrice, int Stock){
  112.         try {
  113.             pstat = con.prepareStatement("INSERT INTO MsProduct(ProductName, ProductPrice, Stock) VALUES (?,?,?)");
  114.             pstat.setString(1, ProductName);
  115.             pstat.setString(2, ProductPrice);
  116.             pstat.setInt(3, Stock);
  117.             pstat.execute();
  118.         } catch (Exception e) {
  119.             System.out.println("Data Tidak Masuk");
  120.         }
  121.     }
  122.  
  123.    
  124. }
Add Comment
Please, Sign In to add comment