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

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - 编程 » SQL Anywhere 数据访问 API » SQL Anywhere JDBC 驱动程序 » 使用 JDBC 访问数据

 

使用预准备语句进行宽插入

PreparedStatement.addBatch() 方法对于执行成批插入(或大范围插入)十分有用。以下是使用此方法的一些指导。

  1. 应使用 Connection.prepareStatement() 方法之一准备 INSERT 语句。

    // 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++ ) 
    {
        // Note "stmt" is the original prepared insert statement from step 1.
        stmt.setSomeType( 1, param_1 );
        stmt.setSomeType( 2, param_2 );
        .
        .
        .
        // Note that 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.executeUpdate() 方法执行批处理。

应注意,系统仅支持 PreparedStatement.addBatch() 方法,且必须调用 PreparedStatement.executeUpdate() 方法才能执行批处理。不支持语句对象的任何批处理方法(即 Statement.addBatch()、Statement.clearBatch()、Statement.executeBatch()),因为这些方法完全是可选项,而且并非十分有用。对于此类静态批处理,最好是在单个字符串上调用 Statement.execute() 或 Statement.executeQuery() 方法(批处理语句被包装在 BEGIN...END 内)。

注意
  • 批处理中不支持 BLOB 参数。

  • 虽然支持字符串/二进制参数,但字符串/二进制参数的大小是一个问题。缺省情况下,字符串/二进制参数的大小被限制为 255 个字符或 510 个字节。限制的原因是缘于基础 ODBC 协议,这里不加以讨论。有关进一步的信息,最好是查看关于传递 ODBC 中的参数数组的文档。但是,如果应用程序需要在一个批内传递较大的字符串或二进制参数,则已提供了另一种方法 - setBatchStringSize,以增加字符串/二进制参数的大小。请注意,必须在首次 addBatch() 调用之前调用此方法。如果在首次 addBatch() 方法调用之后调用此方法,则将忽略新的大小设置。因此,调用此方法更改字符串/二进制参数的大小时,应用程序需要事先知道该参数的最大字符串或二进制值将是多少。

要使用 setBatchStringSize 方法,必须修改上述 "代码",如下所示:

// You need to cast "stmt" to an IPreparedStatment object 
// to change the size of string/binary parameters.
ianywhere.ml.jdbcodbc.IPreparedStatement _stmt =
    (ianywhere.ml.jdbcodbc.IPreparedStatement)stmt;

// Now, for example, change the size of string parameter 4 
// from the default 255 characters to 300 characters.
// Note that string parameters are measured in "characters".
_stmt.setBatchStringSize( 4, 300 ); 

// Change the size of binary parameter 6 
// from the default 510 bytes to 750 bytes.
// Note that binary parameters are measured in "bytes".
_stmt.setBatchStringSize( 6, 750 ); 

// loop to batch "n" sets of parameters 
// where n should not be too large
for( i=0; i < n; i++ )
{
    // stmt is the prepared insert statement from step 1
    stmt.setSomeType( 1, param_1 ); 
    stmt.setSomeType( 2, param_2 );
    .
    .
    .
    // Note that 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(); 
}

修改参数的最大字符串/二进制大小时应非常小心。如果将最大值设置得过高,则额外的内存分配开销可能会抵销通过使用批处理所获得的任何性能。此外,应用程序还可能不会事先知道某个特定参数的最大字符串或二进制值是多少。因此,建议不要更改某个参数的最大字符串或二进制大小,而是一直使用批处理方法,直到遇到大于当前/缺省的最大值的字符串或二进制大小。那时,应用程序会调用 executeBatch() 来执行当前批处理的参数,然后调用并执行常规设置和 executeUpdate() 方法,直到较大的字符串/二进制参数已处理,接着在遇到较小的字符串/二进制参数时切换回批处理模式。