-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathArticleDao.java
More file actions
54 lines (44 loc) · 1.77 KB
/
ArticleDao.java
File metadata and controls
54 lines (44 loc) · 1.77 KB
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
package db;
import model.Article;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ArticleDao {
// 게시글 저장
public void insert(Article article) {
String sql = "INSERT INTO ARTICLE (writer, title, contents, imagePath) VALUES (?, ?, ?, ?)";
try (Connection connection = ConnectionManager.getConnection();
PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, article.writer());
pstmt.setString(2, article.title());
pstmt.setString(3, article.contents());
pstmt.setString(4, article.imagePath());
pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Failed to save article: " + e.getMessage());
}
}
// 게시글 조회
public List<Article> selectAll() {
String sql = "SELECT * FROM ARTICLE ORDER BY createdAt DESC";
List<Article> articles = new ArrayList<>();
try (Connection connection = ConnectionManager.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Article article = new Article(
rs.getLong("id"),
rs.getString("writer"),
rs.getString("title"),
rs.getString("contents"),
rs.getTimestamp("createdAt").toLocalDateTime(),
rs.getString("imagePath")
);
articles.add(article);
}
} catch (SQLException e) {
throw new RuntimeException("Failed to retrieve article list: {}", e);
}
return articles;
}
}