I am using Java with Microsoft Access through an ODBC driver. When I insert a duplicate entry for primary key it gives me an error: java.sql.SQLException: General error
. I want to show a message to the user that this record already exists, but I think that this exception can be thrown by ODBC in some other cases also. So I found that there are error codes against each message (ref), but I found no error code for primary key violation. Can anyone tell me what error code is for primary key violation for ODBC with MS Access?
Here is basic code
String qry = "INSERT INTO customers VALUES ('" + txtReg.getText()
+ "' ,'" + txtName.getText() + "', '" + txtCity.getText() + "' ,'" + txtCell.getText() + "')";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MyDB");
Statement st = con.createStatement();
st.executeQuery(qry);
st.close();
con.close();
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex, "Error!", JOptionPane.ERROR_MESSAGE);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex, "Error!", JOptionPane.ERROR_MESSAGE);
}
These txtName
and so on are JTextFields
. Here is complete Stack trace
connected
st created
Error code: 0
SQLState: S1000
Messsage: General error
java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6986)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3110)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
at gui.InsertFileCode.btnInsertActionPerformed(insertFileCode.java:399)