敬的世界

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          關(guān)于 PreparedStatement

          If you have a SQL statement that needs to be executed multiple times, it is more efficient to use a JDBC PreparedStatement object to run it. JDBC PreparedStatement class supports the following main features:

          • ????SQL statements PreparedStatement objects are pre-compiled on the database server side.
          • ????IN parameters are supported in SQL statements in PreparedStatement objects.
          • ????Batch execution mode is supported to run the run SQL statement multiple times in a single transaction.

          您可以使用Connection的prepareStatement()方法建立好一個(gè)預(yù)先編譯(precompile)的SQL語句,當(dāng)中參數(shù)會(huì)變動(dòng)的部份,先指定"?"這個(gè)佔(zhàn)位字元,例如:

          PreparedStatement stmt = conn.prepareStatement(
          ??? "INSERT INTO message VALUES(?, ?, ?, ?, ?)");

          使用PreparedStatement也可以進(jìn)行批次處理,直接來看個(gè)例子就知道如何使用:

          PreparedStatement stmt = conn.prepareStatement(
          ??? "INSERT INTO Users VALUES(?,?, ?)");
          ?
          User[] users = ...;

          for(int i=0; i<users.length; i++) {
          ???? stmt.setInt(1, users[i].getID());
          ???? stmt.setString(2, users[i].getName());
          ???? stmt.setString(3, users[i].getPassword());
          ???? stmt.addBatch( );
          }
          ?
          stmt.executeBatch();?

          A PreparedStatement object should be created from a Connection object with the prepareStatement() method and executed like a regular Statement object as shown in the following program:



          import java.util.*;
          import java.sql.*;
          import javax.sql.*;
          import javax.naming.*;
          public class MySqlPreparedSelect {
          ? public static void main(String [] args) {
          ??? Connection con = null;
          ??? try {
          ????? com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
          ??????? = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
          ????? ds.setServerName("localhost");
          ????? ds.setPortNumber(3306);
          ????? ds.setDatabaseName("HerongDB");
          ????? ds.setUser("Herong");
          ????? ds.setPassword("TopSecret");
          ????? con = ds.getConnection();

          // PreparedStatement for SELECT statement
          ????? PreparedStatement sta = con.prepareStatement(
          "SELECT * FROM Profile WHERE ID = 2");

          // Execute the PreparedStatement as a query
          ????? ResultSet res = sta.executeQuery();

          // Get values out of the ResultSet
          ????? res.next();
          ????? String firstName = res.getString("FirstName");
          ????? String lastName = res.getString("LastName");
          ????? System.out.println("User ID 2: "+firstName+' '+lastName);

          // Close ResultSet and PreparedStatement
          ????? res.close();
          ????? sta.close();

          ????? con.close();???????
          ??? } catch (Exception e) {
          ????? System.err.println("Exception: "+e.getMessage());
          ????? e.printStackTrace();
          ??? }
          ? }
          }

          posted on 2008-10-02 16:26 picture talk 閱讀(1100) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南澳县| 仪征市| 正阳县| 广丰县| 诸暨市| 安塞县| 独山县| 临泉县| 江永县| 施甸县| 牙克石市| 万盛区| 微博| 抚宁县| 五常市| 枣强县| 静海县| 阳原县| 靖远县| 泾川县| 清新县| 泰兴市| 万山特区| 大余县| 凌源市| 班戈县| 南昌县| 高尔夫| 公安县| 平利县| 洪湖市| 将乐县| 金沙县| 安新县| 厦门市| 周宁县| 田东县| 锦屏县| 新源县| 玛曲县| 榕江县|