java 批量插入问题

[复制链接]
查看11 | 回复8 | 2011-2-18 11:43:34 | 显示全部楼层 |阅读模式
用executeBatch批量插入数据,表上有唯一约束,有一条记录冲突会导致全部不能插入,有办法忽略失败记录,继续插入剩余记录吗?
回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
有人用过jdbc吗?
回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
本帖最后由 eof007 于 2012-9-27 11:35 编辑
暂时想到 insert into。。。 log errors reject limit unlimited
不知能否用于array interface。准备测下

测试通过。可以用于array interface。
问题解决。

回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
在存储过程中实现 batch 和日志, 最后JAVA 调用即可
回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
solomon_007 发表于 2012-9-27 20:35
在存储过程中实现 batch 和日志, 最后JAVA 调用即可

如果lz的java程序自己能解决,就不需要用存储过程了
回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
〇〇 发表于 2012-9-27 21:10
如果lz的java程序自己能解决,就不需要用存储过程了

JAVA 数据库开发很多要字符串去拼接SQL, 好累啊, 不便调试, 远不如PLSQL 来得方便...
回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
exception table就是比较好的办法了
更好的办法就是事前预防,这个需要合理的业务需求来配合才做得到
回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
上面的方法还是db层的方法,学习了
JDBC规范是支持出错后继续的:
After a command in a batch update fails to execute properly and a BatchUpdateException is thrown, the driver may or may not continue to process the remaining commands in the batch. If the driver continues processing after a failure, the array returned by the method BatchUpdateException.getUpdateCounts will have an element for every command in the batch rather than only elements for the commands that executed successfully before the error. In the case where the driver continues processing commands, the array element for any command that failed is Statement.EXECUTE_FAILED.
但是oracle jdbc是不支持的:
After a batch exception, the update counts array can be retrieved using the getUpdateCounts method of the BatchUpdateException object. This returns an int array of update counts, just as the executeBatch method does. In the Oracle implementation of standard update batching, contents of the update counts array are as follows, after a batch is processed:
For a prepared statement batch, it is not possible to know which operation failed. The array has one element for each operation in the batch, and each element has a value of -3. According to the JDBC 2.0 specification, a value of -3 indicates that an operation did not complete successfully. In this case, it was presumably just one operation that actually failed, but because the JDBC driver does not know which operation that was, it labels all the batched operations as failures.
You should always perform a ROLLBACK operation in this situation.
For a generic statement batch or callable statement batch, the update counts array is only a partial array containing the actual update counts up to the point of the error. The actual update counts can be provided because Oracle JDBC cannot use true batching for generic and callable statements in the Oracle implementation of standard update batching.
For example, if there were 20 operations in the batch, the first 13 succeeded, and the 14th generated an exception, then the update counts array will have 13 elements, containing actual update counts of the successful operations.
回复

使用道具 举报

千问 | 2011-2-18 11:43:34 | 显示全部楼层
所有数据先插入临时表,然后再批量往正式表插入的时候,加个not in或者not exists不就行了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行