<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    敬的世界

    常用鏈接

    統(tǒng)計

    最新評論

    關于 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()方法建立好一個預先編譯(precompile)的SQL語句,當中參數(shù)會變動的部份,先指定"?"這個佔位字元,例如:

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

    使用PreparedStatement也可以進行批次處理,直接來看個例子就知道如何使用:

    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 閱讀(1099) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 久久国产免费福利永久| 日本免费电影一区二区| 午夜爱爱免费视频| 亚洲一级毛片在线观| 99久久精品日本一区二区免费| 亚洲精选在线观看| 91福利视频免费观看| 亚洲国色天香视频| 一个人免费观看在线视频www| 激情五月亚洲色图| 免费的一级黄色片| 看全免费的一级毛片| 亚洲一区二区三区无码影院| 亚洲国产免费综合| 亚洲精品国产美女久久久| 亚洲免费观看视频| 亚洲春色另类小说| 成全影视免费观看大全二| 国产成人亚洲精品无码AV大片| 免费一级成人毛片| 精品国产免费一区二区三区| 精品国产_亚洲人成在线高清| 日韩在线不卡免费视频一区| 亚洲av日韩av无码av| 欧洲精品免费一区二区三区| 免费在线人人电影网| 亚洲女久久久噜噜噜熟女| 久久精品无码专区免费东京热| 日本亚洲精品色婷婷在线影院| 天堂亚洲免费视频| 免费播放一区二区三区| 国产成人精品日本亚洲网址| 亚洲成?v人片天堂网无码| 久久亚洲免费视频| 国产精品亚洲AV三区| 五月天网站亚洲小说| 蜜桃精品免费久久久久影院| 国产免费AV片在线观看播放| 亚洲伦理一二三四| 亚洲午夜激情视频| 国产va免费精品观看精品|