标签:style color os io 使用 java ar for 数据
二者异同:
代码的可读性和可维护性.
PreparedStatement 能最大可能提高性能:PreparedStatement 可以防止 SQL 注入 。
代码:
package com.atguigu.java;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import org.junit.Test;
//批量操作:主要指的是批量插入
//分别使用PreparedStatement和Statement向Oracle数据库的某个表中插入100000条数据,
//对比二者的效率
public class TestJDBC1 {
// 使用PreparedStatement执行插入操作2,结合addBatch() executeBatch() clearBatch()
//结果显示:插入10万条数据花费时间不到1秒 ,推荐使用此方法,
@Test
public void test4() {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
long start = System.currentTimeMillis();
String sql = "insert into dept values(?,?)";
ps = conn.prepareStatement(sql);
for (int i = 0; i < 100000; i++) {
ps.setInt(1, i + 1);
ps.setString(2, "dept_" + (i + 1));
//1."攒sql"
ps.addBatch();
//2.执行
if((i + 1) % 250 == 0){//利用统计学可以找到一个效率最高的峰值,这里我们选用250能被100000整除
ps.executeBatch();
//3.清空
ps.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));// 47052-801
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(null, ps, conn);
}
}
// 使用Statement执行插入操作2,结合addBatch() executeBatch() clearBatch()
//结果发现效率并没有提升
@Test
public void test3() {
Connection conn = null;
Statement st = null;
try {
conn = JDBCUtils.getConnection();
long start = System.currentTimeMillis();
st = conn.createStatement();
for (int i = 0; i < 100000; i++) {
String sql = "insert into dept values(" + (i + 1) + ",'dept_"
+ (i + 1) + "')";
// 1.“攒”sql
st.addBatch(sql);
if ((i + 1) % 250 == 0) {
//2.
st.executeBatch();
//3.
st.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));// 98278-98957
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null, st, conn);
}
}
// 使用PreparedStatement执行插入操作1:明显能提高效率,但是还能改进
@Test
public void test2() {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
long start = System.currentTimeMillis();
String sql = "insert into dept values(?,?)";
ps = conn.prepareStatement(sql);
for (int i = 0; i < 100000; i++) {
ps.setInt(1, i + 1);
ps.setString(2, "dept_" + (i + 1));
ps.execute();
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));// 47052
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(null, ps, conn);
}
}
// 使用Statement执行插入操作1
//发现效率很低,原因是每次插入时都要对sql语句进行语法校验,然后执行每一条SQL语句
@Test
public void test1() {
Connection conn = null;
Statement st = null;
try {
conn = JDBCUtils.getConnection();
long start = System.currentTimeMillis();
st = conn.createStatement();
for (int i = 0; i < 100000; i++) {
String sql = "insert into dept values(" + (i + 1) + ",'dept_"
+ (i + 1) + "')";
st.execute(sql);
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));// 98278
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null, st, conn);
}
}
@Test
public void test0() throws Exception {
Connection conn = JDBCUtils.getConnection();
System.out.println(conn);
}
}
PreparedStatement vs Statement
标签:style color os io 使用 java ar for 数据
原文地址:http://blog.csdn.net/u010834071/article/details/39122173