반응형

 

이전 글 보러가기 ↓↓↓↓↓

https://maxtime1004.tistory.com/76

 

[JSP] MVC를 이용한 회원관리(조회) - 1

다음 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/77 <%@ taglib prefix="fmt" uri="http://jav.." data-og-host="maxtime1004.tistory.com" data-og-source-url="https://maxtime1004.tistory.com/7..

maxtime1004.tistory.com

 

다음 글 보러가기 ↓↓↓↓↓

https://maxtime1004.tistory.com/78

 

[JSP] MVC를 이용한 회원관리(수정/삭제) - 3

이전 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/77 [JSP] MVC를 이용한 회원관리(추가/회원가입) - 2 이전 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/76 [JSP] MVC를 이용한 회원..

maxtime1004.tistory.com


 

listMembers.jsp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<%@ page language="java" contentType="text/html; charset=UTF-8"
    import=" java.util.*,sec01.ex01.*"
    pageEncoding="UTF-8"
    isELIgnored="false"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    request.setCharacterEncoding("utf-8");
%>
<c:set var="contextPath" value="${pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table{
    text-align: center;
    border : 2px solid gray;
}
tr{
    text-align: center;
    border : 2px solid gray;
}
tr th{
    border : 1px solid gray;
    background-color: lightgreen;
}
td{
    border : 1px solid gray;
    width: 7%;
}
.cls1{
    font-size: 40px;
    text-align: center;
}
 
</style>
<meta charset="UTF-8">
<title>회원 정보 출력창</title>
</head>
<body>
    <p class="cls1">회원정보</p>
    <table>
        <tr>
            <th><b>아이디</b></th>
            <th><b>비밀번호</b></th>
            <th><b>이름</b></th>
            <th><b>이메일</b></th>
            <th><b>가입일</b></th>
        </tr>
        
        <c:choose>
            <c:when test="${empty membersList}">
                <tr>
                    <td colspan="5">
                        <b>등록된 회원이 없습니다.</b>
                    </td>
                </tr>
            </c:when>
            
            <c:when test="${!empty membersList}">
                <c:forEach var="mem" items="${membersList }">
                    <tr>
                        <td>${mem.id }</td>
                        <td>${mem.pwd }</td>
                        <td>${mem.name }</td>
                        <td>${mem.email }</td>
                        <td>${mem.joinDate }</td>
                    </tr>
                </c:forEach>
            </c:when>
        </c:choose>
    </table>
    <br>
    <a href="${contextPath}/member/memberForm.do">회원 가입하기</a>
</body>
</html>
cs

 

 

memberForm.jsp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    isELIgnored="false"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html>
<head>
<style>
h1{
    text-align: center;
}
table{
    text-align: center;
}
th{
    width: 200px;
}
td{
    width: 400px;
}
</style>
<meta charset="UTF-8">
<title>회원가입창</title>
</head>
<body>
<form method="post" action="${contextPath}/member/addMember.do">
    <h1>회원가입창</h1>
    <table>
        <tr>
            <th>
                <p align="right">아이디</p>
            </th>
            <td><input type="text" name="id"></td>
        </tr>
        
        <tr>
            <th>
                <p align="right">비밀번호</p>
            </th>
            <td><input type="password" name="pwd"></td>
        </tr>
        
        <tr>
            <th>
                <p align="right">이름</p>
            </th>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <th>
                <p align="right">이메일</p>
            </th>
            <td><input type="text" name="email"></td>
        </tr>
        <tr>
            <th>
                <p align="right">&nbsp;</p>
            </th>
            <td>
            <input type="submit" value="가입하기">
            <input type="reset" value="다시입력">
            </td>
        </tr>
    </table>
</form>
</body>
</html>
cs

 

 

MemberVO

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package sec01.ex01;
 
import java.util.Date;
 
public class MemberVO {
    private String id;
    private String pwd;
    private String name;
    private String email;
    private Date joinDate;
    
    
    public MemberVO(String id, String pwd, String name, String email) {
        this.id = id;
        this.pwd = pwd;
        this.name = name;
        this.email = email;
    }
    
    public MemberVO(String id, String pwd, String name, String email, Date joinDate) {
        this.id = id;
        this.pwd = pwd;
        this.name = name;
        this.email = email;
        this.joinDate = joinDate;
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getPwd() {
        return pwd;
    }
 
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public Date getJoinDate() {
        return joinDate;
    }
 
    public void setJoinDate(Date joinDate) {
        this.joinDate = joinDate;
    }
 
}
 
 
cs

 

 

MemberDAO

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sec01.ex01;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
public class MemberDAO {
    private DataSource dataFactory;
    private Connection conn;
    private PreparedStatement pstmt;
    
    public MemberDAO() {
        try {
            Context ctx = new InitialContext();
            Context envContext = (Context) ctx.lookup("java:/comp/env");
            dataFactory = (DataSource) envContext.lookup("jdbc/oracle");
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public List<MemberVO> listMembers(){
        List<MemberVO> membersList = new ArrayList<MemberVO>();
        try {
            conn = dataFactory.getConnection();
            String query = "select * from t_member order by joinDate desc";
            System.out.println(query);
            pstmt = conn.prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                String id = rs.getString("id");
                System.out.println(id);
                String pwd = rs.getString("pwd");
                String name = rs.getString("name");
                String email = rs.getString("email");
                Date joinDate = rs.getDate("joinDate");
                
                MemberVO memberVO = new MemberVO(id, pwd, name, email, joinDate);
                membersList.add(memberVO);
            }
            rs.close();
            pstmt.close();
            conn.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return membersList;
    }
 
    public void addMember(MemberVO m) {
 
        try {
            conn = dataFactory.getConnection();
            String id = m.getId();
            String pwd = m.getPwd();
            String name = m.getName();
            String email = m.getEmail();
            String query = "insert into t_member(id, pwd, name, email)"
                            + " values(?, ?, ?, ?)";
            System.out.println(query);
            pstmt = conn.prepareStatement(query);
            pstmt.setString(1, id);
            pstmt.setString(2, pwd);
            pstmt.setString(3, name);
            pstmt.setString(4, email);
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
 
cs

 

 

MemberController

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package sec01.ex01;
 
import java.io.IOException;
 
import javax.servlet.RequestDispatcher;
import java.util.List;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MemberController
 */
@WebServlet("/member/*"//   /mem.do -> /member/* 
public class MemberController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    MemberDAO memberDAO;
    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        memberDAO = new MemberDAO();
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        doHandle(request,response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        doHandle(request,response);
    }
    
    //변경
    private void doHandle(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException{
        String nextPage = null//변경
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String action = request.getPathInfo();
        System.out.println("action : " + action);
        
        if(action == null || action.equals("/listMembers.do")) {
            List<MemberVO> membersList =  memberDAO.listMembers();
            request.setAttribute("membersList", membersList);
            nextPage = "/test02/listMembers.jsp";
        }else if(action.equals("/addMember.do")) {
            String id = request.getParameter("id");
            String pwd = request.getParameter("pwd");
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            MemberVO memberVO = new MemberVO(id,pwd,name,email);
            memberDAO.addMember(memberVO);
            nextPage="/member/listMembers.do";
        }else if(action.equals("/memberForm.do")) {
            System.out.println("여기");
            nextPage = "/test02/memberForm.jsp";
        }else {
            List<MemberVO> membersList = memberDAO.listMembers();
            request.setAttribute("membersList", membersList);
            nextPage = "/test02/listMembers.jsp";
        }
        RequestDispatcher dispatch = request.getRequestDispatcher(nextPage);
        dispatch.forward(request, response);
        
    }
 
}
 
cs

 

 

실행화면

회원 정보 테이블이 나타난다. 회원 가입하기를 클릭한다.

 

 

회원 정보를 입력하고 가입하기 버튼을 클릭하면 다시 이전 화면으로 돌아와 회원 테이블을 보여준다.

 

 

 

반응형
반응형

다음 글 보러가기 ↓↓↓↓↓

https://maxtime1004.tistory.com/77

 

[JSP] MVC를 이용한 회원관리(추가/회원가입) - 2

이전 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/76 [JSP] MVC를 이용한 회원관리(조회) - 1 다음 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/77 <%@ taglib prefix="fmt" uri="http:..

maxtime1004.tistory.com


 

 

라이브러리에 jstl jar 파일을 추가합니다. 아래의 링크를 통해 다운받을 수 있습니다.

http://tomcat.apache.org/taglibs/standard/

 

Apache Taglibs - Apache Standard Taglib: JSP[tm] Standard Tag Library (JSTL) implementations

ojdbc jar파일을 추가합니다. 아래의 링크를 통해 다운받을 수 있습니다.

https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html

 

tomcat dbcp 파일을 넣고 Server > context.xml 과 Webcontent > web.xml을 수정합니다.

아래의 링크를 통해 방법을 확인할 수 있습니다.

https://maxtime1004.tistory.com/57

 

[JSP] 커넥션풀(Connection Pool) 데이터베이스 연동하기

▶ 커넥션풀(ConnectionPool) 기존 데이터베이스 연동 방법은 애플리케이션에서 데이터베이스에 연결하는 과정에서 시간이 많이 걸리는데, 이 문제를 해결하기 위해 나온 방법이 커넥션풀(ConnectionPo

maxtime1004.tistory.com

 

 

MemberVO.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package sec01.ex01;
 
import java.util.Date;
 
public class MemberVO {
    private String id;
    private String pwd;
    private String name;
    private String email;
    private Date joinDate;
    
    
    public MemberVO(String id, String pwd, String name, String email) {
        this.id = id;
        this.pwd = pwd;
        this.name = name;
        this.email = email;
    }
    
    public MemberVO(String id, String pwd, String name, String email, Date joinDate) {
        this.id = id;
        this.pwd = pwd;
        this.name = name;
        this.email = email;
        this.joinDate = joinDate;
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getPwd() {
        return pwd;
    }
 
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public Date getJoinDate() {
        return joinDate;
    }
 
    public void setJoinDate(Date joinDate) {
        this.joinDate = joinDate;
    }
 
}
 
 
cs

 

 

MemberDAO.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package sec01.ex01;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
public class MemberDAO {
    private DataSource dataFactory;
    private Connection conn;
    private PreparedStatement pstmt;
    
    public MemberDAO() {
        try {
            Context ctx = new InitialContext();
            Context envContext = (Context) ctx.lookup("java:/comp/env");
            dataFactory = (DataSource) envContext.lookup("jdbc/oracle");
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public List<MemberVO> listMembers(){
        List<MemberVO> membersList = new ArrayList<MemberVO>();
        try {
            conn = dataFactory.getConnection();
            String query = "select * from t_member order by joinDate desc";
            System.out.println(query);
            pstmt = conn.prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                String id = rs.getString("id");
                System.out.println(id);
                String pwd = rs.getString("pwd");
                String name = rs.getString("name");
                String email = rs.getString("email");
                Date joinDate = rs.getDate("joinDate");
                
                MemberVO memberVO = new MemberVO(id, pwd, name, email, joinDate);
                membersList.add(memberVO);
            }
            rs.close();
            pstmt.close();
            conn.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return membersList;
    }
}
 
cs

 

 

MemberConteroller.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package sec01.ex01;
 
import java.io.IOException;
 
import javax.servlet.RequestDispatcher;
import java.util.List;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MemberController
 */
@WebServlet("/mem.do")
public class MemberController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    MemberDAO memberDAO;
    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        memberDAO = new MemberDAO();
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        doHandle(request,response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        doHandle(request,response);
    }
    
    private void doHandle(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        List<MemberVO> membersList =  memberDAO.listMembers();
        request.setAttribute("membersList", membersList);
        
        RequestDispatcher dispatch = request.getRequestDispatcher("/test01/listMembers.jsp");
        dispatch.forward(request, response);
    }
 
}
 
cs

 

 

실행결과

 

반응형
반응형

· 유즈빈의 사용형식

 

1
<jsp:useBean id="빈 이름" class="패키지 이름을 포함한 자바 빈 클래스 [scope="접근범위"]/>
cs

 

id는 JSP 페이지에서 자바 빈 객체에 접근할 때 사용할 이름을 의미합니다.

class는 패키지 이름을 포함한 자바 빈 이름을 의미합니다.

scope는 자바 빈에 대한 접근 범위를 지정하는 역할을 합니다. (page, reqeust, session, application을 가지며 기본값은 page)

 

 

 

테이블 생성

 

1
2
3
4
5
6
7
create table t_member(
    id varchar2(10primary key,
    pwd varchar2(10),
    name varchar2(50),
    email varchar2(50),
    joinDate date default sysdate
);
cs

 

 

※ 커넥션 풀 연결방법을 알고싶다면?

maxtime1004.tistory.com/57

 

[JSP] 커넥션풀(Connection Pool) 데이터베이스 연동하기

▶ 커넥션풀(ConnectionPool) 기존 데이터베이스 연동 방법은 애플리케이션에서 데이터베이스에 연결하는 과정에서 시간이 많이 걸리는데, 이 문제를 해결하기 위해 나온 방법이 커넥션풀(ConnectionPo

maxtime1004.tistory.com

 

 

 

memberForm.html

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<head>
<style>
h1{
    text-align: center;
}
table{
    text-align: center;
}
.td1{
    width: 200px;
}
.td2{
    width: 400px;
}
p{
    text-align: right;
}
 
</style>
<meta charset="UTF-8">
<title>회원 가입창</title>
</head>
<body>
<form method="post" action="member.jsp">
    <h1>회원 가입창</h1>
    <table>
    <tr>
        <td class="td1">
            <p>아이디</p>
        </td>
        <td class="td2">
            <input type="text" name="id">
        </td>
    </tr>
    
    <tr>
        <td class="td1">
            <p>비밀번호</p>
        </td>
        <td class="td2">
            <input type="password" name="pwd">
        </td>
    </tr>
    
    <tr>
        <td class="td1">
            <p>이름</p>
        </td>
        <td class="td2">
            <input type="text" name="name">
        </td>
    </tr>
    
    <tr>
        <td class="td1">
            <p>이메일</p>
        </td>
        <td class="td2">
            <input type="text" name="email">
        </td>
    </tr>
    
    <tr>
        <td class="td1">
            <p>&nbsp;</p>
        </td>
        <td class="td2">
            <input type="submit" value="가입하기">
            <input type="reset" value="다시입력">
        </td>
    </tr>
    
    </table>
 
 
</form>
 
</body>
</html>
cs

 

아이디, 비밀번호, 이름, 이메일을 입력하고 가입하기 버튼을 클릭하면 member.jsp로 파라미터를 전송합니다.

 

 

 

member.jsp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<%@ page language="java" contentType="text/html; charset=UTF-8"
    import="java.util.*, sec01.ex01.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("UTF-8");
%>
 
<jsp:useBean id="m" class="sec01.ex01.MemberBean" scope="page" />
 
<%
    String id = request.getParameter("id");
    String pwd = request.getParameter("pwd");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    
    m.setId(id);
    m.setPwd(pwd);
    m.setName(name);
    m.setEmail(email);
    
    MemberDAO memberDAO = new MemberDAO();
    
    memberDAO.addMember(m);
    List memberList = memberDAO.listMembers();
    
%>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table{
    border : 1px solid black;
    text-align: center;
}
tr{
    text-align: center;
    padding: 2px;
}
.tr1{
    background-color: yellow;
}
td{
    width: 100px;
}
p{
    text-align: center;
}
 
</style>
<meta charset="UTF-8">
<title>회원 목록창</title>
</head>
<body>
<table border="1">
    <tr class="tr1">
        <td>아이디</td>
        <td>비밀번호</td>
        <td>이름</td>
        <td>이메일</td>
        <td>가입일</td>
    
    </tr>
    <%
        if(memberList.size()==0){
            
    %>
    <tr>
        <td colspan="5">등록된 회원이 없습니다.</td>
    </tr>
    <% 
        }else{
            for(int i=0; i<memberList.size(); i++){
                MemberBean bean = (MemberBean) memberList.get(i);
    %>
            <tr>
                <td><%=bean.getId() %></td>
                <td><%=bean.getPwd() %></td>
                <td><%=bean.getName() %></td>
                <td><%=bean.getEmail() %></td>
                <td><%=bean.getJoinDate() %></td>
            </tr>
            
    
    <%        
            }
        }
    %>
</table>
 
</body>
</html>
cs

 

useBean의 아이디를 m으로 설정하고 sec01 > ex01 > MemberBean 경로에 있다고 class를 지정해줍니다.

파라미터를 변수에 세팅하고 useBean에도 세팅해줍니다.

그리고 memberDAO.addmember로 이동합니다.

 

 

 

④ memberDAO.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package sec01.ex01;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
public class MemberDAO {
    private Connection con;
    private PreparedStatement pstmt;
    private DataSource dataFactory;
 
    public MemberDAO() {
        try {
            Context ctx = new InitialContext();
            Context envContext = (Context) ctx.lookup("java:/comp/env");
            dataFactory = (DataSource) envContext.lookup("jdbc/oracle");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public List listMembers() {
        List list = new ArrayList();
        try {
            con = dataFactory.getConnection();
            String query = "select * from t_member order by joinDate desc ";
            System.out.println("prepareStatment: " + query);
            pstmt = con.prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                String id = rs.getString("id");
                String pwd = rs.getString("pwd");
                String name = rs.getString("name");
                String email = rs.getString("email");
                Date joinDate = rs.getDate("joinDate");
                
                MemberBean vo = new MemberBean();
                vo.setId(id);
                vo.setPwd(pwd);
                vo.setName(name);
                vo.setEmail(email);
                vo.setJoinDate(joinDate);
                list.add(vo);
            }
            rs.close();
            pstmt.close();
            con.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    public void addMember(MemberBean memberBean) {
        try {
            Connection con = dataFactory.getConnection();
            
            String id = memberBean.getId();
            String pwd = memberBean.getPwd();
            String name = memberBean.getName();
            String email = memberBean.getEmail();
            
            String query = "insert into t_member";
            query += "(id, pwd, name, email)";
            query += " values(?,?,?,?)";
            System.out.println("prepareStatment : " + query);
            pstmt = con.prepareStatement(query);
            pstmt.setString(1, id);
            pstmt.setString(2, pwd);
            pstmt.setString(3, name);
            pstmt.setString(4, email);
            
            pstmt.executeUpdate();
            pstmt.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
cs

 

addmember 메소드를 통해 DB에 정보를 추가합니다.

다시 member.jsp로 돌아가 다시 dao의 listMembers 메소드를 통해 학생의 정보들을 조회하여 list로 return 합니다.

최종적으로 member.jsp에서 학생 list를 출력합니다.

 

 

만약 학생이 한명도 없다면 다음과 같은 문구가 뜨게 됩니다.

 

반응형

+ Recent posts