Java批处理操作mysql

本文介绍使用Java通过JDBC API和PreparedStatement实现数据库批量插入的方法。通过示例代码展示如何高效地批量处理数据,适用于数据导入等场景。

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

转载
实现Java批量插入数据库数据,在javaeye中看到过几篇关于实现Java批量插入数据库数据,转载时没有找到,就自己写一下,也算是对自己学习过程中所遇到过的问题做一个总结。
一般关于批量向数据库插入数据都采用PreparedStatement、Statement…………也包括直接使用JDBC API、框架…………
也看到过几篇关于这些内容的总结,及大家的评论,以下为我总结的关于批量向数据库插入数据。

1,使用JDBC API实现配量插入数据:有篇文章介绍过关于JDBC API、Hibernate实现批量插入数据,采用JDBC API 方式实现随着数据的增长,速度更胜于Hibernate。当然,对于这个测试的准确我并不保证,但是我也会优先选用JDBC API方式实现(原因:简单、易学、相对于框架更通用,不会过时)。

2,采用PreparedStatement对象实现批量插入数据:PreparedStatement是真正的批处理命令,不是其他的伪批处理命令可以相比的(个人意见),它相对于其他的实现批量处理是非常的强大,比如字段不断改变,每次都要从文件从新读取就只能使用PreparedStatement对象来实现。再有就是存在即合理,既然PreparedStatement对象可以多次高效地执行预编译的语句,就一定有其原因(JDk源码没有分析过,和Statement实现的区别不了解)。

3,实现批量插入数据库数据


Java代码
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://" +
"localhost:3306/excel2mysql", "wanle", "wanle");

// 关闭事务自动提交
con.setAutoCommit(false);

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Long startTime = System.currentTimeMillis();

PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into test04 values (?,'中国')");
for (int i = 0; i < 10000; i++) {
pst.setInt(1, i);
// 把一个SQL命令加入命令列表
pst.addBatch();
}
// 执行批量更新
pst.executeBatch();
// 语句执行完毕,提交本事务
con.commit();

Long endTime = System.currentTimeMillis();
System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));

pst.close();
con.close();

Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://" + "localhost:3306/excel2mysql", "wanle", "wanle");
// 关闭事务自动提交
con.setAutoCommit(false);

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Long startTime = System.currentTimeMillis();

PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into test04 values (?,'中国')");
for (int i = 0; i < 10000; i++) {
pst.setInt(1, i);
// 把一个SQL命令加入命令列表
pst.addBatch();
}
// 执行批量更新
pst.executeBatch();
// 语句执行完毕,提交本事务
con.commit();

Long endTime = System.currentTimeMillis();
System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));

pst.close();
con.close();
插入10000条数据用时3141毫秒,对于我已经很理想了, 毕竟我们不会使用MySQL进行非常大型项目的开发,对于10000条数据3秒多点,已经可以了,我相信对于大家应该也足以应付了,我们不会每天都插入10000条吧,当然对于我的话如果有这样的需求,我不会选择MySQL。

以上所有内容均为对于我所学习使用过程中、实际项目开发中的总结,也应用于其中。对于批量插入,数据导入均采用这样的方式


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class Read {

private static Connection con=null;
private static PreparedStatement pstmt=null;

public static void writeDb(Record r){

try {
pstmt.setString(1, r.item1);
pstmt.setString(2, r.item2);
pstmt.setDouble(3, r.frequency);
pstmt.addBatch();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void readFile(){
try {
String s=null;
String[] split=new String[3];
FileReader fr = new FileReader("E:\\eclipse-SDK-3.5-win32\\eclipse\\workspace\\exp\\sina_entertaiment.txt");
BufferedReader bf=new BufferedReader(fr);

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/experiment?useUnicode=true&characterEncoding=utf-8","root","12345");
//con.createStatement();
pstmt=con.prepareStatement("insert into sina_entertaiment(item1,item2,frequency) values (?,?,?)");


while((s=bf.readLine())!=null){
// if(s.trim()=="")
// continue;
split=s.split("\t");
System.out.println(s);
writeDb(new Record(split));
}
pstmt.executeBatch();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e)
{
}
catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally
{
try {
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static void main(String[] args){
Read.readFile();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值