jdbc插入百万数据
我使用以下代码插入5W数据要2分钟,100W就得40分钟左右,大家有没有更好的方法,提高速度。。。。
表T有70个字段。
conn = getConnection();
conn.setAutoCommit(false);
stat = conn.createStatement();
String sql = "insert T .......";
for(int i=0;i<50000;i++){
stat.addBatch(sql);
}
stat.executeBatch();
conn.commit();
private Connection getConnection()throws Exception{
String dataSourcePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if(dataSourcePath.indexOf("WEB-INF")>0){
dataSourcePath = dataSourcePath.substring(0,dataSourcePath.indexOf("WEB-INF/classes"));
}
//将属性文件加载到内存
Properties properties = new Properties();
FileInputStream in = new FileInputStream(dataSourcePath+"WEB-INF/config/jdbc.properties");
properties.load(in);
in.close();
//读取属性文件
String classname = properties.getProperty("jdbc.driverClassName");
String url = properties.getProperty("jdbc.url");
String username = properties.getProperty("jdbc.username");
String password = properties.getProperty("jdbc.password");
//DBUtil dbUtil = DBUtil.getInstance();
Connection conn = DriverManager.getConnection(url, username, password);
//返回连接
return conn;
}