전 시간에 insert를 했으니 이번에는 글 목록을 select 해보자. select는 저번에 말한 개념에서 ResultSet을 활용해야해.
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Article> articles = new ArrayList<>();
try {
Class.forName("org.mariadb.jdbc.Driver");
String url = "jdbc:mariadb://127.0.0.1:3306/AM_DB_25_03?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul";
conn = DriverManager.getConnection(url, "root", "");
System.out.println("연결 성공!");
String sql = "SELECT *";
sql += " FROM article";
sql += " ORDER BY id DESC";
System.out.println(sql);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String regDate = rs.getString("regDate");
String updateDate = rs.getString("updateDate");
String title = rs.getString("title");
String body = rs.getString("body");
Article article = new Article(id, regDate, updateDate, title, body);
articles.add(article);
}
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로딩 실패" + e);
} catch (SQLException e) {
System.out.println("에러 : " + e);
} finally {
try {
if (rs != null && !rs.isClosed()) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (pstmt != null && !pstmt.isClosed()) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
아까 말한 ResultSet이 하나 필요하고, 목록이니까 List도 하나 필요하겠지.
이번에는 sql문에 select * from article order by id desc를 했는데 oder by id desc는 id를 내림차순 형태로 정렬 하겠다는 뜻이야.
그리고 rs로 select문을 실행시켜주고 while문을 rs.next()뭐 이런 조건을 걸고 빙빙 도는데, rs.next()는 첫번째 행부터 마지막 까지 행을 추출하는데, 다음에 행이 없을때 까지 반복을 하는 친구야.
글을 수정하는 기능은 insert하는 기능과 비슷해
글을 수정하려면 어떤 생각을 먼저 할까!?!? 새로운 제목과 내용이 필요하겠지?
음,, 그리고 sql문에 update article set ~~~ 이런식으로 하면 될 것같아.
System.out.println("==글 수정==");
System.out.print("새 제목 : ");
String title = sc.nextLine().trim();
System.out.print("새 내용 : ");
String body = sc.nextLine().trim();
/// ///////////////////////////////////////////
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("org.mariadb.jdbc.Driver");
String url = "jdbc:mariadb://127.0.0.1:3306/AM_DB_25_03?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul";
conn = DriverManager.getConnection(url, "root", "");
System.out.println("연결 성공!");
// 있는지 없는지?
String sql = "UPDATE article";
sql += " SET updateDate = NOW()";
if (title.length() > 0) {
sql += ", title = '" + title + "'";
}
if (body.length() > 0) {
sql += ", body = '" + body + "'";
}
sql += " WHERE id = " + id + ";";
System.out.println(sql);
pstmt = conn.prepareStatement(sql);
int affectedRows = pstmt.executeUpdate();
System.out.println("affected rows: " + affectedRows);
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로딩 실패" + e);
} catch (SQLException e) {
System.out.println("에러 : " + e);
} finally {
try {
if (pstmt != null && !pstmt.isClosed()) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(id + "번 글이 수정되었습니다");
}
}