Java 数据截断:日期时间值不正确:''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20088808/
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
Data truncation: Incorrect datetime value: ''
提问by user2951465
Can anyone help me with a sample JSP code to store date in a MySql database through JDBC? When I try to execute the code given below, I get the following exception:
任何人都可以用示例 JSP 代码帮助我通过 JDBC 在 MySql 数据库中存储日期吗?当我尝试执行下面给出的代码时,出现以下异常:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'date' at row 1
com.mysql.jdbc.MysqlDataTruncation:数据截断:日期时间值不正确:“日期”列在第 1 行
How to overcome this problem? Following is my code:
如何克服这个问题?以下是我的代码:
Connection con = null;
String StaffName = request.getParameter("StaffName");
// String subcode = request.getParameter("subcode");
String hourId = request.getParameter("hourId");
if (hourId == null)
hourId = "";
String day = request.getParameter("day");
if (day == null)
day = "";
String date = request.getParameter("date");
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation", "root", "success");
// PreparedStatement stat = con.PrepareStatement();
String updateString = "INSERT INTO tblstaffallocation (StaffName,hourId,daysId,date) VALUES (?,?,?,?)";
PreparedStatement preparedStatement = con.prepareStatement(updateString);
preparedStatement.setString(1, StaffName);
preparedStatement.setInt(2, 0);
preparedStatement.setInt(3, 0);
preparedStatement.setString(4, date);
} catch (Exception e) {
out.print(e);
}
采纳答案by MartenCatcher
To set date to prepared statement you need change type of value:
要将日期设置为准备好的语句,您需要更改值的类型:
String date = request.getParameter("date");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // your template here
java.util.Date dateStr = formatter.parse(date);
java.sql.Date dateDB = new java.sql.Date(dateStr.getTime());
now convert String date to java.sql.Date and use another method:
现在将字符串日期转换为 java.sql.Date 并使用另一种方法:
preparedStatement.setDate(4,dateDB);
回答by sumitsabhnani
Try reformating the date
尝试重新格式化日期
String date = new SimpleDateFormat("yyyy-MM-dd")
.format(new Date(request.getParameter("date")));
and then insert into the database. Note that request.getParameter("date") should be in format 11/20/2013 for this to work or you can use a similar way to achieve.
然后插入到数据库中。请注意, request.getParameter("date") 应采用 11/20/2013 格式才能使其工作,或者您可以使用类似的方式来实现。
回答by Kimball Robinson
I had a similar error. It turns out I just needed to update the jar version for mysql-connector-java (using maven)
我有一个类似的错误。原来我只需要更新 mysql-connector-java 的 jar 版本(使用 maven)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>...</version>
</dependency>
回答by Serhii Maksymchuk
If someone will have similar error for entity field with Data type annotated as @Temporal
, the solution for me was to replace annotation value TemporalType.TIMESTAMP
by TemporalType.TIME
:
如果有人对数据类型注释为 的实体字段有类似的错误,@Temporal
我的解决方案是将注释值替换TemporalType.TIMESTAMP
为TemporalType.TIME
:
@Temporal(TemporalType.TIMESTAMP)
private Date dateField;
should be like this:
应该是这样的:
@Temporal(TemporalType.TIME)
private Date dateField;
Another way to resolve this problem without any changes in code (at least for me) was to run application on higher Tomcat version, hope it will help.
在不更改代码的情况下(至少对我而言)解决此问题的另一种方法是在更高的 Tomcat 版本上运行应用程序,希望它会有所帮助。
回答by Anantha Raju C
回答by jtcotton63
I know this is an old thread, but none of these solutions solved the problem for me. What worked for me was to upgrade hibernate to version 5.2.10.Final (see this SO post).
我知道这是一个旧线程,但这些解决方案都没有为我解决问题。对我有用的是将 hibernate 升级到版本 5.2.10.Final(请参阅此 SO 帖子)。
Running Spring Boot and Spring Data JPA 1.5.4.RELEASE and hibernate 5.2.10.Final.
运行 Spring Boot 和 Spring Data JPA 1.5.4.RELEASE 和 hibernate 5.2.10.Final。