1

Considering this query:

insert (a, b, update_time) values (1, 1, now() - interval 10 second)

Now, I need to convert it to a parameterized statement:

insert (a, b, update_time) values (?, ?, ?) 

The problem is you cannot use SQL function in the parameter. How do I write this kind of code?

2
  • You could calculate it using Java code ? Commented Aug 27, 2010 at 16:51
  • never mind. I got the answer already. Commented Aug 27, 2010 at 17:00

2 Answers 2

2
Date now = new Date();
PreparedStatement ps = connection.prepareStatement("INSERT INTO your_table(a, b, update_time) VALUES(?, ?, ?)");
ps.setObject(1, a);
ps.setObject(2, b);
ps.setDate(3, now);
ps.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

1

There's no need to parameterize the date code:

insert (a, b, update_time) values (?, ?, now() - interval 10 second)

Now it only takes two parameters and the date will be handled on the server. I've had timezone issues between JDBC and MySQL concerning daylight savings time. Be careful!

Comments

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.