DAO

jinkyung·2021년 1월 21일
0

JSP

목록 보기
11/20

Data Access Object

: 데이터에 접근하는 객체를 다음과 같이 따로 만들 수 있다

MemberDao.java

package spms.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;

import spms.vo.Member;

public class MemberDao {
	Connection connection;
	
	/*의존성 제어(DI, Dependency Injection)
	 * 역제어(IoC, Inversion of Control)*/
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	
	
	public List<Member> selectList() throws Exception{
		Statement stmt = null;
		ResultSet rs = null;
		String sqlSelect = "SELECT mno,mname,email,cre_date " + "\r\n" +
						   "FROM members " + "\r\n" + 
						   "ORDER BY mno ASC";
		try {
			stmt = connection.createStatement();
			rs = stmt.executeQuery(sqlSelect);
			ArrayList<Member> members = new ArrayList<Member>();
			while(rs.next()) {
				members.add(new Member()
							.setNo(rs.getInt("mno"))
							.setName(rs.getString("mname"))
							.setEmail(rs.getString("email"))
							.setCreatedDate(rs.getDate("cre_date")));
			}
			return members;
			
		}catch(Exception e) {
			throw e;
		}finally {
			try { if(rs != null) rs.close(); }catch(Exception e) { throw e;}
			try { if(stmt != null) stmt.close(); }catch(Exception e) { throw e;}
		}
	}
}

MemberListServlet

package spms.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import spms.dao.MemberDao;
import spms.vo.Member;

@SuppressWarnings("serial")
@WebServlet("/member/list")
public class MemberListServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Connection conn = null;	// MySQL 연결담당

		/*ServletContext 영역에 공유한 conn 객체를 가져와서 사용하겠다*/
		ServletContext sc = this.getServletContext();
		conn = (Connection)sc.getAttribute("conn");
		
		try {			
			
			resp.setContentType("text/html; charset=UTF-8"); // 먼저 호출
			
			
			//객체 생성을 Dao에 위임 
			MemberDao memberDao = new MemberDao();
			memberDao.setConnection(conn);
			List<Member> members = memberDao.selectList();
			
			// jsp에 전달하기 위해 request의 공유 공간에 저장한다
			req.setAttribute("members", members);
			
			// jsp로 request를 전달한다
			RequestDispatcher rd = 
					req.getRequestDispatcher("/member/MemberList.jsp");
			rd.include(req, resp);
			/* jsp로 전달(위임)하는 방식 2가지
			 * 1) forward : 제어권을 아예 넘겨준다(네가 알아서 처리해라)
			 * 2) include : 실행할 동안 제어권을 줬다가 처리가 끝나면 다시 넘겨 받는다
			 *              (마지막 확인은 내가 처리한다)
			 * */
			
		}catch(Exception e) {
			//throw new ServletException(e);
			
			req.setAttribute("error", e);
			RequestDispatcher rd = req.getRequestDispatcher("/Error.jsp");
			rd.forward(req, resp);
			
		}
	}
}

0개의 댓글