- 자바
package web.jsp07.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class MemberDAO {
// 커넥션 메서드
private Connection getConnection() throws NamingException, SQLException {
Context ctx = new InitialContext();
Context env = (Context)ctx.lookup("java:comp/env");
DataSource ds = (DataSource)env.lookup("jdbc/orcl");
return ds.getConnection();
}
//회원가입 처리
public void addMember (MemberDTO dto) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection(); // Naming/SQLException 발생가능
String sql = "insert into member values(?,?,?,?,?,sysdate)";
// 커넥션으로 준비한 쿼리문 주고 실행기능 갖는 pstmt 리턴받기 (쿼리문 준비)
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dto.getId());
pstmt.setString(2, dto.getPw());
pstmt.setString(3, dto.getName());
pstmt.setString(4, dto.getGender());
pstmt.setString(5, dto.getEmail());
int result = pstmt.executeUpdate();
System.out.println("insert result : " + result);
}catch(Exception e) { // Naming/SQLException 발생했을때 둘다 받아줄 부모로 처리
e.printStackTrace();
}finally {
if(pstmt != null) try { pstmt.close(); } catch(SQLException e) { e.printStackTrace(); }
if(conn != null) try { conn.close(); } catch(SQLException e) { e.printStackTrace(); }
}
}
// 로그인 : id, pw 맞는지 체크해서 결과 돌려주는 메서드
// id,pw 맞으면 true, 둘중하나라도 틀리거나 없으면 false
public boolean idPwCheck(String id, String pw) {
System.out.println("idPwCheck 호출!");
boolean result = false; // 최종 결과 리턴해줄 변수 미리 선언
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection(); // Naming/SQLException 발생가능
String sql = "select * from member where id =? and pw =?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pw);
rs = pstmt.executeQuery();
// rs에 돌려받은 결과에 따라 result 변수값 변경
if(rs.next()) {
System.out.println("rs.next() -> true");
result = true;
}
}catch(Exception e) { // Naming/SQLException 발생했을때 둘다 받아줄 부모로 처리
e.printStackTrace();
}finally {
if(rs != null) try { rs.close(); } catch(SQLException e) { e.printStackTrace(); }
if(pstmt != null) try { pstmt.close(); } catch(SQLException e) { e.printStackTrace(); }
if(conn != null) try { conn.close(); } catch(SQLException e) { e.printStackTrace(); }
}
return result; // result변수에 들어있는 결과 리턴
}
}
'WEB Creator > Webpage' 카테고리의 다른 글
[Webpage] 회원가입 로그인 연습1 - 4-1) signup Form (0) | 2022.06.23 |
---|---|
[Webpage] 회원가입 로그인 연습1 - 4) loginForm (0) | 2022.06.23 |
[Webpage] 회원가입 로그인 연습1 - 2) DTO변수 get/set (0) | 2022.06.23 |
[Webpage] 회원가입 로그인 연습1 - 1) DB 테이블 (0) | 2022.06.23 |
[Webpage] 회원가입, 로그인 연습 1 (0) | 2022.06.23 |