大家好!我的“日期”有问题。它需要被解析,但我不知道该怎么做。可以通过在我的代码上建议修复来帮助我吗?Java中的解析日期
<%
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靛蓝,SQLite的,和Apache Tomcat
插入语句(IM使用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;
}
我认为你的sql是错误的。您为2列插入3个值。向我们展示插入查询。 – 2012-04-01 07:22:59
我知道这并不是100%的答案,但只要您需要使用Java中的日期,请亲自动手并使用Joda Time。 http://joda-time.sourceforge.net/ – moodywoody 2012-04-01 07:23:19
我已经包含了插入语句。 – Zark 2012-04-01 07:28:40