λ°μ΄ν° λͺ¨λΈλ§
μ€κ³κ° λ λ°μ΄ν°λ² μ΄μ€ ν μ΄λΈμ ν λλ‘ JAVA μμ€μ½λλ‘ μμ±ν΄μ£Όλ©΄λλ€.
UserDTO ν΄λμ€ μ€μ
1) νμΌ μμ±
- Java Resources ν΄λ > src > user ν¨ν€μ§λ₯Ό μμ±νκ³ ,
- user ν¨ν€μ§ > UserDTO.java ν΄λμ€, UserDAO ν΄λμ€λ₯Ό μμ±ν΄μ€λ€.
- μμ±ν UserDTO.java νμΌ μμ λ€μ μ½λλ₯Ό μμ±ν΄μ€λ€.
package user;
public class UserDTO {
private String userID;
private String userPassword;
private String userEmail;
private String userEmailHash;
private Boolean userEmailChecked;
}
2) λ³μμ getter, setter μμ±
μ€λ₯Έμͺ½ λ§μ°μ€ ν΄λ¦ > Source > "Generate getters and setters"λ₯Ό ν΄λ¦ν΄
λͺ¨λ λ³μμ getterμ setterλ₯Ό μμ±ν΄μ€λ€.
3) λ³μμ μμ±μ μμ±
- μ€λ₯Έμͺ½ λ§μ°μ€ ν΄λ¦ > Source > "Generate constructor using fields"λ₯Ό ν΄λ¦ν΄ λͺ¨λ λ³μμ μμ±μλ₯Ό μμ±ν΄μ€λ€.
- μ무κ²λ λ΄μ§μλ μμ±μ λν μΆκ°ν΄μ€λ€. => μ΄κΈ°νν΄μ£Όλ ν¨μ
public UserDTO() {}
UserDAO ν΄λμ€ μ€μ
1) login ν¨μ μ€μ
package user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import util.DatabaseUtil;
public class UserDAO {
public int login(String userID, String userPassword) {
String SQL = "SELECT userPassword FROM USER WHERE userID=?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
rs = pstmt.executeQuery();
if(rs.next()) {
if(rs.getString(1).equals(userPassword)) {
return 1; // λ‘κ·ΈμΈ μ±κ³΅
}
else {
return 0; // λΉλ°λ²νΈ νλ¦Ό
}
}
return -1; // μμ΄λ μμ
} catch (Exception e) {
e.printStackTrace();
} finally {
try {if (conn != null) conn.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (pstmt != null) pstmt.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (rs != null) rs.close();} catch (Exception e2) {e2.printStackTrace();}
}
return -1; // λ°μ΄ν°λ² μ΄μ€ μ€λ₯
}
}
2) join ν¨μ μ€μ
public int join(UserDTO user) {
String SQL = "INSERT INTO USER VALUES (?, ?, ?, ?, false);";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, user.getUserID());
pstmt.setString(1, user.getUserPassword());
pstmt.setString(1, user.getUserEmail());
pstmt.setString(1, user.getUserEmailHash());
return pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {if (conn != null) conn.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (pstmt != null) pstmt.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (rs != null) rs.close();} catch (Exception e2) {e2.printStackTrace();}
}
return -1; // νμκ°μ
μ€ν¨
}
3) getUserEmailChecked ν¨μ μ€μ
public boolean getUserEmailChecked(String userID) {
String SQL = "SELECT userEmailChecked FROM USER WHERE userID = ?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getBoolean(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {if (conn != null) conn.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (pstmt != null) pstmt.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (rs != null) rs.close();} catch (Exception e2) {e2.printStackTrace();}
}
return false; // λ°μ΄ν°λ² μ΄μ€ μ€λ₯
}
4) getUserEmail ν¨μ μ€μ
public String getUserEmail(String userID) {
String SQL = "SELECT userEmail FROM USER WHERE userID = ?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getString(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {if (conn != null) conn.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (pstmt != null) pstmt.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (rs != null) rs.close();} catch (Exception e2) {e2.printStackTrace();}
}
return null; // λ°μ΄ν°λ² μ΄μ€ μ€λ₯
}
5) setUserEmailChecked ν¨μ μ€μ
setUserEmailChecked ν¨μ : μ΄λ©μΌ κ²μ¦μ ν΅ν΄μ μΈμ¦μ΄ μλ£κ° λλλ‘ ν΄μ£Όλ ν¨μ
public boolean setUserEmailChecked(String userID) {
String SQL = "UPDATE USER SET userEmailChecked = true WHERE userID = ?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getConnection();
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {if (conn != null) conn.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (pstmt != null) pstmt.close();} catch (Exception e2) {e2.printStackTrace();}
try {if (rs != null) rs.close();} catch (Exception e2) {e2.printStackTrace();}
}
return false; // λ°μ΄ν°λ² μ΄μ€ μ€λ₯
}
'π»WEB BackEnd > JSP' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
ꡬν(1) - νμκ°μ λ° μ΄λ©μΌ μΈμ¦ (0) | 2022.11.08 |
---|---|
DB μ€κ³ & λ°μ΄ν° λͺ¨λΈλ§ (0) | 2022.11.07 |
λ‘κ·ΈμΈ, νμκ°μ , λ‘κ·Έμμ ꡬν (0) | 2022.11.07 |
index.jsp νμ΄μ§ (0) | 2022.11.07 |
μΉλμμΈ νλ μμν¬ - Bootstrap & jQuery (0) | 2022.11.07 |