【问题标题】:Data Truncation :Incorrect value: error while inserting Date field to Mysql DB数据截断:不正确的值:将日期字段插入 Mysql DB 时出错
【发布时间】:2026-02-14 01:10:01
【问题描述】:

大家好,我是新手。 我试图将两个日期字段插入 Mysql .PFB // DAO类方法添加字段。所有字段都来自jsp表单。

 public boolean addIssue(Connection conn, IssueDTO dto)throws ParseException{

    String startDate=dto.getStartDate();
    String endDate=dto.getStartDate();
    System.out.println("the start date from dto--"+dto.getStartDate());
    System.out.println("The end date from dto"+dto.getEndDate());
    DateFormat  format=new SimpleDateFormat("MM/dd/yyyy");

    Date newStartDate=format.parse(startDate);
    Date  newEndDate=format.parse(endDate);


  try{
        String sql="INSERT INTO issue_description (issue,keyword,applicationName,objectName,teamName,startDate,endDate,resolution,priority) values (?,?,?,?,?,?,?,?,?)";
        PreparedStatement statement=conn.prepareStatement(sql);

        statement.setString(1, ""+dto.getIssue());
        statement.setString(2, ""+dto.getKeyword());
        statement.setString(3, ""+dto.getApplicationName());
        statement.setString(4, ""+dto.getObjectName());
        statement.setString(5, ""+dto.getTeamName());
        statement.setString(6, ""+newStartDate);
        statement.setString(7, ""+newEndDate);
        statement.setString(8, ""+dto.getResolution());
        statement.setString(9, ""+dto.getPriority());

        statement.executeUpdate();

下面是错误日志:::前四行是来自jsp在各个阶段检查的日期值。但是在插入数据库时​​,它会更改为“Wed Oct 14 00:00:00 IST 2015”。请建议这里发生的事情。

 10/14/2015
10/14/2015
the start date fr om dto--10/14/2015
The end date from dto10/14/2015
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value:         'Wed Oct 14 00:00:00 IST 2015' for column 'startDate' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2983)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at  com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at com.sm.dao.IssueDAO.addIssue(IssueDAO.java:65)
at com.controllers.AddIssueController.doPost(AddIssueController.java:72)

【问题讨论】:

    标签: java mysql date


    【解决方案1】:

    这是因为newStartDatenewEndDateDate 对象,并且每当您将任何java 对象与String 连接时,java 将调用对象的toString() 方法来获取对象的字符串表示形式。

    这是你的犯罪方代码:

        statement.setString(6, ""+newStartDate);
        statement.setString(7, ""+newEndDate);
    

    这是Date.toString()的格式:

    Converts this Date object to a String of the form: 
    
     dow mon dd hh:mm:ss zzz yyyy
    where:
    
    •dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat). 
    •mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec). 
    •dd is the day of the month (01 through 31), as two decimal digits. 
    •hh is the hour of the day (00 through 23), as two decimal digits. 
    •mm is the minute within the hour (00 through 59), as two decimal digits. 
    •ss is the second within the minute (00 through 61, as two decimal digits. 
    •zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all. 
    •yyyy is the year, as four decimal digits. 
    

    我不会将日期字符串转换为对象。相反,我的 SQL 查询将有一个 STR_TO_DATE 函数,格式为 %d/%m/%Y,并让 MySQL 进行日期格式化。

    【讨论】:

    • 谢谢大家。目前问题已解决。我只是将表中的日期字段更改为“varchar”。并将值作为字符串插入。
    • @imsolo,请不要忘记接受有帮助的答案。
    【解决方案2】:

    如果您使用 varchar 将日期存储在数据库中,则在 Date newStartDate=format.parse(startDate) 处会出现数据截断错误,因为您获取的是默认格式的日期。

    如果您使用日期类型来存储日期,那么您可以将日期作为字符串发送并使用 str_to_date 函数在 mysql 中格式化。

    STR_TO_DATE('10/14/2015', '%m/%d/%Y)
    

    【讨论】:

      【解决方案3】:

      试试下面的,

      String startDate = dto.getStartDate();
      DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
      String newStartDate = format.format(format.parse(startDate));
      

      我相信为 Date sql 字段传递字符串应该可以工作

      【讨论】:

        【解决方案4】:

        afaik MySQL 使用 yyyy-MM-dd 作为默认日期字符串格式。 您可以尝试将日期字符串更改为该格式吗?

        顺便说一句,最好使用此处描述的“STR_TO_DATE”在 SQL 中进行显式转换:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

        我没有先看到它:

        newStartDate 和 newEndDate 是日期对象。如果这些对象来自 java.sql.Date 类型,那么它是传递它的最佳方式:

        statement.setDate(6, newStartDate);
        statement.setDate(7, newEndDate);
        

        【讨论】: