2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > JDBC学习(七 批处理操作)

JDBC学习(七 批处理操作)

时间:2019-11-20 19:25:24

相关推荐

JDBC学习(七 批处理操作)

一、批量操作(batch)

当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率.

JDBC的批量处理语句包括下面两个方法:

addBatch(String sql):添加需要批量处理的SQL语句或是参数;

executeBatch();执行批量处理语句;

通常我们会遇到两种批量执行SQL语句的情况:

多条SQL语句的批量处理; :Statement

一个SQL语句的批量传参; :PreparedStatement

需求:同时向t_student表,插入1000条数据,在JDBC中,MySQL不支持批量操作。

-------------------------------------------------------------------------

Statement 批处理 : 一次性可以执行多条sql语句,需要编译多次。

应用场景:系统初始化 (创建表,创建数据等)

添加sql语句,st.addBatch(sql) --添加sql语句

批量处理sql语句,int[] st.executeBatch()

清除缓存: st.clearBatch();

-------------------------------------------------------------------------

PreparedStatement 批处理 : 执行一条sql语句,编译一次,执行sql语句的参数不同。

应用场景:表数据初始化

添加批量参数:psmt.addBatch() --添加实际参数,执行之前,需要执行psmt.setXxx()设置实际参数

执行批处理:int[] psmt.executeBatch()

清除缓存:pstm.clearBatch();

1.使用Statement完成,没有批处理

代码演示:

public void saveByStatement() {// 声明资源对象Connection conn = null;Statement stmt = null;// 贾琏欲执事try {// 1.加载注册驱动Class.forName("com.mysql.jdbc.Driver");// 2.获取连接对象conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "rootroot");// 3.获取语句对象stmt = conn.createStatement();// 循环5000次long begin = System.currentTimeMillis();for (int i = 0; i < 1000; i++) {String sql = "insert into t_student (name,age) values('a'," + i + ")";stmt.executeUpdate(sql);}long end = System.currentTimeMillis();System.out.println(end - begin);} catch (Exception e) {e.printStackTrace();} finally {// 5.释放资源try {if (stmt != null) {stmt.close();}if (conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}}

代码运行结果:

// InnoDB:2263ms// MyISAM:300ms

2.使用Statement完成,使用批处理

代码演示:

@Testpublic void BatchsaveByStatement() {// 声明资源对象Connection conn = null;Statement stmt = null;// 贾琏欲执事try {// 1.加载注册驱动Class.forName("com.mysql.jdbc.Driver");// 2.获取连接对象conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "rootroot");// 3.获取语句对象stmt = conn.createStatement();// 循环5000次long begin = System.currentTimeMillis();for (int i = 0; i < 1000; i++) {String sql = "insert into t_student (name,age) values('a'," + i + ")";// 将SQL添加到批处理中stmt.addBatch(sql);if (i % 200 == 0) {stmt.executeBatch();// 执行批量操作stmt.clearBatch();// 清除批处理}}long end = System.currentTimeMillis();System.out.println(end - begin);} catch (Exception e) {e.printStackTrace();} finally {// 5.释放资源try {if (stmt != null) {stmt.close();}if (conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}}

代码运行结果:

// InnoDB:1990ms// MyISAM:387ms

3.使用PreparedStatement完成,没有批处理

代码演示:

public void saveByPreparedStatement() {// 声明资源对象Connection conn = null;PreparedStatement ps = null;// 贾琏欲执事try {// 1.加载注册驱动Class.forName("com.mysql.jdbc.Driver");// 2.获取连接对象conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "rootroot");// 3.获取语句对象String sql = "insert into t_student (name,age) values('a',?)";ps = conn.prepareStatement(sql);// 循环5000次long begin = System.currentTimeMillis();for (int i = 0; i < 1000; i++) {ps.setInt(1, i);ps.executeUpdate();}long end = System.currentTimeMillis();System.out.println(end - begin);} catch (Exception e) {e.printStackTrace();} finally {// 5.释放资源try {if (ps != null) {ps.close();}if (conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}}

代码运行结果:

// InnoDB:1793ms// MyISAM:277ms

4.使用PreparedStatement完成,使用批处理

代码演示:

public void BatchsaveByByPreparedStatement() {// 声明资源对象Connection conn = null;PreparedStatement ps = null;// 贾琏欲执事try {// 1.加载注册驱动Class.forName("com.mysql.jdbc.Driver");// 2.获取连接对象conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "rootroot");// 3.获取语句对象String sql = "insert into t_student (name,age) values('a',?)";ps = conn.prepareStatement(sql);// 循环5000次long begin = System.currentTimeMillis();for (int i = 0; i < 1000; i++) {ps.setInt(1, i);ps.addBatch();if (i % 200 == 0) {ps.executeBatch();ps.clearBatch();ps.clearParameters(); // 清除占位符操作}}long end = System.currentTimeMillis();System.out.println(end - begin);} catch (Exception e) {e.printStackTrace();} finally {// 5.释放资源try {if (ps != null) {ps.close();}if (conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}}

代码运行结果:

// InnoDB:1793ms// MyISAM:277ms

二、结论

MySQL服务器既不支持PreparedStatement的性能优化,也不支持JDBC中的批量操作.但是,在新的JDBC驱动中,我们可以通过设置参数来优化。

url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true

5.使用PreparedStatement完成,使用批处理,rewriteBatchedStatements=true

代码演示:

public void BatchsaveByByPreparedStatement2() {// 声明资源对象Connection conn = null;PreparedStatement ps = null;// 贾琏欲执事try {// 1.加载注册驱动Class.forName("com.mysql.jdbc.Driver");// 2.获取连接对象conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true", "root", "rootroot");// 3.获取语句对象String sql = "insert into t_student (name,age) values('a',?)";ps = conn.prepareStatement(sql);// 循环5000次long begin = System.currentTimeMillis();for (int i = 0; i < 1000; i++) {ps.setInt(1, i);ps.addBatch();if (i % 200 == 0) {ps.executeBatch();ps.clearBatch();ps.clearParameters(); // 清除占位符操作}}long end = System.currentTimeMillis();System.out.println(end - begin);} catch (Exception e) {e.printStackTrace();} finally {// 5.释放资源try {if (ps != null) {ps.close();}if (conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}}

代码运行结果:

// InnoDB:51ms// MyISAM:29ms

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。