PreparedStatement and DB default values - Mailing list pgsql-jdbc

From Tomisław Kityński
Subject PreparedStatement and DB default values
Date
Msg-id 000e01c223a7$b6de2e80$625d4cd5@xpecet
Whole thread Raw
List pgsql-jdbc
Hello,

I have strange problem here. I have table declared as below:

                                      Table "users"
 Column  |         Type          |                       Modifiers
---------+-----------------------+------------------------------------------
-------------
 id_user | integer               | not null default
nextval('"users_id_user_seq"'::text)
 first   | character varying(24) | not null default '(imię)'
 last    | character varying(32) | not null default '(nazwisko)'
 email   | character varying(24) |
 vip     | boolean               | not null default 'f'
 ed      | boolean               | not null default 'f'
Primary key: users_pkey
Rules: on_delete_to_users,
       on_insert_to_users
Triggers: RI_ConstraintTrigger_27009,
          RI_ConstraintTrigger_27011,
          RI_ConstraintTrigger_27019,
          RI_ConstraintTrigger_27021,
          RI_ConstraintTrigger_27028,
          RI_ConstraintTrigger_27030,
          RI_ConstraintTrigger_27063,
          RI_ConstraintTrigger_27065,
          RI_ConstraintTrigger_27086,
          RI_ConstraintTrigger_27088

When I issue a statement from psql console like this:

INSERT INTO users (first, last, email) VALUES ('Frst', 'Lst', 'em@il');

(or AFAIR also from Statement by executeUpdate()), then id_user would be
properly autogenerated. Unfortunately it does not work for
PreparedStatement,
thats what I get:

INSERT INTO "users" ("first", "last", "email", "ed") VALUES (?, ?, ?, ?);
java.sql.SQLException: ERROR:  ExecAppend: Fail to add null value in not
null attribute id_user

        at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:94)
        at org.postgresql.Connection.ExecSQL(Connection.java:398)
        at org.postgresql.jdbc2.Statement.execute(Statement.java:130)
        at org.postgresql.jdbc2.Statement.executeUpdate(Statement.java:73)
        at
org.postgresql.jdbc2.PreparedStatement.executeUpdate(PreparedStatement.java:
113)

The first line is not the stack trace fragment, but update string construed
by application, that is passed (the string) to the prepareStatement()
method.

Why it wants to add the null value to the column, which is not named in
the statement and which have defined default value in database?

I could use usual Statement, if there were no problems with escaping
and/or quoting inserted/updated values. First--this is pain in a** to
compose query and second, for different databases (I inted to write
the code as far flexible and DB independent as I can) the escaping
could be different (especially considering PSQL regexps). Is there
any way to pass it by? Thank you for your help!





pgsql-jdbc by date:

Previous
From: Tomisław Kityński
Date:
Subject: PreparedStatement and DB default values (2)
Next
From: Barry Lind
Date:
Subject: Re: [Fwd: [Fwd: Re: solved: fastpath error?]]