Unix Technical Forum

Java: MySQL DATETIME conversion errors?

This is a discussion on Java: MySQL DATETIME conversion errors? within the MySQL forums, part of the Database Server Software category; --> I do the following in my java application: Statement s = myConnection.createStatement(); ResultSet rs = s.executeQuery("SELECT startTime FROM MyTable ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 08:32 AM
Cédric Pillonel
 
Posts: n/a
Default Java: MySQL DATETIME conversion errors?

I do the following in my java application:

Statement s = myConnection.createStatement();
ResultSet rs = s.executeQuery("SELECT startTime FROM MyTable WHERE
_rowid=1");
if (rs.next())
{
java.sql.Timestamp stamp = (java.sql.Timestamp)rs.getObject(1);
String startTime = stamp.toString();
}

I get the following value in startTime: "2006-03-02 12:00:00.0"
But in the database is the following value stored: "2006-02-30 12:00:00.0"

I am running MySQL 4.1.12 on RedHat Linux ES 4.0 EMT64.
I run my java application on Windows 2000 with java 1.5.0_03-b07 and
mysql-connector-java-3.1.12. I have also tried
mysql-connector-java-5.0.0-beta, same problem.

Any idea?
Thank you.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 08:32 AM
Brian Wakem
 
Posts: n/a
Default Re: Java: MySQL DATETIME conversion errors?

Cédric Pillonel wrote:

> I do the following in my java application:
>
> Statement s = myConnection.createStatement();
> ResultSet rs = s.executeQuery("SELECT startTime FROM MyTable WHERE
> _rowid=1");
> if (rs.next())
> {
> java.sql.Timestamp stamp = (java.sql.Timestamp)rs.getObject(1);
> String startTime = stamp.toString();
> }
>
> I get the following value in startTime: "2006-03-02 12:00:00.0"
> But in the database is the following value stored: "2006-02-30 12:00:00.0"



No such date as Feb 30th


> I am running MySQL 4.1.12 on RedHat Linux ES 4.0 EMT64.
> I run my java application on Windows 2000 with java 1.5.0_03-b07 and
> mysql-connector-java-3.1.12. I have also tried
> mysql-connector-java-5.0.0-beta, same problem.
>
> Any idea?



It is assuming you mean 2 days after Feb 28th.


--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 08:32 AM
Peter H. Coffin
 
Posts: n/a
Default Re: Java: MySQL DATETIME conversion errors?

On Thu, 30 Mar 2006 16:00:14 +0200, Cédric Pillonel wrote:
> I do the following in my java application:
>
> Statement s = myConnection.createStatement();
> ResultSet rs = s.executeQuery("SELECT startTime FROM MyTable WHERE
> _rowid=1");
> if (rs.next())
> {
> java.sql.Timestamp stamp = (java.sql.Timestamp)rs.getObject(1);
> String startTime = stamp.toString();
> }
>
> I get the following value in startTime: "2006-03-02 12:00:00.0"
> But in the database is the following value stored: "2006-02-30 12:00:00.0"


As of MySQL 4.1, no datetime or timestamp columns present with a
fractional second. Look at the database with another tool than your java
application (ideally, the mysql client), to see what's really in the
database. I suspect that your conversion from an SQL result set into a
java timestamp is not doing it correctly.

> I am running MySQL 4.1.12 on RedHat Linux ES 4.0 EMT64.
> I run my java application on Windows 2000 with java 1.5.0_03-b07 and
> mysql-connector-java-3.1.12. I have also tried
> mysql-connector-java-5.0.0-beta, same problem.


--
Graham's First Rule of Internet Retailing:
If your 'shopping cart' site requires anything more complex than
HTML, SSL and a session cookie, at least one of your competitors
will run a site which does not. Your competitor will get the sale.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 08:32 AM
Bill Karwin
 
Posts: n/a
Default Re: Java: MySQL DATETIME conversion errors?

Brian Wakem wrote:
> It is assuming you mean 2 days after Feb 28th.


Yes. This was reported as a bug
(http://bugs.mysql.com/bug.php?id=16147) and was closed, claiming it is
intentional behavior.

In 5.0, if you try to put in an invalid date, it issues a warning or an
error by default.

SELECT DATE('2006-02-30');
Error 1292 Truncated incorrect datetime value: '2006-02-30'

SET GLOBAL SQL_MODE='ALLOW_INVALID_DATES';
SELECT DATE('2006-02-30');
2006-02-30

In 4.1 and prior releases, MySQL allows invalid dates; day must be
0..31, and month must be 0..12.

See also:
http://dev.mysql.com/doc/refman/5.0/...ime-types.html
http://dev.mysql.com/doc/refman/5.0/...-sql-mode.html

Regards,
Bill K.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:13 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com