java 批量提交 oracle,java使用oracle数据库进行批量插入操作

该博客详细介绍了如何使用Java通过PreparedStatement进行批量插入操作,涉及了JDBC连接、PreparedStatement的使用、参数设置以及事务处理。重点在于批量添加数据到数据库表的过程。
摘要由CSDN通过智能技术生成

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值