From: Ian Pilcher <i.pilcher@comcast.net>
To: pgsql-jdbc@postgresql.org
Subject: Foreign key constraint & getErrorCode
Date: Fri, 10 Sep 2004 10:49:02 -0500
Message-ID: <chsidf$6ha$1@sea.gmane.org> (raw)
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
========================================================================
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: i.pilcher@comcast.net
Subject: Re: Foreign key constraint & getErrorCode
In-Reply-To: <chsidf$6ha$1@sea.gmane.org>
References: <chsidf$6ha$1@sea.gmane.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox