Connection conn;
Statement stat;
ResultSet rs;
ResultSetMetaData rsmd;
PreparedStatement pstat;
[color=red]/**
* 插入数据库(批量)
* */[/color]
public int addShopInfoList(Listlist){
try {
//获取表名
Properties info = new Properties();
info.load(new FileInputStream(new File("jdbc.properties")));
String table_name= info.getProperty("table_name");
System.out.println(table_name);
long start = System.currentTimeMillis();//开始时间
conn=JdbcUtil.getConnection();
conn.setAutoCommit(false);
String sql ="insert into "+table_name+" " +
"(shop_name,shop_url,area,phone,address,ev_num,bad_num,score,pt_name,fp," +
" open_date,city,sales,code,lon,lat,update_zt,img_zz_count,is_yyzz,is_xkz," +
" is_lhfj,csdj,gldj,jcrq,dwmc,xkzh,fddbr,jydz,ztyt,jyfw," +
" yxq,fl,img_url)" +
" values" +
"(?,?,?,?,?, ?,?,?,?,?," +
" ?,?,?,?,?, ?,?,?,?,?," +
" ?,?,?,?,?, ?,?,?,?,?," +
" ?,?,?)";
pstat= conn.prepareStatement(sql);
for (Shop_Info_Model shopInfoModel:list) {//批量添加
System.err.println(shopInfoModel.getShop_name());
//Map map=(Map) list.get(i);
//System.out.println(map);
pstat.setString(1, shopInfoModel.getShop_name());
pstat.setString(2, shopInfoModel.getShop_url());
pstat.setString(3, shopInfoModel.getArea());
pstat.setString(4, shopInfoModel.getPhone());
pstat.setString(5, shopInfoModel.getAddress());
pstat.setString(6, shopInfoModel.getEv_num());
pstat.setString(7, shopInfoModel.getBad_num());
pstat.setString(8, shopInfoModel.getScore());
pstat.setString(9, shopInfoModel.getPt_name());
pstat.setString(10, shopInfoModel.getFp());
pstat.setString(11, shopInfoModel.getOpen_date());
pstat.setString(12, shopInfoModel.getCity());
pstat.setString(13, shopInfoModel.getSales());
pstat.setString(14, shopInfoModel.getCode());
pstat.setString(15, shopInfoModel.getLon());
pstat.setString(16, shopInfoModel.getLat());
pstat.setString(17, shopInfoModel.getUpdate_zt());
pstat.setString(18, shopInfoModel.getImg_zz_count());
pstat.setString(19, shopInfoModel.getIs_yyzz());
pstat.setString(20, shopInfoModel.getIs_xkz());
pstat.setString(21, shopInfoModel.getIs_lhfj());
pstat.setString(22, shopInfoModel.getCsdj());
pstat.setString(23, shopInfoModel.getGldj());
pstat.setString(24, shopInfoModel.getJcrq());
pstat.setString(25, shopInfoModel.getDwmc());
pstat.setString(26, shopInfoModel.getXkzh());
pstat.setString(27, shopInfoModel.getFddbr());
pstat.setString(28, shopInfoModel.getJydz());
pstat.setString(29, shopInfoModel.getZtyt());
pstat.setString(30, shopInfoModel.getJyfw());
pstat.setString(31, shopInfoModel.getYxq());
pstat.setString(32, shopInfoModel.getFl());
pstat.setString(33, shopInfoModel.getImg_url());
pstat.addBatch();
}
pstat.executeBatch(); //批量执行预定义SQL
conn.commit();//提交事务
//pstat.close();
clojdbc(); //关闭连接
long end = System.currentTimeMillis();
System.out.println("批量插入需要时间:"+(end - start)); //批量插入需要时间:24675
return 1;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
[color=red]/**
* 关闭数据库
* */[/color]
public void clojdbc() {
try {
if (rs != null)
rs.close();
if (stat != null)
stat.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}