简体   繁体   中英

insert date from jsp into database

Below code is only a part of the application.First page is Login page when submited, it will go to TimseSheet.jsp page. In Timsesheet.jsp page we need to fill the id and date and save the values to database but it is storing null values in the database table.

Please tell me how to insert date in the table.

In Jsp page:TimseSheet.jsp

 <form action="TimseSheetProcess.jsp" method="post">
 <td><input type="text" name="empid" required="required" /></td>  
 <td><input type="date" name="logindate" required="required" /></td>
 <input type="submit" value="Submit">

In EmployeeBean class:

public class EmployeeBean {
private String empid;
private Date logindate;
 public String getEmp_id() {
    return empid;
}

public void setEmp_id(String empid) {
    this.empid = empid;
}
public Date getLoginDate() {
    return logindate;
}

public void setLoginDate(Date logindate) {
    this.logindate = logindate;
}

} In TimseSheetDao class:

public class TimseSheetDao {
public static int insert(EmployeeBean eb){
int status=0;  
PreparedStatement ps = null;  

ps=conn.prepareStatement("insert into tab values(?,?");  
ps.setString(1,eb.getEmp_id());   
ps.setDate(2,eb.getLoginDate()); 
status=ps.executeUpdate(); 
   }
}

In TimseSheetProcess.jsp:

<%@page import="com.eis.Dao.TimseSheetDao"%>
<jsp:useBean id="obj" scope="session" class="com.eis.bean.EmployeeBean"/>  

<jsp:setProperty property="*" name="obj"/>  
<%  out.print("You are in loop"); 
int status=TimseSheetDao.insert(obj);
if(status>0) { 
out.print("You are successfully registered");  
response.sendRedirect("timsesheet.jsp");
}
else{
out.print("Error");
}
%>  

Thanks

Your are setting the id and the date at the same parameterindex "1" of the preparement statement! That's wrong ....

ps.setString(1,eb.getEmp_id());   
ps.setDate(1,eb.getLoginDate()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM