Java.sql.SQLException : Result set after last row exception … here is a solution to the problem.
Java.sql.SQLException : Result set after last row exception
I
think I’m having trouble because of this line in the RequestDaoImpl
class mentioned below rs.next();
。 Whenever I try to retrieve the value of STATUS
, I keep getting the following error:
java.sql.SQLException: The result set follows the last line
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 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 errors in the here post, but their code is different from mine.
Solution
This may be because no rows were fetched.
ResultSet#next
returns a boolean
value indicating whether a row exists.
What you need to apply for is a condition that checks the condition.
Since you need a line, if
is a great fit.
if (rs.next()) {
...
requestStatus = rs.getString("STATUS");
...
}
Note that your queries can be optimized by applying keywords that rely on the DBMS, such as MySQL's
LIMIT
SELECT STATUS FROM Mytable WHERE request_user= ? and app_name =? and request_id=? LIMIT 1