tags:

views:

60

answers:

5

hi,

I used the following code, but the DateTime field at sql is 2005-04-08 00:00:00

I want to have the time too. where should i change

... ...

// Get the system date and time.
java.util.Date utilDate = new Date();
// Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());
...
...
...
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setDate(1, date);
A: 

Try using setDate(utilDate) instead of trying to set SQL date. In SQL, date and time are different data types.

Reddy
+3  A: 

Try using setTimestamp instead of setDate, since a timestamp is the prefered format for data and time.

perdian
java.sql.Date only supports the date, not time.From the javadoc - "To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated."
Harry Lime
A: 

Try this:

Connection con;
Statement stmt;
con = DriverManager.getConnection(url, username, password);
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO MYTABLE(time) " +
"VALUES ('" + new java.sql.Date(System.CurrentTimeMillis())"');");
Truong Ha
+1  A: 

Use the sql function now().

INSERT INTO table date_field VALUES(NOW());
M42
A: