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

问题描述 投票:1回答:2

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

java.sql.SQLException: Result set after last row

我在这做错了什么?

  @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");



    }

我的RequestDao是:

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

here上看到类似的错误,但是他们的代码与我的不同。

java jdbc
2个回答
0
投票

这可能是因为没有获取任何行。 ResultSet#next返回boolean值,表示是否存在行。

您需要申请的是检查这一点的条件。 因为你需要一排,if非常适合。

if (rs.next()) {
   ...
   requestStatus = rs.getString("STATUS");
   ...
}

请注意,可以通过应用DBMS相关关键字来优化查询,例如LIMIT用于MySQL

SELECT STATUS FROM Mytable WHERE request_user= ? and app_name =? and request_id=? LIMIT 1

0
投票

你应该问一下resultset的值是否有一段时间

while(rs.next){ //your code here }

这样你就不会陷入错误,只是跳过返回

© www.soinside.com 2019 - 2024. All rights reserved.