mysql jdbc batch_JDBC批处理(batch)

本文对比了使用JDBC进行数据库操作时,普通插入与批处理插入两种方式的性能差异。通过插入10万条记录的实验,展示了批处理能够显著提高数据插入效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一下是关于JDBC批处理的一个程序测试

Nobatch

package JDBCBatch;

import java.sql.*;

public class TestCommon {

static long startTime;

public static void main(String[] args) throws Exception {

Connection conn = getConnection();

PreparedStatement ps = null;

try {

startTime=System.nanoTime(); //获取开始时间

ps = conn

.prepareStatement("INSERT INTO batchtab values (?, ?)");

conn.setAutoCommit(false);

for (int n = 0; n < 10000; n++) {

Integer i = new Integer(n);

ps.setString(1, i.toString());

ps.setString(2, "value" + i.toString());

ps.executeUpdate();

}

conn.commit();

long endTime=System.nanoTime(); //获取结束时间

System.out.println("程序运行时间: "+(endTime-startTime)+"ns");

}catch (SQLException ex) {

System.out.println("SQLException: " + ex.getMessage());

System.out.println("SQLState: " + ex.getSQLState());

System.out.println("Message: " + ex.getMessage());

System.out.println("Vendor error code: " + ex.getErrorCode());

} catch (Exception e) {

e.printStackTrace();

System.err.println("Exception: " + e.getMessage());

} finally {

if (conn != null)

conn.close();

if (ps != null)

ps.close();

}

}

public static Connection getConnection() {

Connection con = null; //创建用于连接数据库的Connection对象

try {

Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动

con = DriverManager.getConnection("jdbc:mysql://localhost/hibernate",

"root","123");

} catch (Exception e) {

System.out.println("数据库连接失败" + e.getMessage());

}

return con; //返回所建立的数据库连接

}

}

下面是使用批处理的

package JDBCBatch;

import java.sql.*;

public class TestPreStatementBatch {

static long startTime;

public static void main(String[] args) throws Exception {

Connection conn = getConnection();

ResultSet rs = null;

PreparedStatement ps=null;

try {

startTime=System.nanoTime(); //获取开始时间

ps = conn.prepareStatement("INSERT INTO batchtab values (?, ?)");

conn.setAutoCommit(false);

ps.clearBatch();

for (int n=0; n<10000; n++) {

Integer i = new Integer(n);

ps.setString(1, i.toString());

ps.setString(2, "value" + i.toString());

ps.addBatch();

}

ps.executeBatch();

conn.commit();

long endTime=System.nanoTime(); //获取结束时间

//打印消耗时间

System.out.println("程序运行时间: "+(endTime-startTime)+"ns");

} catch (BatchUpdateException b) {

System.out.println("SQLException: " + b.getMessage());

System.out.println("SQLState: " + b.getSQLState());

System.out.println("Message: " + b.getMessage());

System.out.println("Vendor error code: " + b.getErrorCode());

System.out.print("Update counts: ");

} catch (SQLException ex) {

System.out.println("SQLException: " + ex.getMessage());

System.out.println("SQLState: " + ex.getSQLState());

System.out.println("Message: " + ex.getMessage());

System.out.println("Vendor error code: " + ex.getErrorCode());

} catch (Exception e) {

e.printStackTrace();

System.err.println("Exception: " + e.getMessage());

} finally {

if( conn != null )

conn.close();

if(ps !=null)

ps.close();

if(rs !=null)

rs.close();

}

}

public static Connection getConnection() {

Connection con = null; //创建用于连接数据库的Connection对象

try {

Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动

con = DriverManager.getConnection("jdbc:mysql://localhost/hibernate",

"root","123");

} catch (Exception e) {

System.out.println("数据库连接失败" + e.getMessage());

}

return con; //返回所建立的数据库连接

}

}

下面是插入10W条数据的测试

批处理:902111464ns

普通插入:1030343361ns

代码的主要区别:

这个是批处理的:

for (int n=0; n<10000; n++) {

Integer i = new Integer(n);

ps.setString(1, i.toString());

ps.setString(2, "value" + i.toString());

ps.addBatch();

}

ps.executeBatch();

这个是不使用批处理的:

for (int n = 0; n < 10000; n++) {

Integer i = new Integer(n);

ps.setString(1, i.toString());

ps.setString(2, "value" + i.toString());

ps.executeUpdate();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值