概述
我制作了一个 Controller ,通过 Autowiring 传递 Dao 并使用 jdbctemplate 执行查询。
Controller 调用dao中的方法从数据库中检索表。
代码
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;
}
问题是,当我尝试运行 web 应用程序时,当我尝试检索表数据时,出现以下错误。
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)
我检查了我传递的参数,它们都有值。
我似乎没有找到问题的根源。谁能帮我解决这个问题吗?
最佳答案
您忘记将参数
作为参数
添加到查询方法中,请查看此处:
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;
}
关于java - 嵌套异常是 java.sql.SQLTransactionRollbackException : At least one parameter to the current statement is uninitialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219247/