import org.junit.Test;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class JdbcTest {
@Test
public void testPreparedStatement() throws Exception {
long start=System.currentTimeMillis();
String driveclass="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/bigtable";
String username="root";
String password="root";
Class.forName(driveclass);
java.sql.Connection conn= DriverManager.getConnection(url,username,password);
conn.setAutoCommit(false);
//创建语句
String sql="insert into persion(name,age) values(?,?)";
PreparedStatement ppst= (PreparedStatement) conn.prepareStatement(sql);
for(int i=0;i<10000;i++){
ppst.setString(1,"tom"+i);
ppst.setInt(2,i%100);
ppst.addBatch();
if(i%2000==0){
ppst.addBatch();
}
}
ppst.executeBatch();
conn.commit();
ppst.close();
conn.close();
System.out.println(System.currentTimeMillis()-start);
}
public void testStatement() throws Exception {
long start=System.currentTimeMillis();
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/bigtable";
String username="root";
String password="root";
//取得连接
Class.forName(driverClass);
java.sql.Connection conn= DriverManager.getConnection(url,username,password);
conn.setAutoCommit(false);
//进行表的操作
Statement st=conn.createStatement();
for(int i=0;i<10000;i++){
String sql="insert into persion(name,age) values(tom"+i+","+i+")";
st.execute(sql);
}
conn.commit();
st.close();
conn.close();
System.out.println(System.currentTimeMillis()-start);
}
}
初步的JDBC学习。