Reputation: 157
Overview
I made a controller where I pass Dao through autowire and execute a query using jdbctemplate.
The controller calls the method made in dao to retrieve a table from the database.
Code
Controller
@Controller
public class ManagerRoles {
@Autowired
EmployeeDao dao;
@RequestMapping("getEmployeeSchedule/{empID}")
public ModelAndView employeeData(@PathVariable("empID")int empID,HttpServletRequest req,HttpServletResponse res,ModelMap model){
long millis=System.currentTimeMillis();
Date dateStart = new Date(millis);
Date dateFinal = new Date(dateStart.getYear(),dateStart.getMonth(),dateStart.getDate()+30);
System.out.println(dateStart);
System.out.println(dateFinal);
List<EmployeeHolidays> holidayList = dao.retrieveHolidays(dateStart, dateFinal);
if(holidayList!=null){
model.put("holidayList",holidayList);
}
System.out.println(holidayList);
return null;
}
}
DAO
public List<EmployeeHolidays> retrieveHolidays(Date startDate,Date endDate){
String sql = "SELECT * FROM HOLIDAYS WHERE DATE >= ? AND DATE <= ? ";
List<EmployeeHolidays> list = template.query(sql ,new RowMapper<EmployeeHolidays>(){
public EmployeeHolidays mapRow(ResultSet rs,int rownumber) throws SQLException{
EmployeeHolidays e = new EmployeeHolidays();
e.setDate(rs.getDate(1));
e.setReason(rs.getString(2));
e.setStatus(rs.getString(3));
return e;
}
});
return list;
}
The problem is that when I try to run the webapp I get the following error when I try to retrieve the table data.
Type Exception Report
Message Request processing failed; nested exception is org.springframework.dao.ConcurrencyFailureException: StatementCallback; SQL [SELECT * FROM HOLIDAYS WHERE DATE >= ? AND DATE <= ? ]; At least one parameter to the current statement is uninitialized.; nested exception is java.sql.SQLTransactionRollbackException: At least one parameter to the current statement is uninitialized.
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.ConcurrencyFailureException: StatementCallback; SQL [SELECT * FROM HOLIDAYS WHERE DATE >= ? AND DATE <= ? ]; At least one parameter to the current statement is uninitialized.; nested exception is java.sql.SQLTransactionRollbackException: At least one parameter to the current statement is uninitialized. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
I checked the parameters that I am passing and both of them have values.
I don't seem to find the source of the problem. Can anyone help me with this?
Upvotes: 0
Views: 247
Reputation: 4461
You forgot to add the parameters
as arguments
to the query method, look here:
public List<EmployeeHolidays> retrieveHolidays(Date startDate,Date endDate){
String sql = "SELECT * FROM HOLIDAYS WHERE DATE >= ? AND DATE <= ? ";
List<EmployeeHolidays> list = template.query(
sql ,
new Object[] { startDate, endDate} //add this
new RowMapper<EmployeeHolidays>(){
public EmployeeHolidays mapRow(ResultSet rs,int rownumber) throws SQLException{
EmployeeHolidays e = new EmployeeHolidays();
e.setDate(rs.getDate(1));
e.setReason(rs.getString(2));
e.setStatus(rs.getString(3));
return e;
}
});
return list;
}
Upvotes: 2