Thread: How to set java.sql.Date to null ?

How to set java.sql.Date to null ?

From
"Kapoor, Nishikant X"
Date:
Hi there

I'm trying to pass a java.sql.Date parameter to a sql query using
JDBC-PostgreSQL interface. The problem shows up when I pass a null
java.sql.Date but as it happens in my application, the user may or may
not
enter the logDate.
(It works fine if the java.sql.Date(logDate) is a valid date).
Here is the code:

    PreparedStatement pstmt = null;
    try {
      pstmt = connect.prepareStatement("insert into prfs (prfNo, author,
severity, blockFunc, probArea, status, probDesc, logDate, estCompDate,
actCompDate, assigned, assignee,
 solution, attachment, siemensVar, reviewer) values " +
"(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" );

      pstmt.setString(1,  prfNo);
      pstmt.setString(2,  author);
      pstmt.setInt(3,     severity);
      pstmt.setInt(4,     blockFunc);
      pstmt.setInt(5,     probArea);
      pstmt.setInt(6,     status);
      pstmt.setString(7,  probDesc);
-->   pstmt.setDate(8,    logDate); <-- NullPointerException ex
    } catch (SQLException e) { }


How do I set a Date to be a null ?

Thanks
Nishi

--------To err is human but to really mess it up is computer--------
mailto:nishikant.x.kapoor@nspco.com   http://www.c3ipros.com/nkapoor
-----------------------------------~~~------------------------------


Re: [INTERFACES] How to set java.sql.Date to null ?

From
Peter T Mount
Date:
On Mon, 3 Aug 1998, Kapoor, Nishikant X wrote:

> Hi there
>
> I'm trying to pass a java.sql.Date parameter to a sql query using
> JDBC-PostgreSQL interface. The problem shows up when I pass a null
> java.sql.Date but as it happens in my application, the user may or may
> not
> enter the logDate.
> (It works fine if the java.sql.Date(logDate) is a valid date).
> Here is the code:
>
>     PreparedStatement pstmt = null;
>     try {
>       pstmt = connect.prepareStatement("insert into prfs (prfNo, author,
> severity, blockFunc, probArea, status, probDesc, logDate, estCompDate,
> actCompDate, assigned, assignee,
>  solution, attachment, siemensVar, reviewer) values " +
> "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" );
>
>       pstmt.setString(1,  prfNo);
>       pstmt.setString(2,  author);
>       pstmt.setInt(3,     severity);
>       pstmt.setInt(4,     blockFunc);
>       pstmt.setInt(5,     probArea);
>       pstmt.setInt(6,     status);
>       pstmt.setString(7,  probDesc);
> -->   pstmt.setDate(8,    logDate); <-- NullPointerException ex
>     } catch (SQLException e) { }
>
>
> How do I set a Date to be a null ?

Currently the only way is to use:

    if(logDate==null)
      pstmt.setString(8,null);
    else
      pstmt.setDate(8,jobDate);

I'm adding it to my TODO list so that setDate() and others handle nulls
correctly.

--
Peter T Mount peter@retep.org.uk or petermount@earthling.net
Main Homepage: http://www.retep.org.uk
PostgreSQL JDBC Faq: http://www.retep.org.uk/postgres