Primary Key Conflicts While Inserting Rows
Hi,
I am inserting some rows into a table which has primary key constraints on columns 1,2,3. I have set the key columns in my program to 1,2,3. When I try to insert a row it throws a syncresolver exception stating that there is a key conflict. But i don't understand why it is doing this because there is no conflict. I can remove the primary key constraints off the sql table (but still leave them on in the program) and it will insert everything correctly.
Any ideas?
Thanks
# 2
If you have 3 primary keys, your database may be set up to either require all 3 keys together be unique, or each by themselves to be unique. My guess is the second case.
If you search on google for
syncresolver exception sql
you may get more information to help you, such as:
http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/spi/SyncProviderException.html
# 8
Using SQL Server 2005 Drivers.
import java.sql.*;
import javax.sql.rowset.*;
import com.sun.rowset.*;
public class TestDB {
public static void main(String[] args){
String connectionUrl = "jdbc:sqlserver://DBSERVER;DatabaseName=fTrack;user=sa;password=1234";
// Declare the JDBC objects.
Connection con = null;
CachedRowSet crs = null;
int keys[] = {1,2,3};
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * FROM pt_OMS_Data WHERE Switch=4";
crs = new CachedRowSetImpl();
crs.setCommand(SQL);
crs.execute(con);
crs.setKeyColumns(keys);
crs.beforeFirst();
crs.moveToInsertRow();
crs.updateInt(1, 5);
crs.updateTimestamp(2, Timestamp.valueOf("2007-07-20 00:00:00"));
crs.updateInt(3, 0);
crs.insertRow();
crs.moveToCurrentRow();
crs.moveToInsertRow();
crs.updateInt(1, 5);
crs.updateTimestamp(2, Timestamp.valueOf("2007-07-20 00:00:00"));
crs.updateInt(3, 1);
crs.insertRow();
crs.moveToCurrentRow();
crs.acceptChanges(con);
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
} finally {
if (crs != null)
try {
crs.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
}
}
Layout of DB:
Switch Date BSC ColA ColB ColC
Switch, Date, and BSC are the PKs.
Switch is type Int, Date is type datetime, BSC is type int
Here is the error which i receive:
javax.sql.rowset.spi.SyncProviderException: 4 conflicts while synchronizing
at com.sun.rowset.internal.CachedRowSetWriter.writeData(Unknown Source)
at com.sun.rowset.CachedRowSetImpl.acceptChanges(Unknown Source)
at com.sun.rowset.CachedRowSetImpl.acceptChanges(Unknown Source)
at TestDB.main(TestDB.java:39)
I inserted the exact same data manually into the DB and it works fine.
By the way, the program inserts the first row, but not the second. Based on my trials with the original program I am working on the syncproviderexception handler seems to have a conflict on insert row on Switch column. This is why I believe java is not interpreting the PKs as a group but rather as individually.
Message was edited by:
bce_developer