프로그래밍/게시판만들기

나도 답변형 게시판을 만들어보자 ! (2)

Jay Tech 2017. 1. 22. 23:44
반응형

DB 테이블을 생성해 보자.






각 처리 분야별 역할은

서비스 : 게시판 관련 로직을 수행한다. 

ListArticleService - 게시글 목록 제공

ReadArticleService - 게시글 읽기 기능

WriteArticleService - 게시글 쓰기 기능

ReplyArticleService - 답변 쓰기 기능

UpdateArticleService - 게시글 수정 기능

DeleteArticleService - 게시글 삭제 기능


DAO : DB 테이블에 대한 CRUD작업을 수행.

모델 : Article(게시글 데이터), ArticleListModel (게시글 목록 화면을 생성할 때 필요한 데이터 저장)




Article 모델

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
import java.util.Date;
 
public class Article {
 
    private int id;
    private int groupId;
    private String sequenceNumber;
    private Date postingDate;
    private int readCount;
    private String writerName;
    private String password;
    private String title;
    private String content;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getGroupId() {
        return groupId;
    }
    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }
    public String getSequenceNumber() {
        return sequenceNumber;
    }
    public void setSequenceNumber(String sequenceNumber) {
        this.sequenceNumber = sequenceNumber;
    }
    public Date getPostingDate() {
        return postingDate;
    }
    public void setPostingDate(Date postingDate) {
        this.postingDate = postingDate;
    }
    public int getReadCount() {
        return readCount;
    }
    public void setReadCount(int readCount) {
        this.readCount = readCount;
    }
    public String getWriterName() {
        return writerName;
    }
    public void setWriterName(String writerName) {
        this.writerName = writerName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
 
    public int getLevel() {
        if (sequenceNumber == null) {
            return -1;
        }
        if (sequenceNumber.length() != 16) {
            return -1;
        }
        if (sequenceNumber.endsWith("999999")) {
            return 0;
        }
        if (sequenceNumber.endsWith("9999")) {
            return 1;
        }
        if (sequenceNumber.endsWith("99")) {
            return 2;
        }
        return 3;
    }
    
}
 
cs



ArticleDao


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
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import mvjsp.chap17.board.model.Article;
import mvjsp.jdbc.JdbcUtil;
 
public class ArticleDao {
 
    private static ArticleDao instance = new ArticleDao();
    public static ArticleDao getInstance() {
        return instance;
    }
 
    private ArticleDao() {
    }
 
    public int selectCount(Connection conn) throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select count(*) from article");
            rs.next();
            return rs.getInt(1);
        } finally {
            JdbcUtil.close(rs);
            JdbcUtil.close(stmt);
        }
    }
 
    public List<Article> select(Connection conn, int firstRow, int endRow)
            throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement("select article_id, "
                    + "group_id, sequence_no, posting_date, "
                    + "read_count, writer_name, password, title "
                    + "from article order by sequence_no desc limit ?, ?");
            pstmt.setInt(1, firstRow - 1);
            pstmt.setInt(2, endRow - firstRow + 1);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                return Collections.emptyList();
            }
            List<Article> articleList = new ArrayList<Article>();
            do {
                Article article = makeArticleFromResultSet(rs, false);
                articleList.add(article);
            } while (rs.next());
            return articleList;
        } finally {
            JdbcUtil.close(rs);
            JdbcUtil.close(pstmt);
        }
    }
 
    private Article makeArticleFromResultSet(ResultSet rs, boolean readContent)
            throws SQLException {
        Article article = new Article();
        article.setId(rs.getInt("article_id"));
        article.setGroupId(rs.getInt("group_id"));
        article.setSequenceNumber(rs.getString("sequence_no"));
        article.setPostingDate(rs.getTimestamp("posting_date"));
        article.setReadCount(rs.getInt("read_count"));
        article.setWriterName(rs.getString("writer_name"));
        article.setPassword(rs.getString("password"));
        article.setTitle(rs.getString("title"));
        if (readContent) {
            article.setContent(rs.getString("content"));
        }
        return article;
    }
 
    public int insert(Connection conn, Article article) throws SQLException {
        PreparedStatement pstmt = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement("insert into article "
                    + "(group_id, sequence_no, posting_date, read_count, "
                    + "writer_name, password, title, content) "
                    + "values (?, ?, ?, 0, ?, ?, ?, ?)");
            pstmt.setInt(1, article.getGroupId());
            pstmt.setString(2, article.getSequenceNumber());
            pstmt.setTimestamp(3new Timestamp(article.getPostingDate()
                    .getTime()));
            pstmt.setString(4, article.getWriterName());
            pstmt.setString(5, article.getPassword());
            pstmt.setString(6, article.getTitle());
            pstmt.setString(7, article.getContent());
            int insertedCount = pstmt.executeUpdate();
 
            if (insertedCount > 0) {
                stmt = conn.createStatement();
                rs = stmt.executeQuery("select last_insert_id() from article");
                if (rs.next()) {
                    return rs.getInt(1);
                }
            }
            return -1;
        } finally {
            JdbcUtil.close(rs);
            JdbcUtil.close(stmt);
            JdbcUtil.close(pstmt);
        }
    }
 
    public Article selectById(Connection conn, int articleId)
            throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement("select * from article "
                    + "where article_id = ?");
            pstmt.setInt(1, articleId);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                return null;
            }
            Article article = makeArticleFromResultSet(rs, true);
            return article;
        } finally {
            JdbcUtil.close(rs);
            JdbcUtil.close(pstmt);
        }
    }
 
    public void increaseReadCount(Connection conn, int articleId)
            throws SQLException {
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("update article "
                    + "set read_count = read_count + 1 "
                    + "where article_id = ?");
            pstmt.setInt(1, articleId);
            pstmt.executeUpdate();
        } finally {
            JdbcUtil.close(pstmt);
        }
    }
 
    public String selectLastSequenceNumber(Connection conn,
            String searchMaxSeqNum, String searchMinSeqNum) 
    throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn
                    .prepareStatement("select min(sequence_no) from article "
                            + "where sequence_no < ? and sequence_no >= ?");
            pstmt.setString(1, searchMaxSeqNum);
            pstmt.setString(2, searchMinSeqNum);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                return null;
            }
            return rs.getString(1);
        } finally {
            JdbcUtil.close(rs);
            JdbcUtil.close(pstmt);
        }
    }
 
    public int update(Connection conn, Article article) throws SQLException {
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("update article "
                    + "set title = ?, content = ? where article_id = ?");
            pstmt.setString(1, article.getTitle());
            pstmt.setString(2, article.getContent());
            pstmt.setInt(3, article.getId());
            return pstmt.executeUpdate();
        } finally {
            JdbcUtil.close(pstmt);
        }
    }
 
    public void delete(Connection conn, int articleId) throws SQLException {
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("delete from article "
                    + "where article_id = ?");
            pstmt.setInt(1, articleId);
            pstmt.executeUpdate();
        } finally {
            JdbcUtil.close(pstmt);
        }
    }
 
}
 
cs


반응형