20.03 ~ 20.08 국비교육/MySQL・ORACLE

PreparedStatement

찹키리 2020. 4. 9. 17:21


<PreparedStatement>


: 실행속도를 빠르게 하기 위한 메소드. 컴파일을 미리 하기 때문에 실행속도가 빨라진다

-> 매우 권장되는 기능이나, 데이터베이스가 지원해야 실행 가능하다.

 

 

sql ---> 문장(사람이 읽는 문자) ---> DB

1)해당 문장이 실행 가능한 지 구문 분석

2)접근 권한 확인

3)이미 실행된 경험이 있는 지 확인

4)이전에 실행 경험이 없다면 컴파일, 있다면 했던 걸 찾아 실행(더 빠르게 실행된다)
-> sql은 첫 번째보다 두 번재 실행이 더 빠른 이유

 

 

<PreparedStatement를 이용한 Insert>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
import java.sql.*;
 
public class InsertPrepDB{
    public static void main(String args[]) {
String url = "jdbc:mysql://localhost:3306/javadb?
useUnicode=true&characterEncoding=euckr";
        Connection con = null;        
        PreparedStatement pstmt = null;    
 
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
        } catch(java.lang.ClassNotFoundException e) {
            System.err.println(e.getMessage());
        }
 
        try {
            con = DriverManager.getConnection(url, "javauser""psw");
    String sqlStr = "insert into owner_info(id,nm,handphone,gender) values"
        + "(?, ?, ?, ?)"//미리 컴파일할 수 없는 부분
            pstmt = con.prepareStatement(sqlStr);
            //statement 객체 생성할 때 미리 명령문 입력 -> 
            pstmt.setInt(110);
            //첫 번째 물음표에 정수 10 입력
            pstmt.setString(2"홍길동");
            pstmt.setString(3"999-9999-9999");
            pstmt.setString(4"남");
 
            pstmt.executeUpdate();
        }catch(SQLException 3) {
            System.out.println(e.getMessage());
        }finally {
            try {
                pstmt.close();
                con.close();
            }catch(Exception e) {
                System.out.println(e.toString());
            }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

 

 

 

 

<PreparedStatement를 이용한 Select>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
import java.sql.*;
 
public class InsertPreDB{
    public static void main(String args[]) {
String url = "jdbc:mysql://localhost:3306/javadb?
useUnicode=true&characterEncoding=euckr";
        Connection con = null;        
        PreparedStatement pstmt = null;
        ResultSet rs = null;
 
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
        } catch(java.lang.ClassNotFoundException e) {
            System.err.println(e.getMessage());
        }
 
        try {
            con = DriverManager.getConnection(url, "javauser""psw");
    String sqlStr = "select * from owner_info where ID = ?";
            //? -> 미리 컴파일할 수 없는 부분
            pstmt = con.prepareStatement(sqlStr);
 
            pstmt.setInt(11);
            //첫 번째 물음표에 1 입력
 
            rs = pstmt.executeQuery();
            //이 과정을 생략하면 바로 오류가 생긴다.
        }catch(SQLException 3) {
            System.out.println(e.getMessage());
        }finally {
            try {
                rs.close();
                pstmt.close();
                con.close();
            }catch(Exception e) {
                System.out.println(e.toString());
            }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter