1

I'd much appreciate any help on this I have tried all sorts with this.

Basically I have an SQL database, which I want to save some data to, I know how to do this with all the other fields I have, but I also want to save the current time with this data.

str = "INSERT INTO tblResults (username, curTime, outcome) " +
"VALUES(" + "'" + user + "'" +", " + (new Timestamp(date.getTime())) + ", " + "'" +     outcome + "'" +")";

and then I execute the string, this was an example of one of my many attempts, this attempt compiles, but I get an error:

Incorrect syntax near '16'. Error saving result

16 being the hour right now... I think its because a time stamp puts the time before the date and an sql does the opposite, but I have no idea on how to fix this, so I'd appreciate any help. Thanks

5
  • 1
    stackoverflow.com/questions/2858182/… Commented Feb 20, 2013 at 16:22
  • 1
    Timestamp (java) usually differs from TIMESTAMP in your database. What data type did you specify in your database for the field? Commented Feb 20, 2013 at 16:23
  • 2
    As a suggestion, never use Statement, instead use PreparedStatement Commented Feb 20, 2013 at 16:23
  • How is the date formatted? Have you considered parameterizing the statement? That would save you all the trouble of converting the date/time to a string and back. Commented Feb 20, 2013 at 16:24
  • Yeah I did try other ways, that was just trying every possibility because I was just lost. I specified Time Commented Feb 20, 2013 at 16:24

4 Answers 4

2

Use a PreparedStatement and it will sort it out for you:

    final String str = "INSERT INTO tblResults (username, curTime, outcome)  VALUES( ?, ?, ?)";
    final PreparedStatement preparedStatement = connection.prepareStatement(str);
    preparedStatement.setString(1, user);
    preparedStatement.setTimestamp(2, new Timestamp(date.getTime()));
    preparedStatement.setString(3, outcome);

I guessed at your other data types...
As others have said, use of Statement is not recommended.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I did try a prepared statement the other day, I can't remember what problem I had, but I will definitely try it again.
0
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
str = "INSERT INTO tblResults (username, curTime, outcome) " +
"VALUES(" + "'" + user + "'" +", " + dateFormat.format(date) + ", " + "'"  +     outcome + "'" +")";

Comments

0

You miss apostrophes around new Timestamp() in your SQL query, but you really should use PreparedStatement instead for this case.

1 Comment

I have no idea how I missed the apostrophes, probably all the deleting and retyping, thanks, I did try a prepared statement the other day, I don't know why I gave up on it, thanks I will try again.
0

The error is coming up because the reference value of new Timestamp(date.getTime()) is going into the string instead of correct timestamp formated string.

replace with this and then execute : str = "INSERT INTO tblResults (username, curTime, outcome) " + "VALUES(" + "'" + user + "'" +", " + (new Timestamp(date.getTime()).toString() + ", " + "'" + outcome + "'" +")";

if you do not put the toString() method then the reference address of the object will go into the string.

You can also print the whole string in the Logger to see what is actually going into str

4 Comments

Thanks, I tried this but I get this error, any idea? The statement did not return a result set. Error saving result
are you using executeUpdate() method to insert the record? use this statement if you are not using this one
thank you very much, I was using executeQuery(); executeUpdate() has fixed it, looks like it has at least saved correctly, so at least now I have something that works before I can make it a little better. thanks again.
@user2092020 I am glad I solved your problem and answered your question. Please accept my answer. Many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.