package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* 批处理时候,最好使用statement,因为preparestatement的的编译空间有限,数据过于庞大时会发生异常
*/
public class Demo_main {
public static void main(String[] args){
Statement s = null;
Connection c = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//建立连接
c = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc的使用","root","root");
//批处理时关闭jdbc的自动提交
c.setAutoCommit(false);
//执行SQL语句
long start = System.currentTimeMillis();
s = c.createStatement();
//插入两万行数据
for(int i=0;i<2000;i++) {
s.addBatch("insert into student (name,age) values ('张三"+i+"',1)");//里面写执行语句
}
long end = System.currentTimeMillis();
s.executeBatch();
//手动提交
c.commit();
System.out.println("开始时间:"+start);
System.out.println("结束时间:"+end);
System.out.println("执行时间"+(end-start));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭
try {
if(s != null) {
s.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(c != null) {
c.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}