bananabreadbob
bananabreadbob

Reputation: 407

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: ' '

I have spent the last hour trying alternate versions of inserting date and time via time stamp into a sql database using java.

Date endDate = new Date();    
ptmt.setTimestamp(5, new java.sql.Timestamp(endDate.getTime()));

My create table statement is

CREATE TABLE Location (
  UserID varchar(50),
  Latitude DOUBLE,
  Longitude DOUBLE,
  Altitude DOUBLE,
  TimeInserted timestamp
);

Are there alternative ways of entering the date.

In my prepared statement I tried to have now() within the string, but it looked for the parameter.

String queryString = "INSERT INTO Location(UserID,Latitude,Longitude,Altitude,TimeInserted) VALUES(?,?,?,?,now());";

however it wasn't happy when searching for the 5th parameter.

Any ideas?

I tried making the table date and managed to insert simply the date - however I need the time to order by.

Thanks

Upvotes: 0

Views: 2601

Answers (2)

K K
K K

Reputation: 99

Things to remember

public Timestamp(int year, int month, int date, int hour, int minute, int second, int nano)

Deprecated. instead use the constructor Timestamp(long millis)

Date endDate= new Date();
java.sql.Timestamp eDate = new java.sql.Timestamp(enddate.getTime());
long endDatetime= eDate.getTime();

ptmt.setTimestamp(5, endDatetime);

Try this.

Upvotes: 0

Ramanlfc
Ramanlfc

Reputation: 8354

however it wasn't happy when searching for the 5th parameter.

String queryString = "INSERT INTO Location(UserID,Latitude,Longitude,Altitude,TimeInserted) VALUES(?,?,?,?,now());";

Your query string has 4 bound parameters but this statement ptmt.setTimestamp(5, new java.sql.Timestamp(endDate.getTime())); is trying to bind a long to the 5th parameter that doesn't exist

Upvotes: 1

Related Questions