๐ DAO(Data Access Object)๋?
- ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๊ฐ์ฒด
DB
์ ๊ด๋ จ๋ ๋ชจ๋ ๋์์ ์ํํ๋ ํด๋์ค
- ์ง๊ธ๊น์ง
JDBC
๋ก DB
์ ์ฐ๊ฒฐํ ๋ ์ฌ์ฉํ๋ ค๋ ํ์ด์ง๋ง๋ค ์ผ์ผ์ด ์ฐ๊ฒฐ ์ฝ๋๋ฅผ ์ ์ด์ค์ผ ํ๋๋ฐ ์ด์ ๋ DAO
๋ฅผ ๋ง๋ค์ด์ ์ฐ๊ฒฐ ๋์์ ์ฌ๊ธฐ์ ์ํํ๋๋ก ํ๊ณ DB
์ฐ๊ฒฐ์ด ํ์ํ ํ์ด์ง์์๋ DAO
๋ง ํธ์ถํ๋ฉด ๋๋ค!
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MemberDAO {
// DAO (Data Access Object) : ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๊ฐ์ฒด
// => DB์ ๊ด๋ จ๋ ๋ชจ๋ ๋์์ ์ํํ๋ ํด๋์ค
// ์ฐ๊ฒฐ์ ํ์ํ ์ ๋ณด ์ ์ฅ
private Connection con = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private String sql = "";
// ๋๋น์ฐ๊ฒฐ ๋์
private Connection getConnect()
{
final String DRIVER = "com.mysql.cj.jdbc.Driver";
final String URL = "jdbc:mysql://localhost:3306/jspdb";
final String ID = "root";
final String PASS = "1234";
try // ์์ธ๊ฐ ๋ฐ์ํ ์ง๋ ๋ชจ๋ฅด๋ ์ฝ๋ ์์ฑ
{
// 1. ๋๋ผ์ด๋ฒ ๋ก๋
Class.forName(DRIVER);
// 2. ๋๋น์ฐ๊ฒฐ
con = DriverManager.getConnection(URL, ID, PASS);
System.out.println("DAO : ๋๋น์ฐ๊ฒฐ ์ฑ๊ณต!" + con);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} // ์ฐ๊ฒฐ ์ ๋ณด๊ฐ ๊ณ์ ์ ์ง๋์ด์ผ ํ๊ธฐ ๋๋ฌธ์ finally ๊ตฌ๋ฌธ์ ์์ด๋ค.
return con;
} // ๋๋น์ฐ๊ฒฐ
// ์์ํด์
public void CloseDB()
{
try {
// ๋ฆฌ์์ค ํด์ ๋ ์์ฑ์ ์ญ์์ผ๋ก ํ๋ค.
if (null != rs) rs.close(); // ์ฐ๊ฒฐํด์
if (null != pstmt) pstmt.close();
if (null != con) con.close();
System.out.println("DAO : ์์ํด์ ์๋ฃ");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // ์์ํด์
// ํ์๊ฐ์
- insertMember()
public void insertMember(MemberBean mb)
{
System.out.println("DAO : insertMember() ํธ์ถ");
try {
// 1. ๋๋ผ์ด๋ฒ ๋ก๋
// 2. ๋๋น์ฐ๊ฒฐ
con = getConnect();
// 3. sql ์์ฑ & pstmt ๊ฐ์ฒด ์์ฑ
sql = "insert into itwill_member(id, pass, name, age, gender, email, regdate) "
+ "values(?, ?, ?, ?, ?, ?, ?)";
pstmt = con.prepareStatement(sql);
// ???
pstmt.setString(1, mb.getId());
pstmt.setString(2, mb.getPass());
pstmt.setString(3, mb.getName());
pstmt.setInt(4, mb.getAge());
pstmt.setString(5, mb.getGender());
pstmt.setString(6, mb.getEmail());
pstmt.setTimestamp(7, mb.getRegdate());
// 4. sql ์คํ
int result = pstmt.executeUpdate();
System.out.println("DAO : ํ์๊ฐ์
์ฑ๊ณต!" + result);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
CloseDB();
}
} // ํ์๊ฐ์
- insertMember()
// ๋ก๊ทธ์ธ - loginCheck(mb)
public int loginCheck(MemberBean mb)
{
System.out.println("DAO : ๋ก๊ทธ์ธ - loginCheck(mb)");
int result = -1;
try {
// 1, 2. ๋๋น์ฐ๊ฒฐ
con = getConnect();
// 3. sql ์์ฑ(select) & pstmt ๊ฐ์ฒด ์์ฑ
sql = "select pass from itwill_member where id=?";
pstmt = con.prepareStatement(sql);
// ??
pstmt.setString(1, mb.getId());
// 4. sql ์คํ
rs = pstmt.executeQuery();
// 5. ๋ฐ์ดํฐ์ฒ๋ฆฌ
if (rs.next())
{
// ํ์
if (mb.getPass().equals(rs.getString("pass")))
{
// ๋ณธ์ธ
result = 1;
}
else
{
// ๋น๋ฐ๋ฒํธ ์ค๋ฅ
result = 0;
}
}
else
{
// ๋นํ์
result = -1;
}
System.out.println("DAO : ๋ก๊ทธ์ธ์ฒดํฌ ์๋ฃ (" + result + ")");
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseDB();
}
System.out.println("DAO : ๋ก๊ทธ์ธ - loginCheck(mb)");
return result;
} // ๋ก๊ทธ์ธ - loginCheck(mb)
} // MemberDAO
- ์ด๋ ๊ฒ
DAO
ํด๋์ค๋ฅผ ๋ง๋ ๋ค์์
<body>
<%
// ํ๊ธ์ฒ๋ฆฌ
request.setCharacterEncoding("UTF-8");
// ์ ๋ฌ๋๋ ์ ๋ณด ์ ์ฅ - ์ก์
ํ๊ทธ
%>
<jsp:useBean id="mb" class="com.itwillbs.member.MemberBean"></jsp:useBean>
<jsp:setProperty property="*" name="mb"/>
<%
// DB์ ๋ณด๋ฅผ ์ฌ์ฉํด์ ๋ก๊ทธ์ธ ์ฒดํฌ
// DAO ๊ฐ์ฒด ์์ฑ
MemberDAO dao = new MemberDAO();
int result = dao.loginCheck(mb);
System.out.println("pro : ๋ก๊ทธ์ธ์ฒดํฌ ์๋ฃ (" + result + ")");
if (1 == result)
{
session.setAttribute("id", mb.getId());
%>
<script type="text/javascript">
alert("๋ก๊ทธ์ธ ์ฑ๊ณต!")
location.href='main.jsp';
</script>
<%
}
else if (0 == result)
{
%>
<script type="text/javascript">
alert("๋น๋ฐ๋ฒํธ ์ค๋ฅ!");
history.back();
</script>
<%
}
else
{
%>
<script type="text/javascript">
alert("๋นํ์ ์
๋๋ค!");
history.back();
</script>
<%
}
// ์ฒดํฌ ๊ฒฐ๊ณผ์ ๋ฐ๋ฅธ ํ์ด์ง ์ด๋
%>
</body>
JSP
ํ์ด์ง์์๋ ์๊น ๋ง๋ ๊ฐ์ฒด๋ฅผ ํธ์ถํ๊ธฐ๋ง ํ๋ฉด ๋๋ค!