SHARE
TWEET

Untitled

a guest Aug 13th, 2016 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package CafeDatabase;
  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. import java.util.Scanner;
  11.  
  12. public class CafeTableDb {
  13.  
  14.     Scanner scan = new Scanner(System.in);
  15.  
  16.     String DRIVER1 = "org.mariadb.jdbc.Driver";
  17.     String DBURL = "jdbc:mysql://localhost:3306/cafe";
  18.     String USERID = "root";
  19.     String PW = "";
  20.     Statement st = null;
  21.     ResultSet rs = null;
  22.     Connection con = null;
  23.     PreparedStatement ps = null;
  24.    
  25.     public CafeTableDb(){
  26.         try{
  27.             Class.forName(DRIVER1);
  28.             con = DriverManager.getConnection(DBURL, USERID, PW);
  29.             System.out.println("DB 성공적 연결");
  30.        
  31.         } catch(Exception e){
  32.             e.printStackTrace();
  33.         }
  34.     }
  35.    
  36.     public void dbClose(){
  37.         try{
  38.             if(rs != null)
  39.                 rs.close();
  40.             if(st != null)
  41.                 st.close();
  42.         } catch(SQLException e){
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.    
  47.     public int cafeTableInfoInsert(CafeTableStorage cts, int tableNum){
  48.         int result = 0;
  49.        
  50.         try{
  51.             for(int i = 0; i < tableNum; i++){
  52.                 String query = "INSERT INTO cafeTable values(?, ?)";
  53.                
  54.                 ps = con.prepareStatement(query);
  55.                 ps.setString(1, cts.getId());
  56.                 ps.setInt(3, 0);
  57.                
  58.                 result = ps.executeUpdate();
  59.             }
  60.            
  61.            
  62.         } catch(Exception e){
  63.             System.out.println(e + "=> cafeInfo insert fail");
  64.         }finally{
  65.             dbClose();
  66.         }
  67.        
  68.         return result;
  69.     }
  70.    
  71.     public int seqInitalize (){
  72.         String query = "alter table cafeTable auto_increment=1";
  73.         int result = 0;
  74.        
  75.         try {
  76.             ps = con.prepareStatement(query);
  77.             result = ps.executeUpdate();
  78.         } catch (SQLException e) {
  79.             System.out.println(e + "=> sequence initialize fail");
  80.         }
  81.        
  82.         return result;
  83.     }
  84.    
  85. public ArrayList<CafeTableStorage> getCafeInfo(String key){
  86.        
  87.         ArrayList<CafeTableStorage> list = new ArrayList<>();
  88.        
  89.         try{       
  90.             String query = "SELECT * FROM cafeTable where ID = " + key;
  91.             st = con.createStatement();
  92.             rs = st.executeQuery(query);
  93.            
  94.             while(rs.next()){
  95.                 CafeTableStorage c = new CafeTableStorage();
  96.                
  97.                 c.setId(rs.getString(1));
  98.                 c.setTableSitu(rs.getInt(3));
  99.                
  100.                 list.add(c);
  101.             }
  102.         } catch(Exception e){
  103.             System.out.println(e + "=> get cafeTableInfo list fail");
  104.         } finally{
  105.             dbClose();
  106.         }
  107.        
  108.         return list;
  109.     }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top