java操作数据库批量更新主要为addBatch() 和executeBatch()方法,一般将数据存储在ArrayList里面,一次批量更新为一个addBatch(),全部更新完后,统一执行executeBatch()方法。一下为代码:
public void UpdateData2(List<String[]> list){
try {
connection = getConnection();
String sqlStr = "update t_mas_tph set TPHNAME=?,GENERATEPOSITION=?,GENERATELON=?,GENERATELAT=?,LANDPOSITION=?,LANDLON=?,LANDLAT=?,LOGINTIME=?,TPHINTENSITY=? where TPHNUMBER=?";
pstm = (sqlStr);
for (String[] strings : list) {
if(==8){
(1, strings[1]);
(2, strings[2]);
(3,strings[3].split(",")[0] );
(4, strings[3].split(",")[1]);
(5, strings[4]);
(6, strings[5].split(",")[0]);
(7, strings[5].split(",")[1]);
(8, ChangeTime2Date(strings[6]));
(9, strings[7]);
(10,strings[0] );
}else{
continue;
}
();
}
();
} catch (Exception e) {
();
}finally{
ReleaseResource();
}
}
注:往数据库里面添加date类型数据时,需要用到String类型时间转化为date:
public Object ChangeTime2Date(String timeStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date dateFormat =(timeStr);
dateSQL = new (());
return dateSQL;
} catch (ParseException e) {
();
}
return null;
}