28DAY / DAO , DTO
DAO(Data Access Object) : 데이터 베이스에 접속, 명령전송을 전담하는 클래스
package DTO;
public class Member {
//전역변수 = 필드 = 멤버 = 멤버필드 = 프로필터
private String m_id;
private String m_pw;
private String m_level;
private String m_name;
private String m_email;
public String getM_id() {
return m_id;
}
public void setM_id(String m_id) {
this.m_id = m_id;
}
public String getM_pw() {
return m_pw;
}
public void setM_pw(String m_pw) {
this.m_pw = m_pw;
}
public String getM_level() {
return m_level;
}
public void setM_level(String m_level) {
this.m_level = m_level;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
public String getM_email() {
return m_email;
}
public void setM_email(String m_email) {
this.m_email = m_email;
}
}
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import driverdb.DriverDB;
import DTO.Member;
public class Mdao {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs=null;
public void mInsert(Member m) throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
try{
pstmt = conn.prepareStatement(
"INSERT INTO tb_member VALUES (?, ?, ?, ?, ?)");
pstmt.setString(1, m.getM_id());
pstmt.setString(2, m.getM_pw());
pstmt.setString(3, m.getM_level());
pstmt.setString(4, m.getM_name());
pstmt.setString(5, m.getM_email());
pstmt.executeUpdate();
}
finally{
if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}
}
public void mDelete(String id) throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
try{
pstmt = conn.prepareStatement(
"delete from tb_member where m_id=?");
pstmt.setString(1, id);
pstmt.executeUpdate();
}
finally{
if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}
}
public void mUpdate(Member m) throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
pstmt = conn.prepareStatement(
"UPDATE tb_member SET m_pw=?,m_level=?,m_name=?,m_email=? WHERE m_id=?");
pstmt.setString(1, m.getM_pw());
pstmt.setString(2, m.getM_level());
pstmt.setString(3, m.getM_name());
pstmt.setString(4, m.getM_email());
pstmt.setString(5, m.getM_id());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
public ArrayList<Member> mAllSelect() throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
ArrayList<Member> alm = new ArrayList<Member>();
pstmt = conn.prepareStatement("select * from tb_member");
rs = pstmt.executeQuery();
while(rs.next()){
Member m = new Member();
m.setM_id(rs.getString("m_id"));
m.setM_pw(rs.getString("m_pw"));
m.setM_level(rs.getString("m_level"));
m.setM_name(rs.getString("m_name"));
m.setM_email(rs.getString("m_email"));
System.out.println(m);
alm.add(m);
System.out.println(alm + "<---alm");
}
pstmt.close();
conn.close();
rs.close();
return alm;
}
public Member mSelectforUpdate(String mid) throws SQLException, ClassNotFoundException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
Member m = new Member();
pstmt = conn.prepareStatement("select * from tb_member where m_id=?");
pstmt.setString(1, mid);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("select 쿼리 실행 결과 있다");
m.setM_id(rs.getString("m_id"));
m.setM_pw(rs.getString("m_pw"));
m.setM_level(rs.getString("m_level"));
m.setM_name(rs.getString("m_name"));
m.setM_email(rs.getString("m_email"));
}
pstmt.close();
conn.close();
rs.close();
return m;
}
}
DAO와 DTO를 이용하면 jsp문이 간단해진다.
ex) insert_pro.jsp
<%@ page import = "DAO.Mdao"%>
<% request.setCharacterEncoding("euc-kr"); %>
<jsp:useBean id="m" class="Dto.Member"/>
<jsp:setProperty name="m" property="*"/>
<%
Udao udao = new Udao();
udao.uUpdate(m);
response.sendRedirect(request.getContextPath() + "/usersearch/user_search_list.jsp");
%>