Foreign key constraint & getErrorCode

From: Ian Pilcher <i(dot)pilcher(at)comcast(dot)net>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Foreign key constraint & getErrorCode
Date: 2004-09-10 15:49:02
Message-ID: chsidf$6ha$1@sea.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

I'm using PostgreSQL 7.4.2 on Fedora Core 2 (included RPMS). When a
foreign key constraint fails during an insert, SQLException.getErrorCode
is returning zero.

Here is a code snippet to demonstrate the problem:

import java.io.*;
import java.sql.*;

class ForeignKey
{
public static void main(String [] args) throws Exception
{
Class.forName("org.postgresql.Driver");
Connection db = DriverManager.getConnection(args[0], args[1],
args[2]);
Statement st = db.createStatement();

st.execute("CREATE TABLE table1 (id INT PRIMARY KEY)");

st.execute("CREATE TABLE table2 (id INT PRIMARY KEY, " +
"fk INT NOT NULL REFERENCES " +
"table1)");

try
{
st.execute("INSERT INTO table2 (id, fk) VALUES (0, 0)");
}
catch (SQLException sqle)
{
System.err.println(sqle.getErrorCode() + ": " +
sqle.getMessage());
}
}
}

And here's what I get when I run it:

[pilcher(at)home temp]$ echo $CLASSPATH
.:/usr/share/java/pg74.1jdbc3.jar
[pilcher(at)home temp]$ java ForeignKey jdbc:postgresql:tomcat4 tomcat4 ''
0: ERROR: insert or update on table "table2" violates foreign key
constraint "$1"

Based on my reading of the PostgreSQL docs, getErrorCode should be
returning 23503. Any idea what's going on?

Thanks!

--
========================================================================
Ian Pilcher i(dot)pilcher(at)comcast(dot)net
========================================================================

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Oliver Jowett 2004-09-10 22:43:46 Re: Foreign key constraint & getErrorCode
Previous Message Guillermo 2004-09-10 15:00:28 Postgres 8 & JBuilder X