日期:2014-05-20 浏览次数:20908 次
package cn.dzr.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import cn.dzr.jdbc.utils.JDBCUtils;
public class Test2
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
long end;
for(int i=0;i<1000;i++)
create(i);
end = System.currentTimeMillis();
System.out.println("外部使用for循环所消耗 时间: " + (end - start));
start = System.currentTimeMillis();
create2();
end = System.currentTimeMillis();
System.out.println("内部for循环执行一千次所消耗时间: " + (end - start));
start = System.currentTimeMillis();
create3();
end = System.currentTimeMillis();
System.out.println("内部批处理执行一千次所消耗时间: " + (end - start));
}
private static void create(int i)
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
conn = JDBCUtils.getConnection();
String sql = "insert into myuser (name,money) values(?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "fly"+i);
ps.setDouble(2, 1000+i*10);
ps.executeUpdate();
}
catch(SQLException e)
{
System.out.println("error");
}
finally
{
JDBCUtils.free(rs, ps, conn);
}
}
private static void create2()
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
conn = JDBCUtils.getConnection();
String sql = "insert into myuser (name,money) values(?,?)";
ps = conn.prepareStatement(sql);
for(int i=1000;i<2000;i++)
{
ps.setString(1, "hlong"+i);
ps.setDouble(2, 1000+i*10);
ps.executeUpdate();
}
}
catch(SQLException e)
{
System.out.println("error");
}
finally
{
JDBCUtils.free(rs, ps, conn);
}
}
private static void create3()
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;