gpt4 book ai didi

java - 数据截断:Incorrect value: error while inserting Date field to Mysql DB

转载 作者:行者123 更新时间:2023-11-29 07:29:42 25 4
gpt4 key购买 nike

大家好,我是新手。我试图将两个日期字段插入 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)

最佳答案

这是因为 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 查询将具有格式为 %d/%m/%YSTR_TO_DATE 函数,并让 MySQL 进行日期格式化。

关于java - 数据截断:Incorrect value: error while inserting Date field to Mysql DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33112054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com