JDBC系列:(5)使用PreparedStatement进行批处理
2016-05-12 00:30
441 查看
序号 | 方法 | 作用 |
---|---|---|
1 | void addBatch(String sql) | 添加批处理 |
2 | int[] executeBatch() | 执行批处理 |
3 | void clearBatch() | 清空批处理 |
package com.rk.db.e_batch; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.rk.db.utils.JDBCUtil; public class Demo01 { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; try { // 获取连接 conn = JDBCUtil.getConnection(); //预编译的sql String sql = "INSERT INTO T_Persons(UserName,Pwd) VALUES(?,?)"; // 创建stmt pstmt = conn.prepareStatement(sql); long startTime = System.currentTimeMillis(); for(int i=0;i<976;i++) { // 设置参数 pstmt.setString(1, "Lucy"+i); pstmt.setString(2, "abc"+i); // 添加批处理 pstmt.addBatch(); if(i%100==0) { // 批量执行 pstmt.executeBatch(); // 清空批处理 pstmt.clearBatch(); } } // 批量执行 pstmt.executeBatch(); // 清空批处理 pstmt.clearBatch(); long endTime = System.currentTimeMillis(); System.out.println("Total Time: " + (endTime - startTime)); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtil.close(conn, pstmt, null); } } }
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- PreparedStatement中in子句的处理
- JDBC 数据库常用连接 链接字符串
- JDBC连接Access数据库的几种方式介绍
- JDBC程序更新数据库中记录的方法
- JDBC 程序的常见错误及调试方法
- 在Java的JDBC使用中设置事务回滚的保存点的方法
- Java中使用JDBC操作数据库简单实例
- Java加载JDBC驱动程序实例详解
- JSP使用JDBC完成动态验证及采用MVC完成数据查询的方法
- JSP基于JDBC的数据库连接类实例
- JSP中使用JDBC访问SQL Server 2008数据库示例
- jsp+jdbc实现连接数据库的方法
- 解析jdbc处理oracle的clob字段的详解
- JDBC数据库的使用操作总结
- jdbc操作mysql数据库实例
- JSP使用JDBC连接MYSQL数据库的方法
- Java开发Oracle数据库连接JDBC Thin Driver 的三种方法
- java使用jdbc操作数据库示例分享
- JDBC常见问答