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.
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.