Thread: java.sql.date insert as null

java.sql.date insert as null

From
Mican Bican
Date:
Hello,

How can I store a java.sql.date as null in database. The problem is
(pseudecode):
....
java.sql.Date date = null;
....
Customer cust = new Customer(id);
cust.setSqlDate(date);

..insert into Customer (id..,date,...)
values(...
...." ' "+in.getSqlDate()+" ' ".....
...)

then the exception

java.sql.SQLException: ERROR: invalid input syntax for type date: "null"
........

when I select a row with empty date and system.out.print, it  gives me a
null.. but why I can't store it as null
I use jdbc3, postgresql 8.0.1 and java 5 (tiger) on windows

thank you

best regards Mican Bican





Re: java.sql.date insert as null

From
"Prasanth"
Date:
Remove the quotes on either side when inserting null value.

-Prasanth.

-----Original Message-----
From: pgsql-jdbc-owner@postgresql.org
[mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Mican Bican
Sent: Monday, March 07, 2005 1:07 PM
To: pgsql-jdbc@postgresql.org
Subject: [JDBC] java.sql.date insert as null


Hello,

How can I store a java.sql.date as null in database. The problem is
(pseudecode):
....
java.sql.Date date = null;
....
Customer cust = new Customer(id);
cust.setSqlDate(date);

..insert into Customer (id..,date,...)
values(...
...." ' "+in.getSqlDate()+" ' ".....
...)

then the exception

java.sql.SQLException: ERROR: invalid input syntax for type date: "null"
........

when I select a row with empty date and system.out.print, it  gives me a
null.. but why I can't store it as null
I use jdbc3, postgresql 8.0.1 and java 5 (tiger) on windows

thank you

best regards Mican Bican





---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)





Re: java.sql.date insert as null

From
Oliver Jowett
Date:
Mican Bican wrote:

> How can I store a java.sql.date as null in database. The problem is
> (pseudecode):

Real test code would be much better.

> ..insert into Customer (id..,date,...)
> values(...
> ...." ' "+in.getSqlDate()+" ' ".....
> ...)

If in.getSqlDate() returns null, this turns into:

   insert into Customer (...) values (... 'null' ...)

which is wrong -- the NULL should be unquoted.

I'd suggest using a java.sql.PreparedStatement and parameter
placeholders, then you don't need to worry about the details of "how do
I represent a date in the right format for this DB?"

-O