2019.02.19 Model2 : 게시판 만들기(2)

2019. 2. 19. 11:34JSP

#1 DAO 생성 -> DAO: Data Access Object


***bbsDao.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
87
88
89
90
91
92
93
94
95
96
97
98
package com.bbs.dao;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.bbs.dto.bbsDto;
 
public class bbsDao {
    
    private static bbsDao bDAO;
    private bbsDto bDTO;
    private Connection con;
    private PreparedStatement pstmt;
    private ResultSet rs;
    private int result;
    
    private bbsDao() {}
    
    // 종료메소드
    public void close(Connection con,PreparedStatement pstmt,ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    public static synchronized bbsDao getInstance() { //싱글톤 선언
        if(bDAO==null) {
            bDAO= new bbsDao();
        }
        return bDAO;
    }//bbsDao()
                     
    // DB연결 
    public Connection getConnection() {
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String id = "infortest";
        String pw = "1234";
        
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(url,id,pw);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }//Connection()           
    
    // 삽입 메소드
    public int insert(bbsDto bbsDto) {
            con = this.getConnection();
            StringBuffer query = new StringBuffer(); //SQL문 작성을 위한 버퍼
            query.append("INSERT INTO BBS(BBSID,BBSCATEGORY,BBSTITLE,BBSCONTENT,BBSDATE,BBSHIT,ID)");
            query.append(" VALUES(BBS_SEQ.NEXTVAL,?,?,?,SYSDATE,0,?)"); 
            
            try {
                pstmt = con.prepareStatement(query.toString()); //나눠진 SQL문을 합치는 작업
                pstmt.setString(1, bbsDto.getBbsCategory()); // ?의 값을 순서에 맞게 삽입
                pstmt.setString(2, bbsDto.getBbsTitle());
                pstmt.setString(3, bbsDto.getBbsContent());
                pstmt.setString(4, bbsDto.getBbsId());
                
                result =pstmt.executeUpdate(); // 실직적인 DB에 작성하는 문장
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                this.close(con,pstmt,null);
            }
            return result;
        }//insert()
    }
 
 
cs


#2 완성된 DAO를 controller에서 불러오는 작업


을 하는 문자을 추가한다.




** SQL문 작성시 DB에서 작성된 TABLE의 열순서와 같게 입력되어야 한다.

'JSP' 카테고리의 다른 글

2019.02.20 Model2 : 게시판 만들기(4)  (0) 2019.02.20
2019.02.19 Model2 : 게시판 만들기(3)  (0) 2019.02.19
2019.02.18 Model2 : 게시판 만들기(1)  (0) 2019.02.18
2019.02.18 JSPL functions  (0) 2019.02.18
2019.02.18 JSTL - JSTL core  (0) 2019.02.18