pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: 雨森郷太郎 <gotaroamenomori@gmail.com>
To: pgsql-jdbc@postgresql.org
Subject: Bug? report : PreparedStatement.setObject(java.util.Date) don't work
Date: Sat, 10 Nov 2007 01:48:08 +0900
Message-ID: <3a69fa320711090848n7d51dc57td54f23e0754679cb@mail.gmail.com> (raw)

Hello.

I've faced problem similar to
http://archives.postgresql.org/pgsql-jdbc/2007-06/msg00070.php

Source:
-------------------------------------------------------------------------
package test;

import java.sql.*;
import java.util.*;

public class TestDate{
    public static void main(String[] ags){
        try{
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager.getConnection(
                "jdbc:postgresql:dbname",
                "user",
                "pass");

            PreparedStatement stmt = con.prepareStatement("select *
from table_name where column_name = ?");
            stmt.setObject(1, new java.util.Date());
            ResultSet rs = stmt.executeQuery();

            while(rs.next()){
                System.out.println( rs.getDate(1) );
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

-------------------------------------------------------------------------

Message:
-------------------------------------------------------------------------
org.postgresql.util.PSQLException: Can't infer the SQL type to use for
an instance of java.util.Date. Use setObject() with an explicit Types
value to specify the type to use.
        at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1737)
        at test.TestDate.main(TestDate.java:16)
-------------------------------------------------------------------------

It seemed org.postgresql.jdbc2.AbstractJdbc2Statement#setObject(int,
Object) can't
map java.util.Date to java.sql.Types#DATE.
So I modified AbstractJdbc2Statement.java to accept
java.util.Date,then it worked.

Modification:
-------------------------------------------------------------------------
diff postgresql-jdbc-8.2-506.src/org/postgresql/jdbc2/AbstractJdbc2Statement.java
postgresql-jdbc-8.2-506.src_mod/org/postgresql/jdbc2/AbstractJdbc2Statement.java
1717a1718,1719
>         else if (x instanceof java.util.Date)
>             setDate(parameterIndex, new java.sql.Date(((java.util.Date)x).getTime()));
-------------------------------------------------------------------------

Is this good solution?
Please advice.

Gotaro AMENOMORi

view thread (6+ messages)  latest in thread

Message-ID: <3a69fa320711090848n7d51dc57td54f23e0754679cb@mail.gmail.com>
Permalink:  ../3a69fa320711090848n7d51dc57td54f23e0754679cb@mail.gmail.com/
Also on:    postgresql.org/message-id/3a69fa320711090848n7d51dc57td54f23e0754679cb@mail.gmail.com

 · 

reply

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: gotaroamenomori@gmail.com
  Subject: Re: Bug? report : PreparedStatement.setObject(java.util.Date) don't work
  In-Reply-To: <3a69fa320711090848n7d51dc57td54f23e0754679cb@mail.gmail.com>
  References: <3a69fa320711090848n7d51dc57td54f23e0754679cb@mail.gmail.com>

* 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