**
* @类功能说明:
* @作者: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();
}
}
}
}