大家好!我对我的“约会”有疑问。它需要解析,但我不知道该怎么做。有人可以通过建议修复我的代码来帮助我吗?
<%
String format = "yyyy-MM-dd";
DateFormat df = new SimpleDateFormat(format);
String faculty = request.getParameter("faculty");
String absent_date = request.getParameter("absent_date");
int intFaculty = Integer.parseInt(faculty);
System.out.println(faculty);
System.out.println(df.parse(absent_date));
java.sql.Date absentdate = new java.sql.Date(df.parse(absent_date).getDate());
Absences abs = new Absences();
abs.setFaculty_id(intFaculty);
abs.setAbsent_date(absentdate);
int result = AbsencesImpl.AddAbsences(abs);
if (result == 0)
{
%>
<jsp:forward page="ListAbsences.jsp">
<jsp:param name="msg" value="Record Successfully Added"/>
<jsp:param name="ret" value="meron"/>
</jsp:forward>
<%
}
%>
控制台中的结果和错误:
5
Tue May 01 00:00:00 CST 2012
java.sql.SQLException: 3 values for 2 columns
我发现了这个,但我不知道如何在我的代码上实现它。
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
我使用 Eclipse Indigo、Sqlite 和 Apache Tomcat
插入语句(我使用 MVC)
public int AddAbsences(Absences abs)
{
Connection con = null;
PreparedStatement pstmt;
ConnDB conn = new ConnDB();
String sql = "INSERT INTO absences( faculty_id, absent_date) VALUES(?, ?)";
try{
con = conn.connect();
try{
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, abs.getFaculty_id());
pstmt.setDate(2, abs.getAbsent_date());
pstmt.executeUpdate();
}catch(Exception e){
System.out.println(e);
}
con.close();
}catch(Exception e){
System.out.println(e);
}
return 0;
}
最佳答案
我会推荐使用
df.parse(absent_date).getTime()
而不是
df.parse(absent_date).getDate()
而且,我怀疑您的表缺少 3 列,但您尝试仅插入 2 个值。如果表中的值之一是 id,那么我怀疑该值不是自动生成的。
希望对您有帮助。
关于java - Java 中的日期解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9962634/