简体   繁体   English

在MySQL中从Java插入datetime字符串

[英]Insert datetime string from java in MySQL

Hi all i have a problem in a insert query. 大家好,我在插入查询中遇到问题。

I get datetime from java with: 我从java获取日期时间:

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String strDate = sdfDate.format(now);

ps: I have try a sysout on strDate and it is correct. ps:我在strDate上尝试了sysout,它是正确的。

I read a question, like that, where to enter in MySQL datetime must make a query like this: 我读了一个问题,像这样,在MySQL日期时间输入哪里必须进行如下查询:

INSERT INTO tracks (in) VALUES (STR_TO_DATE( ? ,'%Y-%m-%d %r')) INSERT INTO跟踪(输入)值(STR_TO_DATE(?,'%Y-%m-%d%r'))

Where '?' 哪里? is the string containing the date, but it don' t work. 是包含日期的字符串,但是不起作用。

Can anyone help me? 谁能帮我?

UPDATE: MySQL Server 5.7 更新:MySQL服务器5.7

You have use current time. 您已使用当前时间。 So you can do it simply with NOW(). 因此,您可以使用NOW()轻松完成此操作。

INSERT INTO tracks (`in`) VALUES (NOW())

OR 要么

You can do this way 你可以这样

java.sql.Timestamp now = new java.sql.Timestamp(new java.util.Date().getTime());
PrepStmt.setTimestamp(1, now);

I have found the problem. 我发现了问题。

The name "in" for the DateTime field is not accepted. 不接受DateTime字段的名称“ in”。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'in, session) VALUES (1, NOW(), "abc")' at line 1.

'in, session) '在会话中)

I have try to rename it in "indate" and the query: 我尝试在“ indate”和查询中重命名它:

INSERT INTO tracks (fk_utenza, indate, session) VALUES (1, NOW(), "abc");

works well! 效果很好!

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

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