1. JSP에 연동할 데이터베이스 생성하기
2. 회원정보 : 목록조회
- Controller : MemberList
package member.controller;
import java.io.IOException;
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 member.service.MemberService;
import member.service.MemberServiceImpl;
@WebServlet("/member/list")
public class MemberList extends HttpServlet{
private MemberService memberService = MemberServiceImpl.getInstance();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("members", memberService.list());
req.getRequestDispatcher("/WEB-INF/lib/jsp/member/list.jsp").forward(req, resp);
}
}
- Service : MemberService
package member.service;
import java.util.List;
import member.vo.MemberVo;
public interface MemberService {
public List<MemberVo> list();
}
- Service : MemberServiceImpl
package member.service;
import java.util.List;
import member.dao.MemberDao;
import member.vo.MemberVo;
public class MemberServiceImpl implements MemberService{
private static final MemberService memberService = new MemberServiceImpl();
public static MemberService getInstance() {
return memberService;
}
private MemberServiceImpl() {}
private MemberDao memberDao = MemberDao.getInstance();
@Override
public List<MemberVo> list() {
return memberDao.list();
}
}
- DAO : MemberDao
package member.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import member.vo.MemberVo;
import utils.DBConn;
public class MemberDao {
/**
* singleton
*/
private final static MemberDao dao = new MemberDao();
public static MemberDao getInstance() {
return dao;
}
public MemberDao() {}
/**
*
*/
private Statement stmt;
private PreparedStatement pstmt;
private Connection conn;
public List<MemberVo> list() {
List<MemberVo> list = new ArrayList<>();
try {
conn = DBConn.getConnection();
String query = "SELECT * FROM T_MEMBER";
pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int idx = 1;
MemberVo vo = new MemberVo(
rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getDate(idx++)
);
list.add(vo);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
- VO : MemberVo
package member.vo;
import java.sql.Date;
import lombok.Data;
@Data
public class MemberVo {
private final String id;
private final String pwd;
private final String name;
private final String email;
private final Date joinDate;
}
- JSP : list
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th>아이디</th>
<th>비밀번호</th>
<th>이름</th>
<th>이메일</th>
<th>가입일</th>
</tr>
<c:forEach items="${members}" var="member">
<tr>
<td>${member.id}</td>
<td>${member.pwd}</td>
<td>${member.name}</td>
<td>${member.email}</td>
<td>${member.joinDate}</td>
</tr>
</c:forEach>
</table>
</body>
</html>