Data truncation: Incorrect datetime value: '&#

2019-01-11 20:04发布

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:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'date' at row 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);
}

6条回答
三岁会撩人
2楼-- · 2019-01-11 20:35

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.

enter image description here

查看更多
小情绪 Triste *
3楼-- · 2019-01-11 20:45

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 similar way to achieve.

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-11 20:48

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

查看更多
Bombasti
5楼-- · 2019-01-11 20:53

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

Running Spring Boot and Spring Data JPA 1.5.4.RELEASE and hibernate 5.2.10.Final.

查看更多
forever°为你锁心
6楼-- · 2019-01-11 20:55

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:

preparedStatement.setDate(4,dateDB);
查看更多
萌系小妹纸
7楼-- · 2019-01-11 20:57

I had a similar error. It turns out I just needed to update the jar version for mysql-connector-java (using maven)

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>...</version>
    </dependency>
查看更多
登录 后发表回答