简体   繁体   中英

when inserting date and time in datetime column in mysql database it will insert date but time is 00-00-00

Calendar cal = Calendar.getInstance();

PreparedStatement   pstmt = conn.prepareStatement("insert into organisation (org_id,name,description,logo,created_date,created_by,is_status) values(?,?,?,?,?,?,?)");
pstmt.setInt(1, 1);
pstmt.setString(2, name);
pstmt.setString(3, description);
pstmt.setDate(4, new java.sql.Date(cal.getTimeInMillis()));

int count = pstmt.executeUpdate();

See the javadoc of java.sql.Date :

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value.

If you see the date type only repesents the date not the time part. You have to use java.sql.Timestamp . Here is teh javadoc .

thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value

So your code should look like:

pstmt.setTimestamp(4, new java.sql.Timestamp(cal.getTimeInMillis()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM