Advertisement
hyunjeong

Untitled

Aug 29th, 2019
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package user;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7.  
  8. public class UserDAO {
  9.     private Connection con;
  10.     private PreparedStatement pstmt;
  11.     private ResultSet rs;
  12.    
  13.     public UserDAO (){
  14.         try {
  15.             String dburl="jdbc:mysql://localhost:3306/shift";
  16.             String dbId ="shift";
  17.             String dbPassword = "shift";
  18.             Class.forName("com.mysql.jdbc.Driver");
  19.             con = DriverManager.getConnection(dburl, dbId, dbPassword);
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.         }
  23.     }
  24.    
  25.     public int login (String userId, String userPassword) {
  26.         String SQL = "SELECT userPassword FROM USER WHERE userId=? " ;
  27.         try {
  28.             pstmt = con.prepareStatement(SQL);
  29.             pstmt.setString(1, userId);
  30.             rs = pstmt.executeQuery();
  31.             if (rs.next() ) {
  32.                 if(rs.getString(1).equals(userPassword))
  33.                     return 1;
  34.                 else return 0;
  35.                    
  36.                
  37.             }
  38.             return -1;
  39.         } catch (Exception e) {
  40.             e.printStackTrace();
  41.         }
  42.         return  -2;
  43.     }
  44.    
  45.     public int join(User user){
  46.         String SQL = "INSERT INTO USER VALUES (?, ?, ?, ?, ?)";
  47.         try {
  48.             pstmt = con.prepareStatement(SQL);
  49.             pstmt.setString(1, user.getUserId());
  50.             pstmt.setString(2, user.getUserPassword());
  51.             pstmt.setString(3, user.getUserName());
  52.             pstmt.setString(4, user.getUserGender());
  53.             pstmt.setString(5, user.getUserEmail());
  54.             return pstmt.executeUpdate();
  55.         } catch (Exception e) {
  56.             e.printStackTrace();
  57.         }
  58.         return -1;
  59.     }
  60.    
  61.    
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement