JDBC大批量写入数据到SQLServer2000,记录数大于10000
程序员文章站
2022-09-04 09:06:37
复制代码 代码如下:spobserver.putsp("sessionfactory1"); simpledateformat fomat = new simpledate...
复制代码 代码如下:
spobserver.putsp("sessionfactory1");
simpledateformat fomat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
session s=null;
s=daosupport.getsessionfactory().opensession();
connection con=s.connection();
statement stmt = null;
try {
stmt=con.createstatement();
con.setautocommit(false);
iterator it=list.iterator();
int icount = 1;
long start = system.currenttimemillis();
while(it.hasnext()){
tresultwaterzid t = (tresultwaterzid)it.next();
stringbuffer sb = new stringbuffer();
sb.append("insert into t_result_water_z(schemeid,stcd,z,tm) values('");
sb.append(t.getschemeid()).append("','").append(t.getstcd()).append("','");
sb.append(t.getz()).append("','").append(fomat.format(t.gettm())).append("')");
stmt.addbatch(sb.tostring());
if(icount % 1000 == 0){
stmt.executebatch();
stmt.clearbatch();
}
icount++;
}
stmt.executebatch();
stmt.clearbatch();
con.commit();
long end = system.currenttimemillis();
system.out.println("addtresultwaterzid used time:"+(end-start));
stmt.close();
con.close();
} catch (sqlexception e) {
try {
con.rollback();
} catch (sqlexception e1) {
e1.printstacktrace();
}
e.printstacktrace();
}finally{
try {
stmt.close();
con.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}