๐Ÿ‘€ 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 ํŽ˜์ด์ง€์—์„œ๋Š” ์•„๊นŒ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค!

ํƒœ๊ทธ: , ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: