Foreign key constraint & getErrorCode - Mailing list pgsql-jdbc

From Ian Pilcher
Subject Foreign key constraint & getErrorCode
Date
Msg-id chsidf$6ha$1@sea.gmane.org
Whole thread Raw
Responses Re: Foreign key constraint & getErrorCode  (Oliver Jowett <oliver@opencloud.com>)
List 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@home temp]$ echo $CLASSPATH
.:/usr/share/java/pg74.1jdbc3.jar
[pilcher@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.pilcher@comcast.net
========================================================================

pgsql-jdbc by date:

Previous
From: Tore Halset
Date:
Subject: Re: [GENERAL] ERROR: canceling query due to user request
Next
From: Oliver Jowett
Date:
Subject: Re: Foreign key constraint & getErrorCode