PreparedStatement.addBatch() メソッドはバッチ (またはワイド) 挿入を実行するときに便利です。次に、このメソッドの使用に関するガイドラインの一部を示します。
INSERT 文は、Connection.prepareStatement() メソッドの 1 つを使用して準備します。
// Build the INSERT statement String sqlStr = "INSERT INTO Departments " + "( DepartmentID, DepartmentName ) " + "VALUES ( ? , ? )"; // Prepare the statement PreparedStatement stmt = con.prepareStatement( sqlStr ); |
次のようにして、準備された INSERT 文のパラメータを設定してバッチ処理します。
// 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(); } |
次に、PreparedStatement.executeUpdate() メソッドを使用してバッチを実行します。
サポートされているメソッドは PreparedStatement.addBatch() メソッドだけであるため、バッチを実行するためには PreparedStatement.executeUpdate() メソッドを呼び出す必要があります。Statement.addBatch()、Statement.clearBatch()、Statement.executeBatch() などの Statement オブジェクトのバッチ・メソッドは、どれもサポートされていません。これらのメソッドは完全にオプションのメソッドであり、あまり有用でないためです。このような静的なバッチの場合は、バッチ文を BEGIN...END に含め、単一の文字列から Statement.execute() または Statement.executeQuery() を呼び出すのが最適です。
BLOB パラメータはバッチでサポートされていません。
String パラメータと Binary パラメータはサポートされていますが、パラメータのサイズが問題になります。デフォルトでは、String パラメータの最大文字数は 255 文字、Binary パラメータの最大サイズは 510 バイトです。この制限は ODBC プロトコルに由来しているため、ここでの説明は控えます。詳細については、ODBC でパラメータ配列の受け渡しに関するマニュアルを参照してください。ただし、アプリケーションが制限サイズ以上の大きな String パラメータまたは Binary パラメータをバッチ内で渡す必要がある場合に備えて、String パラメータまたは Binary パラメータのサイズを大きくする setBatchStringSize メソッドが用意されています。このメソッドは、最初の addBatch() の呼び出しの前に呼び出す必要があります。最初の addBatch() を呼び出した後でこのメソッドを呼び出した場合は、新しいサイズ設定は無視されます。このため、このメソッドを呼び出して String パラメータまたは Binary パラメータのサイズを変更するときは、パラメータの最大文字列値または最大バイナリ値についてアプリケーションが事前に認識しておくことが必要です。
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() を呼び出し、バッチ処理中のパラメータを実行します。次に、通常の set メソッドと executeUpdate() メソッドを呼び出して、サイズの大きな String パラメータまたは Binary パラメータが処理されるまで実行し、サイズの小さな String パラメータまたは Binary パラメータが出現したらバッチ・モードに戻します。
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |