大家好我是新来的。 我试图向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在各个阶段检查的日期的值。但在插入数据库时,它将更改为“2015年10月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)
答案 0 :(得分:1)
这是因为newStartDate
和newEndDate
是Date
对象,每当你用String
连接任何java对象时,java都会调用对象&# 39; s 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
的{{1}}函数,并让MySQL进行日期格式化。
答案 1 :(得分:1)
如果您使用varchar在数据库中存储日期,则数据截断错误发生在Date newStartDate=format.parse(startDate)
,因为您正在使用默认格式获取日期。
如果您使用日期类型来存储日期,那么您可以使用str_to_date函数将日期作为字符串发送并在mysql中格式化。
STR_TO_DATE('10/14/2015', '%m/%d/%Y)
答案 2 :(得分:0)
afaik MySQL使用yyyy-MM-dd作为默认的日期字符串格式。 您可以尝试将日期字符串更改为该格式吗?
顺便说一句,最好在SQL中使用“STR_TO_DATE”进行显式转换: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);
答案 3 :(得分:0)
尝试以下方法,
String startDate = dto.getStartDate();
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String newStartDate = format.format(format.parse(startDate));
我认为为Date sql字段传递字符串应该可以正常工作