【求助】我使用JAVA JDBC的批次提交的問題

rexfa發表於2006-09-06
我使用 類似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();
}
}

相關文章