Java论坛网»Java技术»【求助】我使用JAVA JDBC的批量提交插入数据遇到异常(一条违反约束的信息),其后的插入无法进行

【求助】我使用JAVA JDBC的批量提交插入数据遇到异常(一条违反约束的信息),其后的插入无法进行

问?:
我使用 类似Oralce网站上的批量插入方法。
其中setDefaultExecuteBatch (100);再每批中 有一条违反约束的信息(主键或者外键的约束)
其后边的数据全部丢失(被数据库回滚 )

java.sql.SQLException: ORA-02291: integrity constraint (YONG.FK_RE_1) violated - parent key not found

请问有没有办法,无视这条违反约束的信息引起的异常,保证后边的信息插入,只要这条不被
插入到数据库中就可以了。


我参照的程序如下:
import java.sql.*;

// You need to import oracle.jdbc.driver.* in order to use the
// API extensions.
import oracle.jdbc.driver.*;

class SetExecuteBatch
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");

// Default batch value set to 2 for all prepared statements belonging
// to this connection.
((OracleConnection)conn).setDefaultExecuteBatch (2);

PreparedStatement ps =
conn.prepareStatement ("insert into dept values (?, ?, ?)");

ps.setInt (1, 12);
ps.setString (2, "Oracle");
ps.setString (3, "USA");

// No data is sent to the database by this call to executeUpdate
System.out.println ("Number of rows updated so far: "
+ ps.executeUpdate ());

ps.setInt (1, 11);
ps.setString (2, "Applications");
ps.setString (3, "Indonesia");

// The number of batch calls to executeUpdate is now equal to the
// batch value of 2. The data is now sent to the database and
// both rows are inserted in a single roundtrip.
int rows = ps.executeUpdate ();
System.out.println ("Number of rows updated now: " + rows);

ps.close ();
conn.close();
}
}
答!: 1:
不知道什么原因 不过你不嫌麻烦的话 可以对每一句都try catch 哈哈~
答!: 2:
只有一种方法,就是每条try catch.
判断一意制约的error code,在继续。

相关JAVA教程:
利用dom4j的xpath读取子节点的问题
如何table表导出为excel表
在struts 中如何用FileUpload 的组件呢?
struts + ajax的后台问题
spring+hibernate连接数据库问题
推荐一本学习EJB和J2EE的好书 适合有一定jsp struts 编程经验的大学生看的 呵呵
rational rose 下载地址
netbeans 5.0是不是存在内存泄漏啊?
60分在等,javascript 触发Action的问题!请大家执教!
一个jsp赋值问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
求助,一个xml编码的问题,急!
使用Struts开发强制修改用户密码问题,急急急!!请教各位的帮助!