Good day everyone! I have a problem regarding my “date”. It needs to be parsed but I don’t know how to do it. Can someone help me by suggesting fixes on my code?
<%
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>
<%
}
%>
For the result and error in Console:
5
Tue May 01 00:00:00 CST 2012
java.sql.SQLException: 3 values for 2 columns
I found this but I dont know how to implement this on my codes.
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Im using Eclipse Indigo, Sqlite, and Apache Tomcat
Insert Statement (Im using 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;
}
I will recommend to use
instead of
And also, I suspect that you have table absences with 3 columns, but you try to insert only 2 values. If one of values from table is id, then I suspect this value is not autogenerated.
Hope it will help you.