6

I think I am having problem because of this line rs.next(); in the RequestDaoImpl class mentioned below. Whenever I try to retrieve the value of STATUS, I keep on getting the following error:

java.sql.SQLException: Result set after last row

What am I doing wrong here?

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



    }

My RequestDao is :

public interface RequestDao {

    public String getRequestStatus(String msg)throws DaoException;

}

My RequestDaoImpl with the method implementation:

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

Saw similar error post here but their code is different than mine.

2 Answers 2

3

That might happen because no rows were fetched.
ResultSet#next returns a boolean value which represents the presence or absence of a row.

What you'd need to apply is a condition to check that.
Being that you need a single row, an if is perfectly suitable.

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

Note that your query can be optimized by applying a DBMS dependant keyword, such as LIMIT for MySQL

SELECT STATUS FROM Mytable WHERE request_user= ? and app_name =? and request_id=? LIMIT 1
Sign up to request clarification or add additional context in comments.

6 Comments

Hmm, I actually did this if(rs.next()) { System.out.println(rs.getString("STATUS")); }else { System.out.println("Something wrong in the resultset"); } and it always end up printing Something wrong in the resultset . The SQL query on Oracle SQL developer is returning a row though.
@Tan that's not an error, it's just that no rows were retrieved. Check your SQL statement.
You are right, this SQL isn't returning any row. Thanks . I think I got confused with my other SQL query and thought that it's returning a row. The ID was different for that query.
@Tan and remember you can optimize your query by telling the DBMS to fetch only a single row. Like a wrote in the answer.
Thanks. Good point. What would be an alternative for Oracle db? I believe LIMIT is only for MySQL.
|
0

you should ask if resultset has values maybe with a while

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

that way you won't get in the error and just jump through return

2 Comments

I believe while loop won't work in my case since I am only getting a single row?
it will work if you have a single row it will enter one time do your job and will go out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.