bilgehanyildirim
asked on
Java + MySQL and DateTime column exception
Hi,
I am running following sql and SOMETIMES I get this exception
com.mysql.jdbc.MysqlDataTr uncation: Data truncation: Incorrect datetime value: '20090220210660.000000' for column 'bidding_time' at row 1
at com.mysql.jdbc.MysqlIO.che ckErrorPac ket(MysqlI O.java:348 9)
at com.mysql.jdbc.MysqlIO.che ckErrorPac ket(MysqlI O.java:342 3)
at com.mysql.jdbc.MysqlIO.sen dCommand(M ysqlIO.jav a:1936)
at com.mysql.jdbc.MysqlIO.sql QueryDirec t(MysqlIO. java:2060)
at com.mysql.jdbc.ConnectionI mpl.execSQ L(Connecti onImpl.jav a:2536)
at com.mysql.jdbc.ConnectionI mpl.execSQ L(Connecti onImpl.jav a:2465)
at com.mysql.jdbc.StatementIm pl.execute (Statement Impl.java: 734)
at com.xxxxxx.auction.Auction Manager.gl obalTick(A uctionMana ger.java:1 05)
at globaltick.Main.main(Main. java:24)
Can some one help me to optimize this query?
Thanks
I am running following sql and SOMETIMES I get this exception
com.mysql.jdbc.MysqlDataTr
at com.mysql.jdbc.MysqlIO.che
at com.mysql.jdbc.MysqlIO.che
at com.mysql.jdbc.MysqlIO.sen
at com.mysql.jdbc.MysqlIO.sql
at com.mysql.jdbc.ConnectionI
at com.mysql.jdbc.ConnectionI
at com.mysql.jdbc.StatementIm
at com.xxxxxx.auction.Auction
at globaltick.Main.main(Main.
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)+")");
better off using a PreparedStaement, it will handle formatting for you.
I would use a PreparedStatement and do
ps.setTimestamp
ps.setTimestamp
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
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?
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());
Sorry - missed the +1. So, you could do
String ts = String.format("'%s'", new java.sql.Timestamp(System.currentTimeMillis() + (1000*60*60*24)).toString());
>>I need to update the timestamp column with current_timestamp+1
+1 what btw? One day?
+1 what btw? One day?
try something like this:
s2.execute("INSERT INTO auction_bidding_history (auction_id,bidder,bidding _time,bid_ type,new_b id_count) VALUES (" +
rs3.getString("auction_id" )+","+
rs3.getString("user_id")+" ,"+
"date_add(now(), "+row_count+"),"+
"1,"+(rs.getInt("bid_count ")+1)+")") ;
s2.execute("INSERT INTO auction_bidding_history (auction_id,bidder,bidding
rs3.getString("auction_id"
rs3.getString("user_id")+"
"date_add(now(), "+row_count+"),"+
"1,"+(rs.getInt("bid_count
ASKER
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
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
ASKER
+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.
yes, see the code I posted. Don't do it with java its unnecessary.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
let me know how that goes :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
better off letting the database handle the formatting, safer and in your case more accurate.
Not to mention simpler :)
Not to mention simpler :)
ASKER
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!!
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
Bear in mind that code is not portable
ASKER
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. currentTim eMillis() + 1000).toString());
thats actually non-portable, and error prone.
Better to use a PreparedStatement when using timestamps as I suggested earlier
thats actually non-portable, and error prone.
Better to use a PreparedStatement when using timestamps as I suggested earlier