* ȸ¿ø°¡ÀÔ * ¾ÆÀ̵ð/ºñ¹Ð¹øÈ£ ã±â   ID PW
Last Modified : 2007.02.02

PreparedStatement

PreparedStatement ´Â Áö±Ý±îÁö »ç¿ëÇÑ Statement Ŭ·¡½º º¸´Ù Æí¸®Çϰí È¿°úÀûÀÔ´Ï´Ù.
ÀÚ¹Ù ¼Ò½º´Â ÄÄÆÄÀÏµÇ¾î ½ÇÇàÀÌ µÇÁö¸¸ Statement °¡ Àü´ÞÇÏ´Â SQL ¹®Àº DBMS°¡ ó¸®ÇÏ´Â °ÍÀ¸·Î DBMS°¡ ÇØ¼®ÇÒ ¼ö ÀÖ°Ô ¹Ì¸® ÄÄÆÄÀϵǾî Àü´ÞµÇÁö ¾Ê½À´Ï´Ù.
PreparedStatement ¸¦ »ç¿ëÇϸé ÄÄÆÄÀÏµÈ SQL¹®À» DBMS¿¡ Àü´ÞÇÏ¿© ¼º´ÉÀ» Çâ»ó ½Ãŵ´Ï´Ù.
PreparedStatement ÀÇ Æí¸®ÇÑ Á¡Àº SQL¹®¿¡ ? À» ³ÖÀ» ¼ö ÀÖ°í, ÀÌ ºÎºÐ¿¡ ÆÄ¶ó¹ÌÅ͸¦ Àü´ÞÇÒ ¼ö ÀÖ¾î µ¿ÀÏÇÑ ¹®ÀåÀ» ÆÄ¶ó¹ÌÅ͸¸ ¹Ù²Ù¾î ¿©·¯¹ø ¼öÇàÇÒ ¶§ Æí¸®ÇÕ´Ï´Ù.
ÀÌ ºÎºÐÀº ¾Æ·¡ ¿¹Á¦¸¦ ÅëÇØ¼­ È®ÀÎÇϰڽÀ´Ï´Ù. UsingPrepareStatements.java ÆÄÀÏÀ» ¾Æ·¡ JDBC ¼ø¼­·Î ÄÚµùÇÕ´Ï´Ù.

  1. JDBC µå¶óÀ̹ö ·Îµù
  2. Connection ¸Î±â
  3. SQL ½ÇÇà
  4. [SQL¹®ÀÌ select¹®À̾ú´Ù¸é ResultSetÀ» ÀÌ¿ëÇÑ Ã³¸®]
  5. ÀÚ¿ø ¹Ýȯ

UsingPrepareStatements.java

import java.sql.*;

public class UsingPrepareStatements {
  public static void main( String[] args ) {
    Connection con;
    String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";

    try {
      // 1. JDBC µå¶óÀ̹ö ·Îµù
      Class.forName( "oracle.jdbc.driver.OracleDriver" );
    } catch( java.lang.ClassNotFoundException e ) {
      System.err.print( "ClassNotFoundException : " );
      System.err.println( e.getMessage() );
    }
    
    try {
      // 2. Connection ¸Î±â
      con = DriverManager.getConnection( url, "scott", "tiger" );
      // 3. SQL ½ÇÇà
      PreparedStatement updateSales = con.prepareStatement(
        "UPDATE coffees SET sales = ? WHERE cof_name LIKE ? ");
      updateSales.setInt(1, 75);
      updateSales.setString(2, "Colombian");
      updateSales.executeUpdate();
      updateSales.setInt(1, 100);
      updateSales.setString(2, "French_Roast");
      updateSales.executeUpdate();
      // changes SALES column of French Roast row to 100
      updateSales.setString(2, "Espresso");
      updateSales.executeUpdate();
      // changes SALES column of Espresso row to 100 (the first
      // parameter stayed 100, and the second parameter was reset 
      // to "Espresso") 
      // 5. ÀÚ¿ø ¹Ýȯ 
      updateSales.close();
      con.close();
    } catch( SQLException ex ) {
      System.out.println( "SQLException : " + ex.getMessage() );
    }
  }
}

SQL ¹®¿¡¼­ ? ¸¦ ³Ö°í À̰÷¿¡ PrepareStatement ÀÇ setXXX ¸Þ¼Òµå¸¦ ÀÌ¿ëÇØ¼­ ¼³Á¤ÇÕ´Ï´Ù.
XXX ºÎºÐÀº Å×À̺íÀÇ ÇØ´ç ÇʵåŸÀÔ¿¡ µû¶ó¼­ °áÁ¤ÇÕ´Ï´Ù.
setXXX ¸Þ¼ÒµåÀÇ Ã¹¹øÂ° ÆÄ¶ó¹ÌÅÍ´Â ? °¡ À§Ä¡Çϰí ÀÖ´Â ¼ø¼­ÀÔ´Ï´Ù.
setXXX ¸Þ¼ÒµåÀÇ µÎ¹øÂ° ÆÄ¶ó¹ÌÅÍ´Â ? ¸¦ ´ëüÇÒ ¹®ÀÚ¿­ÀÔ´Ï´Ù.
¾Æ·¡¿Í °°ÀÌ ÄÄÆÄÀÏÇÏ°í ½ÇÇàÇÑ °á°ú¿Í SQLPlus¿¡¼­ È®ÀÎÇÑ È­¸éÀÔ´Ï´Ù.

D:\>javac UsingPrepareStatements.java

D:\>java UsingPrepareStatements

D:\>sqlplus scott/tiger

SQL*Plus: Release 10.2.0.1.0 - Production on ±Ý 12¿ù 1 14:17:55 2006

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


´ÙÀ½¿¡ Á¢¼ÓµÊ:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from coffees;

COF_NAME                             SUP_ID      PRICE      SALES      TOTAL
-------------------------------- ---------- ---------- ---------- ----------
Espresso                                150       9.99        100          0
Colombian_Decaf                         101       8.99          0          0
French_Roast_Decaf                       49       9.99          0          0
Colombian                               101       7.99         75          0
French_Roast                             49       8.99        100          0

SQL>

´ÙÀ½Àº ·çÇÁ¹®À» ÀÌ¿ëÇØ¼­ ? ¿¡ ÇØ´çÇÏ´Â °ªÀ» ¼³Á¤ÇÏ¿© ½ÇÇàÇÏ´Â ÆÄÀÏÀÔ´Ï´Ù.

UsingPrepareStatements2.java

import java.sql.*;

public class UsingPrepareStatements2 {
  public static void main( String[] args ) {
    Connection con;
    String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";

    try {
      // 1.JDBC µå¶óÀ̹ö ·Îµù
      Class.forName( "oracle.jdbc.driver.OracleDriver" );
    } catch( java.lang.ClassNotFoundException e ) {
      System.err.print( "ClassNotFoundException :" );
      System.err.println( e.getMessage() );
    }
    
    try {
      // 2. Connection ¸Î±â
      con = DriverManager.getConnection( url, "scott", "tiger" );
      // 3. SQL ½ÇÇà
      PreparedStatement updateSales;
      String updateString = "UPDATE coffees " + 
        "SET sales = ? WHERE cof_name LIKE ?";
      
      updateSales = con.prepareStatement( updateString );
      
      
      int [] salesForWeek = {175, 150, 60, 155, 90};
      
      String [] coffees = { "Colombian", "French_Roast", "Espresso", 
      	"Colombian_Decaf", "French_Roast_Decaf" };
      
      int len = coffees.length;
      
      for ( int i = 0; i < len; i++ ) {
        updateSales.setInt(1, salesForWeek[i]);
        updateSales.setString(2, coffees[i]);
        updateSales.executeUpdate();
      }
      
      // 5. ÀÚ¿ø¹Ýȯ
      updateSales.close();
      con.close();
    } catch( SQLException ex ) {
      System.out.println( "SQLException : " + ex.getMessage() );
    }
  }
}
D:\>javac UsingPrepareStatements2.java

D:\>java UsingPrepareStatements2

D:\>sqlplus scott/tiger

SQL*Plus: Release 10.2.0.1.0 - Production on ±Ý 12¿ù 1 17:10:42 2006

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


´ÙÀ½¿¡ Á¢¼ÓµÊ:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from coffees;

COF_NAME                             SUP_ID      PRICE      SALES      TOTAL
-------------------------------- ---------- ---------- ---------- ----------
Espresso                                150       9.99         60          0
Colombian_Decaf                         101       8.99        155          0
French_Roast_Decaf                       49       9.99         90          0
Colombian                               101       7.99        175          0
French_Roast                             49       8.99        150          0

SQL>