Java JDBC:将日期值插入 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21872040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:11:42  来源:igfitidea点击:

JDBC: Inserting Date values into MySQL

javamysqlsqldatejdbc

提问by Lemon Juice

I would like to know how to set Date values to MySQL database, using Java JDBC. Following is my code.

我想知道如何使用 Java JDBC 将日期值设置为 MySQL 数据库。以下是我的代码。

 String lastCrawlDate = "2014-01-28"
 PreparedStatement p = con.prepareStatement("insert into JsonData (last_crawl_date) values(?))");

 p.setDate(1, Date.valueOf(lastCrawlDate));

As you can see, the lastCrawlDateis a Stringand in the format of yyyy/mm/dd.

如您所见,lastCrawlDateString和 格式为yyyy/mm/dd.

Following is how my table looks like and how the Datefield is defined.

以下是我的表的外观以及Date字段的定义方式。

enter image description here

在此处输入图片说明

How can I do this?

我怎样才能做到这一点?

采纳答案by Sotirios Delimanolis

PreparedStatementprovides the methods setDate(..), setTimestamp(..), and setTime(..)to replace placeholders (?) with date type fields. Use them

PreparedStatement提供方法setDate(..)setTimestamp(..)和用日期类型字段setTime(..)替换占位符 ( ?)。使用它们

String lastCrawlDate = "2014-01-28";
Date utilDate = new SimpleDateFormat("yyyy-MM-dd").parse(lastCrawlDate);
// because PreparedStatement#setDate(..) expects a java.sql.Date argument
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); 

Connection con = ...;
PreparedStatement p = con.prepareStatement("insert into JsonData (last_crawl_date) values(?))");
p.setDate(1, sqlDate);

回答by Lemon Juice

Immediately insert your date as a string

立即将您的日期作为字符串插入

p.setString(1, lastCrawlDate);

Exactly as

正如

insert into Table1 (last_crawl_date) values ('2014-01-28');

MySQL recognizes DATE values in these formats:

  • As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31', '2012/12/31', '2012^12^31', and '2012@12@31' are equivalent.

  • As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date. For example, '20070523' and '070523' are interpreted as '2007-05-23', but '071332' is illegal (it has nonsensical month and day parts) and becomes '0000-00-00'.

  • As a number in either YYYYMMDD or YYMMDD format, provided that the number makes sense as a date. For example, 19830905 and 830905 are interpreted as '1983-09-05'.

MySQL 识别以下格式的 DATE 值:

  • 作为 'YYYY-MM-DD' 或 'YY-MM-DD' 格式的字符串。允许使用“宽松”语法:任何标点字符都可以用作日期部分之间的分隔符。例如,“2012-12-31”、“2012/12/31”、“2012^12^31”和“2012@12@31”是等价的。

  • 作为没有分隔符的字符串,格式为“YYYYMMDD”或“YYMMDD”,前提是该字符串作为日期有意义。例如,'20070523' 和 '070523' 被解释为 '2007-05-23',但 '071332' 是非法的(它有无意义的月份和日期部分)并变成 '0000-00-00'。

  • 作为 YYYYMMDD 或 YYMMDD 格式的数字,前提是该数字作为日期有意义。例如,19830905 和 830905 被解释为“1983-09-05”。

Using this approach, you can write a method to obtain a string from a given Date:

使用这种方法,您可以编写一个方法来从给定日期获取字符串:

public static String getString(Date d) {
   return new SimpleDateFormat("yyyy-MM-dd").format(d);
}

回答by Abdul Manaf

Do like

喜欢

insert into JsonData (last_crawl_date) values(DATE_FORMAT('2014/02/19','%Y-%m-%d'));