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)

'JSP, SERVLET' 카테고리의 다른 글

SERVLET  (0) 2023.01.11
EL JSTL list 값 가져오기  (0) 2022.12.30
<form>태그 multipart/form-data 인코딩 방식  (0) 2022.11.01
jsp :param, :include ... flush 태그  (0) 2022.10.31
dynamic web project 클래스 컴파일 방법  (0) 2022.10.05