JDBC批量处理数据

**
 * @类功能说明:
 * @作者:yunfaliu
 * @创建时间:2018年3月29日 上午9:33:43
 * @版本:V1.0
 */
public class BatchJDBc {

public static void main(String[] args) {
Connection conne = null;
Statement stm = null;
ResultSet rs = null;

  // 导入数据库Jar包
try {
// 加载驱动 放驱动类的全路径名称
Class.forName("com.mysql.jdbc.Driver");
// 获得数据库连接对象
conne = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/myschool", "root", "root");
int result = 0;
stm=conne.createStatement();
// 获得批量处理执行开始时间
Long start = System.currentTimeMillis();
// 关闭事务的自动提交
conne.setAutoCommit(false);

for (int i = 0; i < 1000; i++) {
// 写执行的sql的语句
String sql = "insert  into  student  (studentNO,loginPwd,studentName,sex) values ("
+ (100 + i) + ",'admin','张三','男')";
stm.addBatch(sql);
// 执行sql语句并返回结果

}
stm.executeBatch();
// 提交事务
conne.commit();
Long end = System.currentTimeMillis();
// 处理返回结果
System.out.println("添加100条数据总共耗时:" + (end - start));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// 如果出现sql执行异常就进行数据回滚
                            conne.rollback();
} finally {
// 释放资源
try {
if (rs != null) {
rs.close();
}

if (stm != null) {
stm.close();
}

if (conne != null) {
conne.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值