[java]代码库
package mysql3;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestBatch1 {
private static Connection con = null;
private static ResultSet result=null;
private static Statement statement =null;
public static void main(String[] args) {
//1.加载数据库驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("加载失败!");
e.printStackTrace();
}
System.out.println("加载成功!");
//2.连接数据库
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_library", "root", "123");
} catch (SQLException e) {
System.out.println("连接数据库失败!");
e.printStackTrace();
}
System.out.println("成功连接数据库!");
//3.处理数据库
try {
/*
* insert into person(name, age) values('李四',16);
*/
statement=con.createStatement();
long starttime=System.currentTimeMillis();
//循环单条执行
for(int i=0;i<1000;i++){
int i1 = statement.executeUpdate("insert into person(name, age) values('李四',16)");
}
long endtime=System.currentTimeMillis();
System.out.println(endtime-starttime);
} catch (SQLException e) {
e.printStackTrace();
}
//4.释放数据库的资源
finally{
try {
if(result!=null)
result.close();
if(statement!=null)
statement.close();
if(con!=null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("执行成功!");
}
}