简体   繁体   English

数据截断:错误的日期时间值:“

[英]Data truncation: Incorrect datetime value: ''

Can anyone help me with a sample JSP code to store date in a MySql database through JDBC? 谁能通过示例JSP代码帮助我,以通过JDBC在MySql数据库中存储日期? When I try to execute the code given below, I get the following exception: 当我尝试执行下面给出的代码时,出现以下异常:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'date' at row 1 com.mysql.jdbc.MysqlDataTruncation:数据截断:不正确的datetime值:第1行的'date'列的''

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

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: 现在将String date转换为java.sql.Date并使用另一种方法:

preparedStatement.setDate(4,dateDB);

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>

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”)的格式应为2013年11月20日,否则您可以使用类似的方式来实现。

Make sure that the Date value that you are trying to insert into the table is exactly in the format defined in the date column of your table. 确保您尝试插入表中的“日期”值与表中“日期”列中定义的格式完全相同。

在此处输入图片说明

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版本上运行应用程序,希望它会有所帮助。

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 ). 对我有用的是将休眠模式升级到5.2.10.Final版本(请参阅此SO post )。

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。

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

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