Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 (中文) » SQL Anywhere 服务器 - 编程 » JDBC 支持 » 使用 JDBC 访问数据

 

JDBC 批处理方法

PreparedStatement 类的 addBatch 方法用于执行成批插入(或宽插入)。以下是使用此方法的一些指导。

  1. INSERT 语句应该使用 Connection 类中的 prepareStatement 方法之一来预准备。

    // Build the INSERT statement
    String sqlStr = "INSERT INTO Departments " +
                "( DepartmentID, DepartmentName ) " +
                "VALUES ( ? , ? )";
    // Prepare the statement
    PreparedStatement stmt =
         con.prepareStatement( sqlStr );
  2. 应设置准备的插入语句的参数并对其进行批处理,如下所示:



    // loop to batch "n" sets of parameters
    for( i=0; i < n; i++ ) 
    {
        // "stmt" is the original prepared insert statement from step 1.
        stmt.setSomeType( 1, param_1 );
        stmt.setSomeType( 2, param_2 );
        .
        .
        .
        // There are "m" parameters in the statement.
        stmt.setSomeType( m , param_m );
    
        // Add the set of parameters to the batch and 
        // move to the next row of parameters.
        stmt.addBatch(); 
    }

    示例:

    for( i=0; i < 5; i++ ) 
    {    
        stmt.setInt( 1, idValue );
        stmt.setString( 2, name );
        stmt.addBatch();
    }
  3. 批处理必须用 PreparedStatement 类的 executeBatch 方法执行。

批处理中不支持 BLOB 参数。

使用 SQL Anywhere JDBC 驱动程序执行成批插入时,建议使用较小的列大小。建议不要使用成批插入方式向 long binary 或 long varchar 列中插入大的二进制或字符数据,这样可能会使性能下降。性能会下降的原因是 SQL Anywhere JDBC 驱动程序只有分配较大的内存大小才能保存成批插入的各行。在所有其它情况下,与逐个插入相比,成批插入会提供更佳的性能。