原始批处理添加:
package com.jdbc.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class 原始批处理添加 {
public static void main(String[] args) throws Exception{
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,"root","douqiyuan");
PreparedStatement ps= con.prepareStatement("");
for(int i = 0;i <100;i++) {
String sql = "insert into test2(deptno,dname,loc) values("+i+",'dept_"+i+"','HZ') ";
ps.executeUpdate(sql);
}
if(ps != null) {
ps.close();
}
if(con != null) {
con.close();
}
}
}
预编译实现批处理:
package com.jdbc.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class 预编译实现批处理 {
public static void main(String[] args) throws Exception{
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String sql = "insert into test2(deptno,dname,loc) values(?,?,?)";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,"root","douqiyuan");
PreparedStatement ps= con.prepareStatement(sql);
for(int i =1;i <= 100;i++) {
ps.setInt(1,i);
ps.setString(2,"dept_"+i);
ps.setString(3,"杭州");
ps.addBatch();
}
ps.executeBatch();
if(ps != null) {
ps.close();
}
if(con != null) {
con.close();
}
}
}