主要用到的方法有:

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的批量处理数据的更多相关文章

  1. 使用JDBC批量保存数据(JdbcDaoSupport,JdbcTemplete)

    最近做的一个项目中用到了Hibernate的,然后数据库批量插入数据的时候就使用到了hibernate的批处理,但是效率比较低,看网上说还有一些限制,要禁止二级缓存,还要多一个batch_size的配 ...

  2. 使用JDBC在MySQL数据库中快速批量插入数据

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(10W+),如何提高效率呢? 在JDBC编程接口中Statement 有两个方法特别值得注意: void addBatch ...

  3. JDBC批量插入数据优化,使用addBatch和executeBatch

    JDBC批量插入数据优化,使用addBatch和executeBatch SQL的批量插入的问题,如果来个for循环,执行上万次,肯定会很慢,那么,如何去优化呢? 解决方案:用 preparedSta ...

  4. MySQL:JDBC批量插入数据的效率

    平时使用mysql插入.查询数据都没有注意过效率,今天在for循环中使用JDBC插入1000条数据居然等待了一会儿 就来探索一下JDBC的批量插入语句对效率的提高 首先进行建表 create tabl ...

  5. 【实践】jdbc批量插入数据

    参考文献:http://my.oschina.net/u/1452675/blog/203670 http://superjavason.iteye.com/blog/255423 /*测试批量写入数 ...

  6. JDBC(五)—— 批量插入数据

    批量插入数据 @Test public void testInsert() throws Exception { Connection conn = null; PreparedStatement p ...

  7. Java 批量插入数据(Oracle)

    //批量添加20000条数据用时8秒. try {    String url = "jdbc:oracle:thin:@IP:1521:orcl"; // orcl为数据库的SI ...

  8. Hibernate批量处理数据

    01.批量插入数据 步骤一.创建实体类,Dept和Emp /** * 员工类 * @author Administrator * */ public class Emp { private Integ ...

  9. 批量插入数据(基于Mybatis的实现-Oracle)

    前言:做一个数据同步项目,要求:同步数据不丢失的情况下,提高插入性能. 项目DB框架:Mybatis.DataBase:Oracle. -------------------------------- ...

随机推荐

  1. ASP.NET页面传值之Server.Transfer 和Response.Direct

    先看实例: B.apsx:         public string TextBox1Text         {             get             {             ...

  2. java io读书笔记(8)FileInputStream/FileOutputStream的应用

    转自:http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html 这是一对继承于InputStream和OutputStream的类,用 ...

  3. UML: 对象图

    摘自http://www.umlonline.org/school/thread-33-1-1.html Line表示类,line为Line的对象,下划线表明为对象,一般对象图用不到. 除了静态方法, ...

  4. Spring之我见

    Spring 是什么(1) •Spring 是一个开源框架. •Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. • ...

  5. css3颜色渐变

    从上到下的线性渐变: #grad {  background: -webkit-linear-gradient(red, blue); /* Safari 5.1 - 6.0 */  backgrou ...

  6. [Ubuntu] Ubuntu14.04 64bit 编译安装nginx1.7+php5.4+mysql5.6

    我的操作系统是Ubuntu14.04,其它linux系统的操作流程类似. 主要安装的软件是nginx1.7+php5.4+mysql5.6 1. 创建必要目录 sudo mkdir ~/setup s ...

  7. centos 6 7更改主机名 命令添加ip

    6:vi /etc/sysconfig/network 中hostname =主机名 vi /etc/hosts  添加127.0.0.1相应的主机名 7: hostnamectl set-hostn ...

  8. Respond.js – 让 IE6-8 支持 CSS3 Media Query

    Respond.js 是一个快速.轻量的 polyfill,用于为 IE6-8 以及其它不支持 CSS3 Media Queries 的浏览器提供媒体查询的 min-width 和 max-width ...

  9. eclipse字体颜色设置

    修改编码:window-->perference--->General--> Configure.--> Configure.-->workspace修改编辑背景色:wi ...

  10. scala的继承

    package com.test.scala.test /** * 模拟java的继承,扩展类 */ abstract class ExtendClass(val des:String) { def ...