Jsp - 게시글 생성,삭제,갱신,목록조회 DAO

2020. 7. 30. 21:41Jsp

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package board;
 
import java.sql.Timestamp;
 
public class BoardDTO {
    private int num;
    private int readcount;
    private String id;
    private String pass;
    private String name;
    private Timestamp time;
    private String subject;
    private String content;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public int getReadcount() {
        return readcount;
    }
    public void setReadcount(int readcount) {
        this.readcount = readcount;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPass() {
        return pass;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Timestamp getTime() {
        return time;
    }
    public void setTime(Timestamp time) {
        this.time = time;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}
 
//-----------------BoardDTO 끝------------------------
 
package board;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
public class BoardDAO {
    
    public  Connection con= null;
    public  PreparedStatement pstmt=null;
    public  ResultSet rs= null;
    
    public Connection getConnection()  throws Exception{//커넥션 
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/jspdb5";
        String user="jspid";
        String password="jsppass";
        con=DriverManager.getConnection(url, user, password);
        
        
        return con;
    }
 
    public void insertBoard(BoardDTO bb) {//게시판생성
    
        try {
            con=getConnection();
            String sql="select max(num) from board";
            pstmt=con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            if(rs.next()){
                int num=rs.getInt("max(num)")+1;
                bb.setNum(num);
            }
            
            sql="insert into board(num,name,pass,subject,content,readcount,time) values(?,?,?,?,?,?,?)";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1, bb.getNum());
            pstmt.setString(2, bb.getName());
            pstmt.setString(3, bb.getPass());
            pstmt.setString(4, bb.getSubject());
            pstmt.setString(5, bb.getContent());
            pstmt.setInt(6, bb.getReadcount());
            pstmt.setTimestamp(7, bb.getTime());
 
            
            pstmt.executeUpdate();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
                
        }
        
    }
    
    public List<BoardDTO> checkList() {//글목록
        List<BoardDTO> list = new ArrayList<BoardDTO>();
        try {
            con=getConnection();
            String sql="select * from board order by num desc";
            pstmt=con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while(rs.next()) {
                BoardDTO bb=new BoardDTO();
                bb.setNum(rs.getInt("num"));
                bb.setName(rs.getString("name"));
                bb.setPass(rs.getString("pass"));
                bb.setReadcount(rs.getInt("readcount"));
                bb.setContent(rs.getString("content"));
                bb.setSubject(rs.getString("subject"));
                bb.setTime(rs.getTimestamp("time"));
                list.add(bb);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
        }
        
        return list;
    }
    
    public void updateReadCount(int num) {//조회수상승
        try {
            con=getConnection();
            String sql= "UPDATE board SET readcount=readcount+1 WHERE num=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1, num);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
        }
        
    }
    
    public BoardDTO ContentCheck(int num) {//글내용 확인
        
        BoardDTO bb=new BoardDTO();
        try {
            con=getConnection();
    
            rs=getResultSet(num);
            if(rs.next()) {
                bb.setNum(rs.getInt("num"));
                bb.setName(rs.getString("name"));
                bb.setPass(rs.getString("pass"));
                bb.setReadcount(rs.getInt("readcount")+1);
                bb.setContent(rs.getString("content"));
                bb.setSubject(rs.getString("subject"));
                bb.setTime(rs.getTimestamp("time"));
                
                
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
        }
        return bb;
    }
    
    public ResultSet getResultSet(int num)throws Exception{//ResultSet  리턴
        con=getConnection();
        String sql="SELECT * FROM board where num=?";
        pstmt=con.prepareStatement(sql);
        pstmt.setInt(1, num);
        rs=pstmt.executeQuery();
        
        return rs;
    }
    
    public void UpdateBoard(BoardDTO bb) {//글내용 수정
        try {
        
            rs=getResultSet(bb.getNum());
            if(rs.next()) {
                
                if(bb.getPass().equals(rs.getString("pass"))){
                    String sql="UPDATE board SET name=?, subject=?, content=? where num=? ";
                    pstmt=con.prepareStatement(sql);
                    pstmt.setString(1, bb.getName());
                    pstmt.setString(2, bb.getSubject());
                    pstmt.setString(3, bb.getContent());
                    pstmt.setInt(4,bb.getNum());
                    
                    pstmt.executeUpdate();
                    
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
        }
        
        
    }
    
    public void DeleteBoard(BoardDTO bb) {//글내용 삭제
        try {
            con=getConnection();
            rs=getResultSet(bb.getNum());
            if(rs.next()) {
                if(bb.getPass().equals(rs.getString("pass"))) {
                    String sql="DELETE FROM board WHERE num=?";
                    pstmt=con.prepareStatement(sql);
                    pstmt.setInt(1, bb.getNum());
                    pstmt.executeUpdate();
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
        }
    }
    
    public int  getBoardCount() {//게시글 수 가져오기(페이징)
        int c=0;
        try {
            con=getConnection();
            String sql="SELECT count(*) FROM board";
            pstmt=con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            if(rs.next()) {
                
                c=rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
        }
        
        return c;
    }
    
    public List<BoardDTO> getBoardList(int startRow,int pageSize) {//게시판 글갯수만큼 가져오기(페이징)
        List<BoardDTO> list = new ArrayList<BoardDTO>();
        try {
            con=getConnection();
//            String sql="select * from board order by num desc limit 시작행-1,개수";
            String sql="select * from board order by num desc limit ?,?";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1, startRow-1);
            pstmt.setInt(2, pageSize);
            rs=pstmt.executeQuery();
            while(rs.next()) {
                BoardDTO bb=new BoardDTO();
                bb.setNum(rs.getInt("num"));
                bb.setName(rs.getString("name"));
                bb.setPass(rs.getString("pass"));
                bb.setReadcount(rs.getInt("readcount"));
                bb.setContent(rs.getString("content"));
                bb.setSubject(rs.getString("subject"));
                bb.setTime(rs.getTimestamp("time"));
                list.add(bb);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if (rs!=nulltry{rs.close();} catch(SQLException ex) {}
            if (pstmt!=nulltry{pstmt.close();} catch(SQLException ex) {}
            if (con!=nulltry{con.close();} catch(SQLException ex) {}
        }
        
        return list;
    }
    
}
 
 
 
cs

 

DB에 데이터를 저장과 반환을 위한 DTO(bean)과 DTO를 활용한 기능을 구현하기 위한 메서드의 집합인 DAO를 적어보았다. 어려운 건 없다고 생각하는데 sql 구문만 조금 달라지고 구현하기 위한 필수 코드들은 비슷하다.

그래도 헷갈리지말고 잘 활용해서 깔끔한 코딩을 해야겠다.

'Jsp' 카테고리의 다른 글

Jsp - 게시글내용  (0) 2020.07.30
Jsp - Update 게시글  (0) 2020.07.30
Jsp - Delete 게시글  (0) 2020.07.30
Jsp - 게시글 쓰기  (0) 2020.07.30
Jsp - DAO,Paging 을 활용한 게시판 글목록 불러오기  (1) 2020.07.30