getDate() method and postgres datatype

K

K S Aldebaraan

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)
 
C

Chris Smith

K said:
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
 
N

Neomorph

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.
 
K

K S Aldebaraan

Neomorph said:
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.


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


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

K S Aldebaraan

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top