JDBC的批量处理数据
主要用到的方法有:
preparedStatement.executeBatch();//积攒的数据执行
preparedStatement.clearBatch();//积攒的清除掉
preparedStatement.addBatch();//这儿并不马上执行,积攒到一定数量之后,刷新执行
-----------------------------------------------------------------------------------------------
Test12 t=new Test12();
/*
* 批量处理数据JDBC语句,提高处理速度
* */
//插入数据
@Test
public void testBase() throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
String sql=null;
try {
connection=t.getConnection();
//开始事物取消默认提交
setAutoCommit(connection);
sql="insert into customer where values(?,?,?,?)";
preparedStatement=connection.prepareStatement(sql);
Date date=new Date(new java.util.Date().getTime());
long began=System.currentTimeMillis();
for(int i=0;i<100000;i++){
preparedStatement.setInt(1, i+1);
preparedStatement.setString(2, "name"+i);
preparedStatement.setString(3, "email"+1);
preparedStatement.setDate(4, date);
//preparedStatement.executeQuery();
//这儿并不马上执行,积攒到一定数量之后,刷新执行
preparedStatement.addBatch();
if((i+1)%300==0){
preparedStatement.executeBatch();//积攒的数据执行
preparedStatement.clearBatch();//积攒的清楚掉
}
}
//最后不是300的整数,再执行一次
if(1000000%300!=0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
long end=System.currentTimeMillis();
System.out.println(end-began);
//都成的话,提交事物
commit(connection);
} catch (Exception e) {
}finally {//回滚事物
rollbank(connection);
t.close(connection, preparedStatement, null);
}
}
//开始事物:取消默认提交
public void setAutoCommit(Connection connection){
if(connection!=null){
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//都成功提交事物
public void commit(Connection connection){
if(connection!=null){
try {
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//回滚事物
public void rollbank(Connection connection){
if(connection!=null){
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void TestSetTransactionTsolation(){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection=t.getConnection();
//设置不是自动提交
connection.setAutoCommit(false);
String sql1="update test set grade= grade+100 where flow_id=3";
t1.update(connection, sql1);
//都成功提交事物
connection.commit();
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
JDBC的批量处理数据的更多相关文章
- 使用JDBC批量保存数据(JdbcDaoSupport,JdbcTemplete)
最近做的一个项目中用到了Hibernate的,然后数据库批量插入数据的时候就使用到了hibernate的批处理,但是效率比较低,看网上说还有一些限制,要禁止二级缓存,还要多一个batch_size的配 ...
- 使用JDBC在MySQL数据库中快速批量插入数据
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢? 在JDBC编程接口中Statement 有两个方法特别值得注意: void addBatch ...
- JDBC批量插入数据优化,使用addBatch和executeBatch
JDBC批量插入数据优化,使用addBatch和executeBatch SQL的批量插入的问题,如果来个for循环,执行上万次,肯定会很慢,那么,如何去优化呢? 解决方案:用 preparedSta ...
- MySQL:JDBC批量插入数据的效率
平时使用mysql插入.查询数据都没有注意过效率,今天在for循环中使用JDBC插入1000条数据居然等待了一会儿 就来探索一下JDBC的批量插入语句对效率的提高 首先进行建表 create tabl ...
- 【实践】jdbc批量插入数据
参考文献:http://my.oschina.net/u/1452675/blog/203670 http://superjavason.iteye.com/blog/255423 /*测试批量写入数 ...
- JDBC(五)—— 批量插入数据
批量插入数据 @Test public void testInsert() throws Exception { Connection conn = null; PreparedStatement p ...
- Java 批量插入数据(Oracle)
//批量添加20000条数据用时8秒. try { String url = "jdbc:oracle:thin:@IP:1521:orcl"; // orcl为数据库的SI ...
- Hibernate批量处理数据
01.批量插入数据 步骤一.创建实体类,Dept和Emp /** * 员工类 * @author Administrator * */ public class Emp { private Integ ...
- 批量插入数据(基于Mybatis的实现-Oracle)
前言:做一个数据同步项目,要求:同步数据不丢失的情况下,提高插入性能. 项目DB框架:Mybatis.DataBase:Oracle. -------------------------------- ...
随机推荐
- update kernel
1,version 2,command First, verify the current kernel version: $ uname -r 2.6.32-358.el6.x86_64 Befor ...
- eclipse JAVA实现AES的加密和解密算法
import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.Secre ...
- DB2数据库管理最佳实践(1)
1.DB2 9的新特性 1)DB2 9.1:主要是增加了原生XML,表分区和表压缩功能.从构架上,总的进程模型和内存构架看,都和8没啥区别. DB2 中的 pureXML 支持为管理 XML 数据提供 ...
- views of postgresql user password and encrypted or unencrypted
password_encryption = onpostgres=# create user user1 with encrypted password 'user1';CREATE ROLEpost ...
- Ios(ipad iphone) 支持字体一览
Font Name : ThonburiFont Name : Snell RoundhandFont Name : Academy Engraved LETFont Name : AvenirFon ...
- ServiceController1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Python高频技巧总结[基础篇]
0. 概要说明 python应用最多的场景还是web快速开发.爬虫.自动化运维:简单网站.自动Fuzz脚本.收发邮件脚本.简单验证码识别脚本. 爬虫在开发过程中也有很多复用的过程,这里总结一下,以后也 ...
- 动画--过渡属性 transition-property
早期在Web中要实现动画效果,都是依赖于JavaScript或Flash来完成.但在CSS3中新增加了一个新的模块transition,它可以通过一些简单的CSS事件来触发元素的外观变化,让效果显得更 ...
- Java泛型01--任意数组中两元素交换
package com.zl.generic; /** * 交换“任意”数组 中两个元素 */ public class GenericSwapArray { public static void m ...
- 刚刚学了循环,1到n的求和与阶乘
//求和 int a = Convert.ToInt32(Console.ReadLine()); int c = 0; for (int b = 0; b <= a; b++) { c = c ...