I'm trying to read a DATETIME field from my Pg 6.4.2 using JDBC.
When i try reading the date from my ResultSet using ResultSet.getDate()
i get an error stating:
java.sql.SQLException: Bad Date Format: at 0 in Thu Aug 19 12:19:52 1999
CESTat postgresql.ResultSet.getDate(ResultSet.java:407)at TestDate.main(TestDate.java:32)
When i try using ResultSet.getTimestamp() instead, i also get an error:
java.lang.NumberFormatException: 1999 CESTat java.lang.Float.<init>(Float.java)at
postgresql.ResultSet.getTimestamp(ResultSet.java:455)atTestDate.main(TestDate.java:32)
I CAN use getString(), but i'd like to get a real Date object, not just
a String. Can anyone help ?
Best regards, Jon Bundesen.
Here is the code if anyone is interested:
===========================
Table = foo
+----------------------------------+----------------------------------+-------+
| Field |
Type | Length|
+----------------------------------+----------------------------------+-------+
| dt |
datetime | 8 |
+----------------------------------+----------------------------------+-------+
import java.sql.*;
import java.sql.Date;
public class TestDate {
public static void main (String args[]) throws Exception { Connection con; Statement statement;
PreparedStatementpreparedStatement; ResultSet rs; // String url = "jdbc:postgresql:test1"; String url =
"jdbc:postgresql://127.0.0.1:5432/test1";
Class.forName("postgresql.Driver"); con = DriverManager.getConnection(url, "jon", ""); con.setAutoCommit( false
);
//
statement = con.createStatement(); statement.executeUpdate("insert into foo values ( current_timestamp
)"); con.commit(); statement.close();
//
statement = con.createStatement(); rs = statement.executeQuery("SELECT * FROM foo"); while (rs.next()) {
System.out.println("Result:" + rs.getTimestamp(1)); //System.out.println("Result: " + rs.getDate(1)); }
rs.close(); statement.close();
//
con.close(); }
}