6월25~27일 복습하기
주말내내 회원가입 Servlet/JSP 를 다시 만들어 보았다.
MVC2 모델 방식으로

1.VO
public class memberVO_01 {
private String name;
private String userid;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
2.DAO
public class memberDAO_01 {
//private으로 빈 객체 생성 하여 memberVO_01 변수에 담음
private static memberDAO_01 instance= new memberDAO_01();
//getInstance() 메서드를 만들어서 위에 만든 인스턴스를 꺼냄
public static memberDAO_01 getInstance() {
return instance;
}
public Connection getConnection() throws Exception {
Connection conn=null; //db연동 Connection 변수준비
Context initContext= new InitialContext(); //connectionPool 연결을 시작하는 객체
Context envContext=(Context)initContext.lookup("java:/comp/env");//리소스가 위치해 있는 곳으로 가서 정보를 읽어옴
DataSource ds=(DataSource)envContext.lookup("jdbc/myoracle"); //읽어온 정보안에 jdbc/myoracle에 매핑되는 리소스를 가지고와
//DataSource객체에 담는다.
conn=ds.getConnection(); //DataSource에서 연결 메서드를 불러와 Connection타입의 변수에 담는다.
return conn; //connectionPool연결메서드 객체를 반환. 이제 가져다 쓰기만 하면 바로 연결해서 쓸수 있음
}
public int userCheck(String userid, String pwd) { //Login시 id와 pwd가 맞는지 체크하기위함.
int result=-1; //디폴드 결과 값은 -1(존재하지 않는 회원)
String sql="select pwd from member_01 where userid=?"; //userid 기준 pwd를 조회하는 쿼리문
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn=getConnection(); //DB Connection 연결
pstmt=conn.prepareStatement(sql); //연결된 Connection에 sql문 날림
pstmt.setString(1, userid); //매개값으로 받아온 userid로 쿼리문의 ? 부분에 넣어줌
rs=pstmt.executeQuery(); //쿼리문실행 하여 결과 담아주기
if(rs.next()) { //결과에 꺼내올 다음 요소가 있다면
//DB에서 꺼내온요소의 pwd와 매개값으로 받은 pwd(로그인폼(login_01.jsp)에서읽어온값)가 같다면
if(rs.getString("pwd").equals(pwd)) {
result=1; //결과는 1
//DB에서 꺼내온요소의 pwd와 매개값으로 받은 pwd(로그인폼(login_01.jsp)에서읽어온값)가 같지않다면
}else if(!(rs.getString("pwd").equals(pwd))) {
result=0; //결과는 0
}else {
result=-1; //나머지는 -1로 반환
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {//아래는 자원 닫아주기
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result; //if문의 받은 결과값 반환
}
public memberVO_01 getMember(String userid) { //userid에 맞는 전체정보 불러오기
memberVO_01 mVo=new memberVO_01(); //자바빈 객체 생성
String sql="select * from member_01 where userid=?"; //userid 기준 전체를 조회하는 쿼리문
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs= null;
try {
conn=getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs=pstmt.executeQuery();
if(rs.next()) { //꺼낼 결과가 있다면
mVo.setName(rs.getString("name")); //DB에서 읽어온 name의 값을 자바빈의 setName메서드를 이용하여 새로설정해죠
mVo.setUserid(rs.getString("userid")); //DB에서 읽어온 userid의 값을 자바빈의 setUserid메서드를 이용하여 새로설정해죠
mVo.setPwd(rs.getString("pwd")); //DB에서 읽어온 pwd의 값을 자바빈의 setPwd메서드를 이용하여 새로설정해죠
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return mVo; //자바빈 객체반환
}
public int insertMember(memberVO_01 mVo) { //회원정보 추가하기
int result=-1; //디폴드 결과 값은 -1(회원가입 실패!!!)
String sql="insert into member_01 values(?,?,?)";
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn=getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, mVo.getName()); //매개변수 mVo(자바빈객체)에서 name을 읽어와 넣어줌
pstmt.setString(2, mVo.getUserid()); //매개변수 mVo(자바빈객체)에서 userid을 읽어와 넣어줌
pstmt.setString(3, mVo.getPwd()); //매개변수 mVo(자바빈객체)에서 pwd를 읽어와 넣어줌
result=pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
try {
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
//idCheckServlet01 에서 넘어옴
public int confirmID(String userid) { //사용가능한 아이디인지 알려줌
int result=-1;
String sql="select userid from member_01 where userid=?"; //입력받은 userid기준을 userid를 불러와줌
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs =null;
try {
conn=getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, userid);
rs=pstmt.executeQuery();
if(rs.next()) { //DB에 같은 userid가 있으면 꺼낼게 있으므로 1
result=1;
}else {
result=-1; //DB에 같은 userid가 없으면 꺼낼게 없으므로 -1
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
public void updateMember(memberVO_01 mVo) { //userid 기준으로 멤버 정보를 수정해줌
String sql="update member_01 set pwd=? where userid=?"; //userid 기준으로 pwd를 수정해줌
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn=getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, mVo.getPwd());
pstmt.setString(2, mVo.getUserid());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}