댓글 추가 함수 예제 (Java)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public void addComment(Connection conn, int pseq, String writer, String password, String content) throws SQLException {
String insertCommentSQL = "INSERT INTO album_cmt_comment1 (pseq, writer, password, content, wdate) VALUES (?, ?, ?, ?, NOW())";
String updateBoardSQL = "UPDATE album_cmt_board1 SET cmtcnt = cmtcnt + 1 WHERE seq = ?";
try (PreparedStatement pstmt1 = conn.prepareStatement(insertCommentSQL);
PreparedStatement pstmt2 = conn.prepareStatement(updateBoardSQL)) {
conn.setAutoCommit(false);
// 댓글 추가
pstmt1.setInt(1, pseq);
pstmt1.setString(2, writer);
pstmt1.setString(3, password);
pstmt1.setString(4, content);
pstmt1.executeUpdate();
// cmtcnt 업데이트
pstmt2.setInt(1, pseq);
pstmt2.executeUpdate();
conn.commit();
} catch (SQLException e) {
conn.rollback();
throw e;
} finally {
conn.setAutoCommit(true);
}
}
'Java, Spring 🌱' 카테고리의 다른 글
[Java/Maven] Eclipse에서 Maven Project 생성 후 배포 + jdbc driver mariadb 설정 (0) | 2024.07.10 |
---|---|
Eclipse에서 만든 프로젝트 Apache-tomcat(아파치 톰캣)으로 자동 배포(deployment)하기 (0) | 2024.07.04 |
Context.xml과 JNDI(Java Naming and Directory Interface) (0) | 2024.07.03 |
Apache Tomcat(아파치 톰캣) 다운로드 및 실행 (0) | 2024.06.13 |
[JFrame, Swing] WindowBuilder 설치방법 (0) | 2024.05.28 |