java.sql.SQLException:最后一行之后的结果集

java.sql.SQLException:最后一行之后的结果集,java,jdbc,Java,Jdbc,我认为我有问题,因为这行rs下;在下面提到的RequestDaoImpl类中。每当我尝试检索STATUS的值时,我总是会遇到以下错误: java.sql.SQLException:最后一行之后的结果集 我做错了什么 @Component public class GetStatus { @JmsListener(destination = "Queue1") public void processStatusMessage(String messag

我认为我有问题,因为这行rs下;在下面提到的RequestDaoImpl类中。每当我尝试检索STATUS的值时,我总是会遇到以下错误:

java.sql.SQLException:最后一行之后的结果集

我做错了什么

  @Component
    public class GetStatus {

        @JmsListener(destination = "Queue1")
        public void processStatusMessage(String message) throws DaoException {

            System.out.println("Message Retrieved is:" +message);

            try {

            RequestDao requestDao = (RequestDao) context.getBean("requestDao");

            String receivedStatus = requestDao.getRequestStatus(message);

            System.out.println("Testing March 11");
            System.out.println(receivedStatus);



            }
            catch(Throwable th){
                th.printStackTrace();   

            }

         }

        private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");



    }
我的要求是:

public interface RequestDao {

    public String getRequestStatus(String msg)throws DaoException;

}
我的RequestDaoImpl与方法实现:

public class RequestDaoImpl implements RequestDao {

    public void setDataSource(DataSource dataSource) 
    {       
        jdbcTemplate = new JdbcTemplate(dataSource);                                    
    }

    @Override
    public String getRequestStatus(String msg) throws DaoException {
        DataSource ds = null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String requestStatus = null;

        //List<String> mylist = new ArrayList<String>();

         try {

                ds = jdbcTemplate.getDataSource();
                conn = ds.getConnection();  

                //I am receiving message like this hence splitting it : 123456#Tan#development
                 String[] parts =   msg.split("#");
                 String requestID = parts[0].trim();
                 String userName =  parts[1].trim();
                 String applicationName = parts[2].trim();


                /*===========================================================================*/
                /*    Code to get the request status from Mytable          */ 
                /*===========================================================================*/
                pstmt = conn.prepareStatement("SELECT STATUS FROM Mytable WHERE request_user= ? and app_name =? and request_id=?");
                pstmt.setString(1,userName);
                pstmt.setString(2,applicationName);
                pstmt.setString(3, requestID);
                rs = pstmt.executeQuery();  
                rs.next();
                System.out.println("The status received is as follows:");

                requestStatus = rs.getString("STATUS");
                System.out.println(requestStatus);



        }
         catch(Throwable th) {
                throw new DaoException(th.getMessage(), th);
            }
            finally {
                if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }}
                if (pstmt != null) { try { pstmt.close(); } catch(SQLException sqe) { sqe.printStackTrace(); }}
                if (conn != null) { try { conn.close(); } catch (SQLException sqle) { sqle.printStackTrace(); }}

            }   



        return requestStatus;
    }
  private JdbcTemplate jdbcTemplate;    
 }

看到类似的错误帖子,但他们的代码与我的不同。

您应该询问resultset是否有值,可能需要一段时间

惠勒,下一个{ //你的代码在这里 }

这样,您就不会出现错误,只需跳转到return,这可能会发生,因为没有获取任何行。 ResultSetnext返回一个布尔值,该值表示行的存在或不存在

你需要申请的是一个检查条件。 因为您需要一行,所以if非常适合

if (rs.next()) {
   ...
   requestStatus = rs.getString("STATUS");
   ...
}
请注意,可以通过应用依赖于DBMS的关键字(如LIMIT for MySQL)来优化查询


嗯,实际上我是这样做的,ifrs.next{System.out.printlnrs.getStringSTATUS;}else{System.out.printlnSomething in resultset;}它总是在resultset中打印出错误的内容。Oracle SQL developer上的SQL查询返回了一行。@这不是错误,只是没有检索到任何行。检查您的SQL语句。您是对的,此SQL不返回任何行。谢谢我想我对我的另一个SQL查询感到困惑,认为它返回了一行。该查询的ID不同。@Tan请记住,您可以通过告诉DBMS仅获取一行来优化查询。就像回答中写的一样。谢谢。说得好。Oracle db的替代方案是什么?我相信限制只适用于MySQL。我相信while循环在我的情况下不起作用,因为我只得到一行?如果你有一行,它会起作用,它会进入一次并完成你的工作,然后离开
SELECT STATUS FROM Mytable WHERE request_user= ? and app_name =? and request_id=? LIMIT 1