반응형

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

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

 

 

실행결과

 

반응형

+ Recent posts