JDBC executeBatch 抛出异常停止
2014-07-21 11:43
89 查看
进行批量更新的时候发现:
addBatch(sql);
executeBatch 抛出异常后,剩余的sql没有更新,即出现异常之前的都入库了,异常之后即使有可执行sql都不会执行。
百度资料后了解:这是DBMS数据库管理系统控制的,有的DBMS在异常之后,剩下的sql也会执行,有的DBMS异常之后其余的不入库。
我的改进方法如下:public static boolean bachInsertData(List<String> sqlList) { boolean bool = false; Connection conn = null;
以前不知道哇啊啊啊啊,好多得改!!!
addBatch(sql);
executeBatch 抛出异常后,剩余的sql没有更新,即出现异常之前的都入库了,异常之后即使有可执行sql都不会执行。
百度资料后了解:这是DBMS数据库管理系统控制的,有的DBMS在异常之后,剩下的sql也会执行,有的DBMS异常之后其余的不入库。
我的改进方法如下:public static boolean bachInsertData(List<String> sqlList) { boolean bool = false; Connection conn = null;
public static boolean bachInsertData(List<String> sqlList) { boolean bool = false; Connection conn = null; Statement stm = null; try { conn = MyDAO.getConnection(); conn.setAutoCommit(false); stm = conn.createStatement(); for (String sql : sqlList) { stm.addBatch(sql); } stm.executeBatch(); conn.commit(); bool = true; } catch (Exception e) { if(e instanceof BatchUpdateException){ BatchUpdateException bException = (BatchUpdateException)e; int[] s = bException.getUpdateCounts(); logger.info("更新失败数据:"+sqlList.get(s.length)); if(s.length+1<sqlList.size()){ List<String> sList = sqlList.subList(s.length+1, sqlList.size()); bachInsertData(sList); } }else{ e.printStackTrace(); try { if (stm != null) stm.close(); } catch (SQLException ex) { ex.printStackTrace(); } try { conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } } finally { try { if (stm != null) stm.close(); } catch (SQLException ex) { ex.printStackTrace(); } try { conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return bool; }
以前不知道哇啊啊啊啊,好多得改!!!
相关文章推荐
- Could not execute JDBC batch update异常的解决办法
- Hibernate数据关联映射出现Could not execute JDBC batch update的异常处理
- 小谈Hibernate下SQL Grammer Exception,Could not execute JDBC batch update异常的解决
- Hibernate 抛出的 Could not execute JDBC batch update
- hibernate的Could not execute JDBC batch update错误原因及处理
- nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
- hibernate 错误 Could not execute JDBC batch update,已解决
- org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
- Could not execute JDBC batch update
- org.hibernate.exception.SQLGrammarExceptionCould not execute JDBC batch update的问题
- org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update错误
- org.hibernate.exception.SQLGrammarException:Could not execute JDBC batch update问题的解决
- hibernate 级联删除报更新失败的问题(org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update)
- Could not execute JDBC batch update和hibernate.dialect' must be set when no Connection available错误
- org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
- jdbc 捕捉sql server raiseerror 方法抛出的异常
- SQLGrammarException: Could not execute JDBC batch update
- org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
- hibernate的Could not execute JDBC batch update错误原因及处理
- org.springframework.dao.DataIntegrityViolationException Could not execute JDBC batch update