java 批量执行 sql_JDBC批量执行SQL

批量执行SQL,效率比较高,但执行SQL太多,谨防内存溢出。

代码案例一:

package com.what21.jdbc.demo05;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

public class JDBCDemo {

/**

* @param args

*/

public static void main(String[] args) {

// 1. 创建连接

Connection connection = null;

try {

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo","root","123124");

} catch (Exception e) {

e.printStackTrace();

}

Statement stat = null;

try {

// 2. 创建Statement

stat = connection.createStatement();

// 3. 执行SQL

String sql = "insert into users(name,email,phone,mobile) value('name','email','phone','mobile')";

String sql2 = "insert into users(name,email,phone,mobile) value('name2','email2','phone','mobile')";

stat.addBatch(sql);

stat.addBatch(sql2);

int[] results = stat.executeBatch();

for(int result : results){

System.out.println(result);

}

stat.clearBatch();

} catch (SQLException e) {

e.printStackTrace();

}

// 5. 关闭连接

if(stat!=null){

try {

stat.close();

} catch (SQLException e) {

e.printStackTrace();

}

stat=null;

}

if(connection!=null){

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

connection=null;

}

}

}

代码案例二:

package com.what21.jdbc.demo05;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class JDBCDemo2 {

/**

* @param args

*/

public static void main(String[] args) {

// 1. 创建连接

Connection connection = null;

try {

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo","root","123124");

} catch (Exception e) {

e.printStackTrace();

}

PreparedStatement pstat = null;

try {

String sql = "insert into users(name,email,phone,mobile) value(?,?,?,?)";

// 2. 创建PreparedStatement

pstat = connection.prepareStatement(sql);

pstat.setString(1, "_name");

pstat.setString(2, "_email");

pstat.setString(3, "_phone");

pstat.setString(4, "_mobile");

pstat.addBatch();

pstat.setString(1, "_name2");

pstat.setString(2, "_email2");

pstat.setString(3, "_phone2");

pstat.setString(4, "_mobile2");

pstat.addBatch();

// 3. 执行

int[] results = pstat.executeBatch();

for(int result : results){

System.out.println(result);

}

// 4. 处理返回结果(这里无结果)

} catch (SQLException e) {

e.printStackTrace();

}

// 5. 关闭连接

if(pstat!=null){

try {

pstat.close();

} catch (SQLException e) {

e.printStackTrace();

}

pstat=null;

}

if(connection!=null){

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

connection=null;

}

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值