SetDate « Date « Java Database Q&A

1. SetDate with Statement?

With PreparedStatement, you just call SetDate and be done with it. How do you pass in a Date with a regular Statement?

2. JDBC Prepared Statement . setDate(....) doesn't save the time, just the date.. How can I save the time as well?

I have the following Query :

INSERT INTO users (user_id, date_created) VALUES (?,?)
I have the following prepared statement
PreparedStatement insertUser = dbConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

insertUser.setInt(1, 7);
java.util.Date now = new java.util.Date(System.currentTimeMillis());
insertUser.setDate(2, new java.sql.Date((new Date(System.currentTimeMillis())).getTime()));
If i check ...

4. doubts in setDate(i,date,cal) API of preparedStatement

What error are you getting ? Check out my code below /** * Author Ujjwal B Soni * Code Added : 29th December 2008 * Last Modified 31st July 2009 */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class DemoPreparedStatementSetDate { public static java.sql.Date getCurrentJavaSqlDate() { java.util.Date today = new java.util.Date(); return new java.sql.Date(today.getTime()); } public static Connection getConnection() throws Exception ...