Scripts 学盟
标题:
java 批量插入数据
[打印本页]
作者:
那个谁
时间:
2011-6-27 15:14:05
标题:
java 批量插入数据
package jdbc2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Mysql {
public static void main(String[] args) {
test_mysql();
//test_mysql_batch();
//test_oracle();
//test_oracle_batch();
}
/**
* mysql非批量插入10万条记录
* 第1次:17437 ms
* 第2次:17422 ms
* 第3次:17046 ms
*/
public static void test_mysql(){
String url="jdbc:mysql://192.168.10.139:3306/test";
String userName="root";
String password="1234";
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, userName, password);
conn.setAutoCommit(false);
String sql = "insert into t_user(id,uname) values(?,?)";
PreparedStatement prest = conn.prepareStatement(sql);
long a=System.currentTimeMillis();
for(int x = 0; x < 100000; x++){
prest.setInt(1, x);
prest.setString(2, "张三");
prest.execute();
}
conn.commit();
long b=System.currentTimeMillis();
System.out.println("MySql非批量插入10万条记录用时"+ (b-a)+" ms");
} catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* mysql批量插入10万条记录
* 第1次:17437 ms
* 第2次:17562 ms
* 第3次:17140 ms
*/
public static void test_mysql_batch(){
String url="jdbc:mysql://192.168.10.139:3306/test";
String userName="root";
String password="1234";
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, userName, password);
conn.setAutoCommit(false);
String sql = "insert into t_user(id,uname) values(?,?)";
PreparedStatement prest = conn.prepareStatement(sql);
long a=System.currentTimeMillis();
for(int x = 0; x < 100000; x++){
prest.setInt(1, x);
prest.setString(2, "张三");
prest.addBatch();
}
prest.executeBatch();
conn.commit();
long b=System.currentTimeMillis();
System.out.println("MySql批量插入10万条记录用时"+ (b-a)+" ms");
} catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* oracle非批量插入10万条记录
* 第1次:22391 ms
* 第2次:22297 ms
* 第3次:22703 ms
*/
public static void test_oracle(){
String url="jdbc:oracle:thin:@192.168.10.139:1521:orcl";
String userName="scott";
String password="tiger";
Connection conn=null;
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection(url, userName, password);
conn.setAutoCommit(false);
String sql = "insert into t_user(id,uname) values(?,?)";
PreparedStatement prest = conn.prepareStatement(sql);
long a=System.currentTimeMillis();
for(int x = 0; x < 100000; x++){
prest.setInt(1, x);
prest.setString(2, "张三");
prest.execute();
}
conn.commit();
long b=System.currentTimeMillis();
System.out.println("Oracle非批量插入10万记录用时"+ (b-a)+" ms");
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* oracle批量插入10万条记录
* 第1次:360 ms
* 第2次:328 ms
* 第3次:359 ms
*/
public static void test_oracle_batch(){
String url="jdbc:oracle:thin:@192.168.10.139:1521:orcl";
String userName="scott";
String password="tiger";
Connection conn=null;
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection(url, userName, password);
conn.setAutoCommit(false);
String sql = "insert into t_user(id,uname) values(?,?)";
PreparedStatement prest = conn.prepareStatement(sql);
long a=System.currentTimeMillis();
for(int x = 0; x < 100000; x++){
prest.setInt(1, x);
prest.setString(2, "张三");
prest.addBatch();
}
prest.executeBatch();
conn.commit();
long b=System.currentTimeMillis();
System.out.println("Oracle批量插入10万记录用时"+ (b-a)+" ms");
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
复制代码
作者:
混混@普宁.中国
时间:
2011-6-27 16:23:48
executeBatch 这么牛X
欢迎光临 Scripts 学盟 (http://www.iscripts.org/)
Powered by Discuz! X2