PreparedStatement
는 자바의 JDBC에서 SQL 쿼리를 사전에 컴파일하여 파라미터를 동적으로 설정할 수 있도록 하는 객체입니다. 이 객체는 보안성과 성능을 높이는 데 유용하며, 특히 SQL 인젝션 공격을 방지하는 데 중요한 역할을 합니다.
?
)를 미리 정의합니다.Connection.prepareStatement()
메서드를 사용해 PreparedStatement
객체를 생성합니다.setX()
메서드를 호출해 값을 설정합니다.executeQuery()
나 executeUpdate()
메서드를 사용해 쿼리를 실행합니다.setInt(int parameterIndex, int x)
: 정수형 파라미터 설정setString(int parameterIndex, String x)
: 문자열 파라미터 설정setDouble(int parameterIndex, double x)
: 실수형 파라미터 설정setDate(int parameterIndex, java.sql.Date x)
: 날짜형 파라미터 설정executeQuery()
: SELECT
문 실행 (결과로 ResultSet
반환)executeUpdate()
: INSERT
, UPDATE
, DELETE
실행 (영향받은 행 수 반환)1
부터 시작하며, SQL 문의 ?
순서와 일치해야 합니다.PreparedStatement
와 Connection
은 반드시 close()
를 호출하여 리소스를 반환합니다.SQLException
에 대비하여 적절히 예외를 처리하는 것이 좋습니다.PreparedStatement
는 쿼리를 사전에 컴파일하고, 동일한 쿼리를 여러 번 실행할 때 재사용하므로 성능이 향상됩니다.setInt()
, setString()
등으로 파라미터 값을 설정할 수 있습니다.// insert
package com.exam;
import java.sql.*;
public class PreparedStatementEx01 {
public static void main(String[] args) {
System.out.println("시작");
// 데이터베이스 URL, 사용자명, 비밀번호 설정
String url = "jdbc:mariadb://localhost:3306/sample";
String user = "root";
String password = "123456";
// Connection 및 PreparedStatement 객체 초기화
Connection conn = null;
PreparedStatement pstmt = null;
try {
// MariaDB JDBC 드라이버 로딩
Class.forName("org.mariadb.jdbc.Driver");
System.out.println("드라이버 로딩 성공");
// 데이터베이스 연결 생성
conn = DriverManager.getConnection(url, user, password);
System.out.println("데이터베이스 연결 성공");
// SQL 쿼리를 준비한 PreparedStatement 객체 생성
String sql = "insert into dept2 values (?, ?, ?)"; // 값만 물음표로 치환 가능(문자열X)
pstmt = conn.prepareStatement(sql);
// PreparedStatement에 각 파라미터 값 설정
pstmt.setInt(1, 61);
pstmt.setString(2, "영업부");
pstmt.setString(3, "서울");
// SQL 실행 및 데이터베이스에 삽입
pstmt.executeUpdate(); // 괄호 안 공백 주의
System.out.println("쿼리 실행 성공");
} catch (ClassNotFoundException e) {
// 드라이버 로딩 실패 시 오류 메시지 출력
System.out.println("[에러] " + e.getMessage());
} catch (SQLException e) {
// SQL 관련 예외 발생 시 오류 메시지 출력
System.out.println("[에러] " + e.getMessage());
} finally {
// 객체 닫기
if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) {}}
if (conn != null) { try { conn.close(); } catch (SQLException e) {}}
}
System.out.println("끝");
}
}
// update
package com.exam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class PreparedStatementEx02 {
public static void main(String[] args) {
System.out.println("시작");
String url = "jdbc:mariadb://localhost:3306/sample";
String user = "root";
String password = "exxyeon";
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("org.mariadb.jdbc.Driver");
System.out.println("드라이버 로딩 성공");
conn = DriverManager.getConnection(url, user, password);
System.out.println("데이터베이스 연결 성공");
**String sql = "update dept2 set loc = ? where deptno = ?;";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "광주");
pstmt.setString(2, "61"); // 61번을 광주로 바꿔줘
int result = pstmt.executeUpdate();**
System.out.println("쿼리 실행 성공");
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch (SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) {}}
if (conn != null) { try { conn.close(); } catch (SQLException e) {}}
}
System.out.println("끝");
}
}
// select - ResertSet!!
package com.exam;
import java.sql.*;
public class PreparedStatementEx03 {
public static void main(String[] args) {
System.out.println("시작");
String url = "jdbc:mariadb://localhost:3306/sample";
String user = "root";
String password = "exxyeon";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("org.mariadb.jdbc.Driver");
System.out.println("드라이버 로딩 성공");
conn = DriverManager.getConnection(url, user, password);
System.out.println("데이터베이스 연결 성공");
// String sql = "select * from dept2";
String sql = "select * from dept2 where deptno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "51");
rs = pstmt.executeQuery();
while (rs.next()) {
String deptno = rs.getString("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.println(deptno + "\t" + dname + "\t" + loc);
}
System.out.println("쿼리 실행 성공");
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch (SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) {}}
if (conn != null) { try { conn.close(); } catch (SQLException e) {}}
}
System.out.println("끝");
}
}