Link to home
Start Free TrialLog in
Avatar of bilgehanyildirim
bilgehanyildirim

asked on

Java + MySQL and DateTime column exception

Hi,
I am running following sql and SOMETIMES I get this exception

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '20090220210660.000000' for column 'bidding_time' at row 1
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
        at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:734)
        at com.xxxxxx.auction.AuctionManager.globalTick(AuctionManager.java:105)
        at globaltick.Main.main(Main.java:24)


Can some one help me to optimize this query?

Thanks
s2.execute("INSERT INTO auction_bidding_history (auction_id,bidder,bidding_time,bid_type,new_bid_count) VALUES (" +
                                        rs3.getString("auction_id")+","+
                                        rs3.getString("user_id")+","+
                                        "now()+"+row_count+","+
                                        "1,"+(rs.getInt("bid_count")+1)+")");

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

better off using a PreparedStaement, it will handle formatting for you.

I would use a PreparedStatement and do


ps.setTimestamp
Avatar of bilgehanyildirim
bilgehanyildirim

ASKER

Ok,
Can you give me an example how to use preparedStatement. But please bear in mind, I need to update the timestamp column with current_timestamp+1
removing the plus one should fix it.
why do u need to add 1?

use DATE_ADD() to add to the date, +1 doesn't really do anything.

A kludgy way of getting a String timestamp would be
String ts = String.format("'%s'", new java.sql.Timestamp(System.currentTimeMillis()).toString());

Open in new window

Sorry - missed the +1. So, you could do
String ts = String.format("'%s'", new java.sql.Timestamp(System.currentTimeMillis() + (1000*60*60*24)).toString());

Open in new window

>>I need to update the timestamp column with current_timestamp+1

+1 what btw? One day?
try something like this:

s2.execute("INSERT INTO auction_bidding_history (auction_id,bidder,bidding_time,bid_type,new_bid_count) VALUES (" +
                                        rs3.getString("auction_id")+","+
                                        rs3.getString("user_id")+","+
                                        "date_add(now(), "+row_count+"),"+
                                        "1,"+(rs.getInt("bid_count")+1)+")");

Savant,
I am inserting series of row which need to have 1 second in between them, that's why I am adding 1.
Cehj,
Is there a better way to get the current timestamp and 1 second to it?


Thanks
+1 is 1 second
> Is there a better way to get the current timestamp and 1 second to it?

yes, see the code I posted. Don't do it with java its unnecessary.

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
let me know how that goes :)

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
better off letting the database handle the formatting, safer and in your case more accurate.
Not to mention simpler :)

Savant: Thanks! It solved my problem. Fantastic tip.
By the way did you mean
"DATE_ADD(now(), INTERVAL "+row_count+" SECOND),"+

CEHJ,
Thanks for the tip about the timestamp!!


:-)

Bear in mind that code is not portable
MySQL is the only DB I know :)))) So I don't think it's going to be any problem.
> String ts = String.format("'%s'", new java.sql.Timestamp(System.currentTimeMillis() + 1000).toString());


thats actually non-portable, and error prone.
Better to use a PreparedStatement when using timestamps as I suggested earlier