daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Aug 13th, 2016 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package CafeDatabase;
  2.  
  3. import java.util.Scanner;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11.  
  12. public class CafeDb {
  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 CafeDb(){
  26.         try{
  27.             Class.forName(DRIVER1);
  28.             con = DriverManager.getConnection(DBURL, USERID, PW);
  29.             System.out.println("DB 성공적 연결");
  30.        
  31.             /*
  32.             stmt = mydb.createStatement();
  33.             String query = "SELECT * FROM cafeInfo where name like '%" + key + "%'";
  34.             rs = stmt.executeQuery(query);
  35.             */
  36.         } catch(Exception e){
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.    
  41.     public void dbClose(){
  42.         try{
  43.             if(rs != null)
  44.                 rs.close();
  45.             if(st != null)
  46.                 st.close();
  47.         } catch(SQLException e){
  48.             e.printStackTrace();
  49.         }
  50.     }
  51.    
  52.     public int cafeInfoInsert(CafeStorage cs){
  53.         int result = 0;
  54.        
  55.         try{
  56.             String query = "INSERT INTO cafeInfo values(?, ?, ?, ?, ?, ?, ?)";
  57.            
  58.             ps = con.prepareStatement(query);
  59.             ps.setString(1, cs.getId());
  60.             ps.setString(2, cs.getName());
  61.             ps.setString(3, cs.getPw());
  62.             ps.setString(4, cs.getTell());
  63.             ps.setString(5, cs.getIntro());
  64.             ps.setString(6, cs.getAddress());
  65.             ps.setInt(7, cs.getTableNum());
  66.            
  67.             result = ps.executeUpdate();
  68.         } catch(Exception e){
  69.             System.out.println(e + "=> cafeInfo insert fail");
  70.         }finally{
  71.             dbClose();
  72.         }
  73.        
  74.         return result;
  75.     }
  76.    
  77.     public ArrayList<CafeStorage> getCafeInfo(String field, String key){
  78.        
  79.         ArrayList<CafeStorage> list = new ArrayList<>();
  80.        
  81.         try{       
  82.             String query = "SELECT * FROM cafeInfo where " + field + " like '%" + key + "%'";
  83.             st = con.createStatement();
  84.             rs = st.executeQuery(query);
  85.            
  86.             while(rs.next()){
  87.                 CafeStorage c = new CafeStorage();
  88.                
  89.                 c.setId(rs.getString(1));
  90.                 c.setName(rs.getString(2));
  91.                 c.setPw(rs.getString(3));
  92.                 c.setTell(rs.getString(4));
  93.                 c.setIntro(rs.getString(5));
  94.                 c.setAddress(rs.getString(6));
  95.                 c.setTableNum(rs.getInt(7));
  96.                
  97.                 list.add(c);
  98.             }
  99.         } catch(Exception e){
  100.             System.out.println(e + "=> get cafeInfo list fail");
  101.         } finally{
  102.             dbClose();
  103.         }
  104.        
  105.         return list;
  106.     }
  107. }
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