java - 嵌套异常是 java.sql.SQLTransactionRollbackException : At least one parameter to the current statement is uninitialized

标签 java spring eclipse spring-mvc

概述

我制作了一个 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)

enter image description 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;
    }

关于java - 嵌套异常是 java.sql.SQLTransactionRollbackException : At least one parameter to the current statement is uninitialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219247/

相关文章:

java - 使用 Java 将 Spark DataFrame 中的数组转换为 DenseVector

mysql - 1 个查询的 SQL 3 关系

java - Spring @Transactional 持久方法不起作用

android - 如何在 Eclipse 中复制或保存 Lint 警告

android - getActionView 使用 searchview 返回 null

java - 在每次出现另一个修复字符时插入一个修复字符

java - 如何对 JOptionPane.showMessageDialog 的 OK 执行操作

java - GAE :Process terminated because the backend took too long to shut down in backends job

java - 通过Spring框架获取依赖图(逆图)

c - Eclipse 编辑器中多行 C/C++ 宏的快捷方式