首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

关于PreparedStatament的大批量安插数据库的写法

2012-11-07 
关于PreparedStatament的大批量插入数据库的写法这样写有问题吗!为什么到最后只执行了一条sql语句 是不是

关于PreparedStatament的大批量插入数据库的写法
这样写有问题吗!
为什么到最后只执行了一条sql语句
是不是应该该成用 Statement 来代替 PreparedStatement呀

public boolean execInsertTables(String[] sqls) {boolean flag = false;con = dbc.getConnection();try {con.setAutoCommit(false);int a = 0;for (int i = 0; i < sqls.length; i++) {pst = con.prepareStatement(sqls[i]);pst.addBatch();if(i<sqls.length-1){pst.close();}a = i;}System.out.println(a+" ** "+sqls.length);pst.executeBatch();con.commit();flag = true;} catch (Exception e) {flag = false;Tasklogger.debug("插入数据失败 TaskDao 里的 execInsertTables!" + e);e.printStackTrace();} finally {dbc.closeExe(pst, con);}return flag;}
1 楼 damoqiongqiu 2009-10-19   pst = con.prepareStatement(sqls[i]); 
这一句是不是应该写到for循环外面? 2 楼 peter2009 2009-10-20   可是大漠 如果写到了for循环的外面的话是不是就没有意义了
因为我是对不同的表进行的插入操作呀!
那就只能用Statement来代替 PreparedStatement了 3 楼 a5600596zhao 2011-12-22   你每次都close了,只剩下最后一条能执行了

热点排行