SHARE
TWEET

Untitled

a guest Jan 26th, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.codezen.mvn_web;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8.  
  9. public class UserDAO {
  10.  
  11.     private Connection conn;
  12.     private PreparedStatement pstmt;
  13.     private ResultSet rs;
  14.  
  15.     public UserDAO() {
  16.         try {
  17.  
  18.             Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  19.             conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb?characterEncoding=utf8&serverTimezone=UTC", "webuser", "0000");
  20.  
  21.         }catch(Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.  
  26.     public int login(String userID, String userPassword) {
  27.  
  28.         String SQL = "SELECT userPassword FROM USER WHERE userID =  ?";
  29.  
  30.         try {
  31.             pstmt = conn.prepareStatement(SQL);
  32.             pstmt.setString(1, userID);
  33.             rs = pstmt.executeQuery();
  34.             if(rs.next()){
  35.                 if(rs.getString(1).equals(userPassword))
  36.                     return 1; // 로그인 성공
  37.                 else
  38.                     return 0; // 비밀번호 불일치
  39.             }
  40.             return -1; // 아이디가 없음
  41.         }catch(Exception e) {
  42.             e.printStackTrace();
  43.         }
  44.         return -2; // 데이터베이스 오류
  45.     }
  46.  
  47.     public int join(User user) {
  48.        
  49.          String SQL = "INSERT INTO USER VALUES (?,?,?,?,?)";
  50.  
  51.          try {
  52.  
  53.              pstmt = conn.prepareStatement(SQL);
  54.  
  55.               pstmt.setString(1,  user.getUserID());
  56.               pstmt.setString(2,  user.getUserPassword());
  57.               pstmt.setString(3,  user.getUserName());
  58.               pstmt.setString(4,  user.getUserGender());
  59.               pstmt.setString(5,  user.getUserEmail());
  60.  
  61.               return pstmt.executeUpdate();
  62.  
  63.          } catch(Exception e) {
  64.              e.printStackTrace();
  65.          }
  66.          return -1; // 데이터 베이스 오류
  67.     }
  68.  
  69. }
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