Problem with PreparedStatement.setTimestamp, ODBC & timezone

I'm having a problem with PreparedStatement.setTimestamp. I'm trying to set a timestamp in a MSSQL table using GMT timezone, so I'm using the version of setTimestamp that lets me specify a calendar (void setTimestamp(int parameterIndex, Timestamp x, Calendar cal)).

The problem I'm having is that when I use the JDBC-ODBC bridge, it's not taking into account the calendar (and timezone) that I specify in setTimestamp(). When I do the same exact thing using a different, non-ODBC driver, it works. The non-odbc driver I'm using is net.sourceforge.jtds.jdbc.Driver. I've tried 2 different JDBC-ODBC bridge drivers (sun.jdbc.odbc.JdbcOdbcDriver and one from Easysoft). They both have this problem.

Here is code that demonstrates my problem. If anyone has any experience with this or knows anything about it, any help would be greatly appreciated.

// Sample code to write the current time to a MSSQL table with a datetime field.

// The table being used is:

// CREATE TABLE test(f1 datetime NULL)

publicclass x

{

publicstaticvoid main(String [] args)throws Exception

{

// THIS DOESN'T WORK--WHEN I READ BACK THE TIME ITS NOT RELATIVE TO GMT:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

java.sql.Connection dbconn =

java.sql.DriverManager.getConnection("jdbc:odbc:mydsn","myuser","mypassword");

// THIS WORKS--WHEN I READ BACK THE TIME IT IS RELATIVE TO GMT:

// Class.forName("net.sourceforge.jtds.jdbc.Driver");

// java.sql.Connection dbconn =

//java.sql.DriverManager.getConnection(

//"jdbc:jtds:sqlserver://localhost:1433/mydatabase", "myuser", "mypassword");

java.sql.PreparedStatement stmt = dbconn.prepareStatement("INSERT INTO test(f1) VALUES (?)");

java.util.Date dt =new java.util.Date();

java.sql.Timestamp ts =new java.sql.Timestamp(dt.getTime());

stmt.setTimestamp(1, ts, m_gmtCalendar);

stmt.executeUpdate();

stmt.close();

dbconn.close();

}

privatestaticfinal java.util.Calendar m_gmtCalendar =

java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT"));

}

Message was edited by:

martinog2

[3119 byte] By [martinog2a] at [2007-11-27 9:34:02]
# 1

ODBC is older technology, JDBC (specifically, type 3 or 4) is newer technology and apparently can handle your code where ODBC cant. I suggest for all your projects, use a JDBC if possible and not that old technology.

As for your code, I suggest dump it and try a more direct solution: example:

//get the date/time as of this instance:

Calendar calendar=

Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT"));

Timestamp ts =

new java.sql.Timestamp(calendar.getTime().getTime());

stmt= dbconn.prepareStatement("INSERT INTO person (personID, creationDate) VALUES (?, ?)");

stmt.setInt(1, personID);

stmt.setTimestamp(2, ts);

stmt.executeUpdate();

Note: if this is a web site, you are getting the GMT date/time from the server at your company (if your running from a server) and not from the user's computer (in China) where he is running the browser.

George123a at 2007-7-12 22:57:12 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
By then way, your code should be in a try/catch/finally block to properly close the connection, and you may want to look into using a connection pool instead of a non-connection pool (DriverManager).
George123a at 2007-7-12 22:57:12 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Thanks George123
martinog2a at 2007-7-12 22:57:12 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...