JDBC之常规插入,Statement和PreparedStatement批处理时间问题
JDBC之常规插入,Statement和PreparedStatement批处理时间问题
已经封装好的通用的批处理语句:代码语言:javascript代码运行次数:0运行复制import java.io.FileotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
i
JDBC之常规插入,Statement和PreparedStatement批处理时间问题
已经封装好的通用的批处理语句:
代码语言:javascript代码运行次数:0运行复制import java.io.FileotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* 是一个工具类:
* 作用:用于封装通用的获取连接、通用的增删改、通用的查询
* 版本:v0.0.0.1
* 方法:getConn();
* close();
* 问题:
* OCP:对扩展开放,对修改的关闭;
* 现在数据库myjdbc,该库名,修改代码;用户 名 密码要改,也需要修改代码.
* 升级:
* 需要读取到db.properties的文件的内容;
* 需要的技术:io
* Properties集合类;
*/
public class JDBCUtilTwo {
//成员变量;
static Properties prop=new Properties();
static String classame=null;
static String url=null;
static String uname=null;
static String pwd=null;
//1.静态代码块,加载数据库的驱动,此处是mysql驱动;
static {
try {
FileReader reader=new FileReader("db.properties");
prop.load(reader);
//getProperty():获取的从文件读取的key;
classame=prop.getProperty("classame");
url=prop.getProperty("url");
uname=prop.getProperty("uname");
pwd=prop.getProperty("pwd");
Class.forame(classame);
} catch (ClassotFoundException | FileotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//2.获取链接;获取一个连接对象;
public static Connection getConn(){
Connection connection = null;
try {
connection = DriverManager.getConnection(url, uname, pwd);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//.关闭对象,结果集对象、语句对象、链接对象;
public static void close(ResultSet rs, Statement st, Connection conn){
if(rs!=null) {
try {
();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null)
try {
();
} catch (SQLException e) {
e.printStackTrace();
}
if(conn!=null)
try {
();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
普通插入方式,耗时比较长:
代码语言:javascript代码运行次数:0运行复制/**
* 执行时间:2 9786毫秒
* @throws SQLException
*/
@Test
public void test021() throws SQLException {
Connection conn = JDBCUtilTwo.getConn();
//常规语句对象+普通方式
Statement st = ();
//1万条数据;
long startTime=();
for (int i = 5; i < 10005; i++) {
String sql="insert t_user(username,passworld)values('tom','tom')";
(sql);
}
();
();
long endTime=();
println("执行时间:"+(endTime-startTime)+"毫秒");
}
Statement方式添加批处理,时间还比较慢。
代码语言:javascript代码运行次数:0运行复制/**
* 使用批处理来进行操作;
* 执行时间:21 4194毫秒 2468毫秒
* @throws SQLException
*/
@Test
public void test022() throws SQLException {
Connection conn = JDBCUtilTwo.getConn();
//设置链接,自动提交事务为false。
conn.setAutoCommit(false);
//常规语句对象+普通方式
Statement st = ();
//1万条数据;
String sql="insert t_user(username,passworld)values('tom','tom')";
long startTime=();
for (int i = 1; i <10010 ; i++) {
//将sql语句,添加到批处理里面;
st.addBatch(sql);
//在某个时间节点,执行一次批处理;
if(i%2000==0) {
(); //执行批处理
(); //清空一下批处理;
}
}
(); //执行批处理
(); //清空一下批处理;
//提交事务;
connmit();
(null,st,conn);
long endTime=();
println("执行时间:"+(endTime-startTime)+"毫秒");
}
时间还比较慢:可以在配置文件,修改mysql批处理为true。
代码语言:javascript代码运行次数:0运行复制classame=jdbc.Driver
url=jdbc:mysql://localhost:06/myjdbc?rewriteBatchedStatements=true
uname=root
pwd=root
再修改事务自动提交为false,会大幅度提交效率。
预编译语句方式,批处理:
代码语言:javascript代码运行次数:0运行复制/**
* 执行时间:22 0170毫秒 1711毫秒
* @throws SQLException
*/
@Test
public void test02() throws SQLException {
Connection conn = JDBCUtilTwo.getConn();
String sql="insert t_user(username,passworld)values(?,?)";
PreparedStatement pst = conn.prepareStatement(sql);
long startTime=();
for (int i = 2; i < 10002; i++) {
pst.setString(1,"u"+i);
pst.setString(2,"p"+i);
//添加到批处理里面;
pst.addBatch();
if(i%2000==0){
p();
p();
}
}
p();
p();
(null,pst,conn);
long endTime=();
println("执行时间:"+(endTime-startTime)+"毫秒");
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:202-10-14,如有侵权请联系 cloudcommunity@tencent 删除连接事务数据库jdbc对象 #感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-28 20:30:34
上一篇:SpringMVC简介
推荐阅读
留言与评论(共有 6 条评论) |
本站网友 东方神韵 | 26分钟前 发表 |
passworld)values(? | |
本站网友 mp3合并 | 29分钟前 发表 |
06/myjdbc?rewriteBatchedStatements=true uname=root pwd=root再修改事务自动提交为false | |
本站网友 怎么辨别奶粉真假 | 7分钟前 发表 |
该库名 | |
本站网友 美发连锁加盟 | 11分钟前 发表 |
添加到批处理里面; st.addBatch(sql); //在某个时间节点 | |
本站网友 章辉 | 15分钟前 发表 |
mysql |