Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.24.0·
页面加载耗时 0.00 毫秒·物理内存 62.5MB ·虚拟内存 1302.1MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
批量处理允许将相关的SQL语句分组到批处理中,并通过对数据库的一次调用来提交它们,一次执行完成与数据库之间的交互。
一次向数据库发送多个SQL语句时,可以减少通信开销,从而提高性能。
以下是使用 Statement对象的批处理的典型步骤序列 -
以下代码片段提供了使用 Statement对象的批量更新示例:
// Create statement object Statement stmt = conn.createStatement(); // Set auto-commit to false conn.setAutoCommit(false); // Create SQL statement String SQL = "INSERT INTO Employees (id, first, last, age) " + "VALUES(200,'Ruby', 'Yang', 30)"; // Add above SQL statement in the batch. stmt.addBatch(SQL); // Create one more SQL statement String SQL = "INSERT INTO Employees (id, first, last, age) " + "VALUES(201,'Java', 'Lee', 35)"; // Add above SQL statement in the batch. stmt.addBatch(SQL); // Create one more SQL statement String SQL = "UPDATE Employees SET age = 35 " + "WHERE id = 100"; // Add above SQL statement in the batch. stmt.addBatch(SQL); // Create an int[] to hold returned values int[] count = stmt.executeBatch(); //Explicitly commit statements to apply changes conn.commit();
以下是使用 PrepareStatement对象进行批处理的典型步骤顺序:
以下代码段提供了使用 PreparedStatement 对象进行批量更新的示例:
// Create SQL statement String SQL = "INSERT INTO Employees (id, first, last, age) " + "VALUES(?, ?, ?, ?)"; // Create PrepareStatement object PreparedStatemen pstmt = conn.prepareStatement(SQL); //Set auto-commit to false conn.setAutoCommit(false); // Set the variables pstmt.setInt( 1, 400 ); pstmt.setString( 2, "JDBC" ); pstmt.setString( 3, "Li" ); pstmt.setInt( 4, 33 ); // Add it to the batch pstmt.addBatch(); // Set the variables pstmt.setInt( 1, 401 ); pstmt.setString( 2, "CSharp" ); pstmt.setString( 3, "Liang" ); pstmt.setInt( 4, 31 ); // Add it to the batch pstmt.addBatch(); //add more batches . . . . //Create an int[] to hold returned values int[] count = stmt.executeBatch(); //Explicitly commit statements to apply changes conn.commit();
PreparedStatement 对象可以使用输入和输出流来提供参数数据。能够将整个文件放入可以容纳大值的数据库列,例如 CLOB 和 BLOB 数据类型。有以下方法可用于流式传输数据:setAsciiStream() :此方法用于提供大的ASCII值。setCharacterStream() :此方法用于提供较大的UNICODE值。