嵌套异常为java.sql.SQLTransactionRollbackException:当前语句的至少一个参数未初始化

时间:2018-09-07 09:13:24

标签: java spring eclipse spring-mvc

概述

我做了一个控制器,在其中我通过自动连线传递Dao并使用jdbctemplate执行查询。

控制器调用dao中的方法从数据库中检索表。

代码

控制器

@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应用程序时,尝试检索表数据时出现以下错误。

  

类型异常报告

     

消息请求处理失败;嵌套异常为   org.springframework.dao.ConcurrencyFailureException:   StatementCallback; SQL [SELECT * FROM HOLIDAYS WHERE DATE> =?和   DATE <=? ];当前语句的至少一个参数是   未初始化。嵌套异常为   java.sql.SQLTransactionRollbackException:至少一个参数   当前语句尚未初始化。

     

说明服务器遇到意外情况,   阻止它满足请求。

     

例外

     

org.springframework.web.util.NestedServletException:请求   处理失败;嵌套异常为   org.springframework.dao.ConcurrencyFailureException:   StatementCallback; SQL [SELECT * FROM HOLIDAYS WHERE DATE> =?和   DATE <=? ];当前语句的至少一个参数是   未初始化。嵌套异常为   java.sql.SQLTransactionRollbackException:至少一个参数   当前语句未初始化。     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)

enter image description here

我检查了我要传递的参数,它们都有值。

我似乎找不到问题的根源。有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

您忘记将parameters作为arguments添加到查询方法中,请看这里:

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;
    }