getDate() method and postgres datatype

Discussion in 'Java' started by K S Aldebaraan, Aug 15, 2003.

  1. I'm calling the getDate() method on a ResultSet to get data that has
    been pulled from a PostgreSQL database. The datatype in the db is
    int8, and i'm just calling getDate because I'm assuming these are
    equivalent.

    I've already tried using the date, timestamp, and numeric datatypes,
    and I'm not understanding why none will work. Here is my piece of
    code (from DBEvent.java) that makes the attempt to get the data:

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM Events WHERE
    eventid='" + eventid + "'");
    if (rs.next()) {
    this.duedate = rs.getDate("duedate");

    ....etc, etc, etc.

    And here's the pertinent stack trace:

    Embedded exception is:
    java.lang.IllegalArgumentException
    at java.sql.Date.valueOf(Date.java:100)
    at org.postgresql.jdbc1.AbstractJdbc1ResultSet.toDate(AbstractJdbc1ResultSet.java:789)
    at org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSet.java:228)
    at org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSet.java:388)
    at db.DBEvent.<init>(DBEvent.java:54)
    at db.DBJob.getJobEvents(DBJob.java:85)
    at data.Tester.testUsers(Tester.java:147)
    at data.Tester.main(Tester.java:223)
    K S Aldebaraan, Aug 15, 2003
    #1
    1. Advertising

  2. K S Aldebaraan

    Chris Smith Guest

    K S Aldebaraan wrote:
    > I'm calling the getDate() method on a ResultSet to get data that has
    > been pulled from a PostgreSQL database. The datatype in the db is
    > int8, and i'm just calling getDate because I'm assuming these are
    > equivalent.


    Well, no. int8 and ResultSet.getDate are certainly not compatible.
    You'll need a column in the database that is either some kind of date
    data type, or (kinda kludgy) a String that holds a date in a format
    parseable by java.sql.Date.valueOf [that is, yyyy-mm-dd, according to
    the API docs].

    > I've already tried using the date, timestamp, and numeric datatypes,
    > and I'm not understanding why none will work.


    Are you saying you've tried using the date data type in your SQL table?
    If so, what happened then?

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Aug 15, 2003
    #2
    1. Advertising

  3. K S Aldebaraan

    Neomorph Guest

    On 15 Aug 2003 14:22:03 -0700, (K S Aldebaraan)
    two-finger typed:

    >I'm calling the getDate() method on a ResultSet to get data that has
    >been pulled from a PostgreSQL database. The datatype in the db is
    >int8, and i'm just calling getDate because I'm assuming these are
    >equivalent.


    Why would you assume that ?

    int8 sounds more like either an 8-bit integer number (a byte) or an 8-byte
    integer number (64bit, like long in java, which is more likely).

    Even if the number stored is meant to represent a date (i.e. ddMMyyyy,
    where each character is represented in ASCII), it still wouldn't be a date
    type, more like a human programmed kludge.

    Try getLong() or something similar.

    >
    >I've already tried using the date, timestamp, and numeric datatypes,
    >and I'm not understanding why none will work. Here is my piece of
    >code (from DBEvent.java) that makes the attempt to get the data:
    >
    > Statement stmt = conn.createStatement();
    > ResultSet rs = stmt.executeQuery("SELECT * FROM Events WHERE
    >eventid='" + eventid + "'");
    > if (rs.next()) {
    > this.duedate = rs.getDate("duedate");
    >
    >...etc, etc, etc.
    >
    >And here's the pertinent stack trace:
    >
    >Embedded exception is:
    >java.lang.IllegalArgumentException


    This could be interpreted to mean: nope it's not a date.

    > at java.sql.Date.valueOf(Date.java:100)
    > at org.postgresql.jdbc1.AbstractJdbc1ResultSet.toDate(AbstractJdbc1ResultSet..java:789)
    > at org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSet.java:228)
    > at org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSet.java:388)
    > at db.DBEvent.<init>(DBEvent.java:54)
    > at db.DBJob.getJobEvents(DBJob.java:85)
    > at data.Tester.testUsers(Tester.java:147)
    > at data.Tester.main(Tester.java:223)


    Cheers.
    Neomorph, Aug 16, 2003
    #3
  4. Neomorph <> wrote in message news:<>...
    > On 15 Aug 2003 14:22:03 -0700, (K S Aldebaraan)
    > two-finger typed:
    >
    > >I'm calling the getDate() method on a ResultSet to get data that has
    > >been pulled from a PostgreSQL database. The datatype in the db is
    > >int8, and i'm just calling getDate because I'm assuming these are
    > >equivalent.

    >
    > Why would you assume that ?
    >
    > int8 sounds more like either an 8-bit integer number (a byte) or an
    > 8-byte
    > integer number (64bit, like long in java, which is more likely).
    >
    > Even if the number stored is meant to represent a date (i.e. ddMMyyyy,
    > where each character is represented in ASCII), it still wouldn't be a
    > date
    > type, more like a human programmed kludge.
    >
    > Try getLong() or something similar.
    >
    > >
    > >I've already tried using the date, timestamp, and numeric datatypes,
    > >and I'm not understanding why none will work. Here is my piece of
    > >code (from DBEvent.java) that makes the attempt to get the data:
    > >
    > > Statement stmt = conn.createStatement();
    > > ResultSet rs = stmt.executeQuery("SELECT * FROM Events WHERE
    > >eventid='" + eventid + "'");
    > > if (rs.next()) {
    > > this.duedate = rs.getDate("duedate");
    > >
    > >...etc, etc, etc.
    > >
    > >And here's the pertinent stack trace:
    > >
    > >Embedded exception is:
    > >java.lang.IllegalArgumentException

    >
    > This could be interpreted to mean: nope it's not a date.
    >
    > > at java.sql.Date.valueOf(Date.java:100)
    > > at

    > org.postgresql.jdbc1.AbstractJdbc1ResultSet.toDate(AbstractJdbc1ResultSet
    > .java:789)
    > > at

    > org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSe
    > t.java:228)
    > > at

    > org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSe
    > t.java:388)
    > > at db.DBEvent.<init>(DBEvent.java:54)
    > > at db.DBJob.getJobEvents(DBJob.java:85)
    > > at data.Tester.testUsers(Tester.java:147)
    > > at data.Tester.main(Tester.java:223)

    >
    > Cheers.


    Thanks for the info, I guess I should have been more explicit about
    what I've tried, as well. The real problem I'm trying to solve is
    that I want to store dates in my db as java.util.Date. Perhaps there
    is a better way, but this way I can convert them to a string however I
    wish, and still get them from the database sorted (with an "ORDER
    BY"). I've tried setting the column to the date datatype, but java
    doesn't recognize it as a date (i.e. I get the same error). I've
    tried setting it to the int8 datatype, but then I can't find a way to
    convert it to a java.util.Date. Thanks for the input so far, I'm
    learning...
    K S Aldebaraan, Aug 16, 2003
    #4
  5. Thanks for everyone's help on this. I've *learned* the solution. I
    wanted to store the date as a long so I could easily sort it. But I
    also wanted the date format so I could easily display it for users. I
    finally discovered that I could store it as a long and convert it to a
    date and thus a date string with the following lines of code:

    java.text.SimpleDateFormat SDF = new java.text.SimpleDateFormat("dd
    MMM yyyy");
    String strdate = new String("23 Oct 2004"); //create string
    java.util.Date thedate = SDF.parse(strdate); //string to date
    long lngdate = thedate.getTime(); //date to long
    java.util.Date newdate = new java.util.Date(lngdate); //long to
    date
    String newstrdate = SDF.format(newdate); //date to string



    (K S Aldebaraan) wrote in message news:<>...
    > Neomorph <> wrote in message news:<>...
    > > On 15 Aug 2003 14:22:03 -0700, (K S Aldebaraan)
    > > two-finger typed:
    > >
    > > >I'm calling the getDate() method on a ResultSet to get data that has
    > > >been pulled from a PostgreSQL database. The datatype in the db is
    > > >int8, and i'm just calling getDate because I'm assuming these are
    > > >equivalent.

    > >
    > > Why would you assume that ?
    > >
    > > int8 sounds more like either an 8-bit integer number (a byte) or an
    > > 8-byte
    > > integer number (64bit, like long in java, which is more likely).
    > >
    > > Even if the number stored is meant to represent a date (i.e. ddMMyyyy,
    > > where each character is represented in ASCII), it still wouldn't be a
    > > date
    > > type, more like a human programmed kludge.
    > >
    > > Try getLong() or something similar.
    > >
    > > >
    > > >I've already tried using the date, timestamp, and numeric datatypes,
    > > >and I'm not understanding why none will work. Here is my piece of
    > > >code (from DBEvent.java) that makes the attempt to get the data:
    > > >
    > > > Statement stmt = conn.createStatement();
    > > > ResultSet rs = stmt.executeQuery("SELECT * FROM Events WHERE
    > > >eventid='" + eventid + "'");
    > > > if (rs.next()) {
    > > > this.duedate = rs.getDate("duedate");
    > > >
    > > >...etc, etc, etc.
    > > >
    > > >And here's the pertinent stack trace:
    > > >
    > > >Embedded exception is:
    > > >java.lang.IllegalArgumentException

    > >
    > > This could be interpreted to mean: nope it's not a date.
    > >
    > > > at java.sql.Date.valueOf(Date.java:100)
    > > > at

    > > org.postgresql.jdbc1.AbstractJdbc1ResultSet.toDate(AbstractJdbc1ResultSet
    > > .java:789)
    > > > at

    > > org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSe
    > > t.java:228)
    > > > at

    > > org.postgresql.jdbc1.AbstractJdbc1ResultSet.getDate(AbstractJdbc1ResultSe
    > > t.java:388)
    > > > at db.DBEvent.<init>(DBEvent.java:54)
    > > > at db.DBJob.getJobEvents(DBJob.java:85)
    > > > at data.Tester.testUsers(Tester.java:147)
    > > > at data.Tester.main(Tester.java:223)

    > >
    > > Cheers.

    >
    > Thanks for the info, I guess I should have been more explicit about
    > what I've tried, as well. The real problem I'm trying to solve is
    > that I want to store dates in my db as java.util.Date. Perhaps there
    > is a better way, but this way I can convert them to a string however I
    > wish, and still get them from the database sorted (with an "ORDER
    > BY"). I've tried setting the column to the date datatype, but java
    > doesn't recognize it as a date (i.e. I get the same error). I've
    > tried setting it to the int8 datatype, but then I can't find a way to
    > convert it to a java.util.Date. Thanks for the input so far, I'm
    > learning...
    K S Aldebaraan, Aug 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kris
    Replies:
    1
    Views:
    4,966
    David Fisher
    Dec 10, 2003
  2. Yann Laviolette

    Linux - Getdate question and calling another program

    Yann Laviolette, Nov 2, 2003, in forum: C Programming
    Replies:
    3
    Views:
    491
    Yann Laviolette
    Nov 2, 2003
  3. Yann Laviolette

    Getdate and gettime

    Yann Laviolette, Nov 2, 2003, in forum: C Programming
    Replies:
    3
    Views:
    463
    Mike Wahler
    Nov 2, 2003
  4. Jeff
    Replies:
    2
    Views:
    1,148
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Apr 16, 2007
  5. Nick Black
    Replies:
    1
    Views:
    155
    Kashia Buch
    Nov 16, 2006
Loading...

Share This Page