问题描述
我正在尝试解析一个字符串并将其转换成一个日期对象,以插入到一个准备的声明。我一直在试图让这个工作,但没有运气。我还会得到有用的错误消息,当我去编译该类。
线程中的异常mainjava.lang.Error:未解决的编译问题:
PreparedStatement类型中的setDate(int,Date)方法不适用于参数(int,Date)
Eh WTF?
这是违规代码。
for(int i = 0; i< flights。 size(); i ++){
String [] details = flight [i] .toString()。split(:);
DateFormat格式化程序
formatter = new SimpleDateFormat(ddMMyyyy);
Date date = formatter.parse(details [1]);
PreparedStatement pstmt = conn.prepareStatement(insertsql);
pstmt.setString(1,details [0]);
pstmt.setDate(2,date);
pstmt.setString(3,details [2] +00);
pstmt.setString(4,details [3]);
pstmt.setString(5,details [4]);
pstmt.setString(6,details [5]);
pstmt.setString(7,details [6]);
pstmt.setString(8,details [7]);
pstmt.setString(9,details [8]);
pstmt.executeUpdate();
}
采用,而不是 java.util.Date 。
(感兴趣的是,你怎么看不到这是编译时错误?如果您可以解决编译,您的生活会变得更加容易失败而不必在测试运行中达到这一点...)
Anytime I have to handle dates/times in java it makes me sad
I'm trying to parse a string and turn it into a date object to insert in a preparepared statement. I've been trying to get this working but am having no luck. I also get the helpful error message when I go to compile the class.
"Exception in thread "main" java.lang.Error: Unresolved compilation problem:The method setDate(int, Date) in the type PreparedStatement is not applicable for the arguments (int, Date)"
Eh WTF?
Here is the offending code.
for(int i = 0; i < flights.size(); i++){
String[] details = flight[i].toString().split(":");
DateFormat formatter ;
formatter = new SimpleDateFormat("ddMMyyyy");
Date date = formatter.parse(details[1]);
PreparedStatement pstmt = conn.prepareStatement(insertsql);
pstmt.setString(1, details[0]);
pstmt.setDate(2, date);
pstmt.setString(3, details[2] + "00");
pstmt.setString(4, details[3]);
pstmt.setString(5, details[4]);
pstmt.setString(6, details[5]);
pstmt.setString(7, details[6]);
pstmt.setString(8, details[7]);
pstmt.setString(9, details[8]);
pstmt.executeUpdate();
}
PreparedStatement.setDate takes a java.sql.Date, not a java.util.Date.
(Out of interest, how come you're not actually seeing this as a compile-time error? Your life will become a lot easier if you can resolve compilation failures without having to get to that point in a test run...)
这篇关于Java:从字符串创建日期对象并插入到MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!