简体   繁体   English

使用 JdbcTemplate 在 SQL 中插入日期

[英]Insert Date in SQL with JdbcTemplate

What is the best way to insert a date in MySQL using JdbcTemplate ?使用JdbcTemplate在 MySQL 中插入日期的最佳方法是什么?

I've seen different approaches, but none of them uses JdbcTemplate (They are using DriverManager.getConnection() ) and I want to use it only.我见过不同的方法,但没有一个使用JdbcTemplate (他们使用DriverManager.getConnection() ),我只想使用它。

Currently, I'm using just Date class from java.sql.Date and although it does not throw an error, it inserts something weird instead.目前,我只使用java.sql.Date Date类,虽然它没有抛出错误,但它插入了一些奇怪的东西。

use java.util.Date, it will work fine.使用 java.util.Date,它会正常工作。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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