JSP, SERVLET
Statement VS PreparedStatement 사용법 차이
Adev
2022. 10. 28. 00:01
Statement()
: executeQuery()로 실행할 때 파라미터로 쿼리문을 전달한다.
수행 시마다 컴파일을 하기 때문에 다중 수행 시 속도가 느리다.
ex)
public int selectCount(Connection conn) throws SQLException {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("select count(*) from pds_item");
rs.next();
return rs.getInt(1);
PreparedStatement()
: 실행 전에 미리 쿼리문을 컴파일 한다.
미리 컴파일해 놓은 구문을 사용하기 때문에 다중 수행 시 속도가 빠르다.
ex)
public int increaseCount(Connection conn, int id) throws SQLException {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement("update pds_item set downloadcount = downloadcount + 1 where pds_item_id = ?");
pstmt.setInt(1, id);
return pstmt.executeUpdate();
참고 : JSP 2.1 웹프로그래밍 (PdsItemDao.java)