본문 바로가기
JSP/JSP & Servlet

JSP 게시판 -DAO 클래스 설정, 글쓰기 폼

by avvin 2019. 5. 8.

JSP 게시판 -DAO 클래스 설정, 글쓰기 폼



DAO 클래스 만들기


커넥션풀을 사용하여 getCon() 메서드 작성


BoardDAO.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
package model;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
public class BoardDAO {
 
    Connection con;
    PreparedStatement pstmt;
    ResultSet rs;
 
    // 커넥션풀 이용하여 데이터베이스에 접근하는 메서드
    public void getCon() {
        try {
            // 외부에서 데이터를 읽어들여야한다
            Context initctx = new InitialContext();
            // 톰캣 서버의 정보를 담아놓은 곳으로 이동
            Context envctx = (Context) initctx.lookup("java:comp/env");
            // 데이터소스 객체(javax.sql.DataSource)를 선언
            // server.xml Context Resource에서 설정한 name을 매개값으로 준다.
            DataSource ds = (DataSource) envctx.lookup("jdbc/pool");
            // Resource이름을 매개값으로 주어 DataResource객체를 리턴
 
            // 외부에서 데이터를 읽어들이는 Context 객체를 생성
            // 그 객체의 lookup메서드로 톰캣서버정보를 읽어들이고
            // 톰캣서버정보를 읽어들인 객체의 lookup메서드 리소스이름을 매개값으로 주어
            // 데이터소스객체(커넥션풀 사용할때 쓰기로한 객체)를 얻는다
            // (커넥션풀)데이터소스를 이용하여 접속
            con = ds.getConnection();
 
        } catch (Exception e) {
 
            e.printStackTrace();
        }
 
    }
 
}
 



글쓰기 폼 만들기


BoardWriteForm.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<center>
    <h2>게시글 쓰기</h2>
 
    <form action="BoardWriteProc.jsp" method="post">
        <table width="600" border="1" bordercolor="gray" bgcolor="skyblue">
            <tr heignt="40">
                <td align="center" width="150">작성자</td>
                <td width="450"><input type="text" name="writer" size="60"></td>
            </tr>
            <tr heignt="40">
                <td align="center" width="150">제목</td>
                <td width="450"><input type="text" name="subject"size="60"></td>
            </tr>
            <tr heignt="40">
                <td align="center" width="150">이메일</td>
                <td width="450"><input type="email" name="email"size="60"></td>
            </tr>
            <tr heignt="40">
                <td align="center" width="150">비밀번호</td>
                <td width="450"><input type="password" name="password" size="60"></td>
            </tr>
            <tr heignt="40">
                <td align="center" width="150">글 내용</td>
                <td width="450">
                <textarea rows="10" cols="50" name="content"></textarea>
                </td>
            </tr>
            <tr heignt="40">
                <td align="center" colspan="2">
                <input type="submit" value="글쓰기"> &nbsp;&nbsp; 
                <input type="reset" value="다시 입력"> &nbsp;&nbsp;
                <button onclick="location.href=BoardList.jsp">전체 게시글 보기</button>
                </td>
            </tr>
        </table>
    </form>
</center>
</body>
</html>
cs