简体   繁体   中英

Insert Date in SQL with JdbcTemplate

What is the best way to insert a date in MySQL using JdbcTemplate ?

I've seen different approaches, but none of them uses JdbcTemplate (They are using DriverManager.getConnection() ) and I want to use it only.

Currently, I'm using just Date class from java.sql.Date and although it does not throw an error, it inserts something weird instead.

use java.util.Date, it will work fine.

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql="insert into order(id, name, createDate) values(?,?,?)"; 
int rows=jdbcTemplate.update(query, new Object[]{id, name, new java.util.Date()});
log.info("rows updated {}", rows);

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