import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectMysql2 {
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/t_user", "root", "123456");
con.setAutoCommit(false);//设为手动提交
stmt = con.createStatement();
for(int i = 0; i<1000; i++) {
stmt.addBatch("insert into user(name,pwd)values('"+i+"',666)");
}
stmt.executeBatch();
con.commit();//提交事务
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
stmt.close();//和下面需要分别关闭的原因是若前面的关闭抛出异常,后面的将不执行
}catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
批处理Batch 插入1000条数据的测试
最新推荐文章于 2025-02-12 15:52:37 发布