我认为我有问题,因为下面提到的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,但它们的代码与我的代码不同。
答案 0 :(得分:0)
您应该询问结果集是否有一段时间的值
while(rs.next){
//your code here
}
那样,您就不会陷入错误,只需跳回return
答案 1 :(得分:0)
可能会发生,因为未获取没有行。
ResultSet#next
返回一个boolean
值,该值表示是否存在行。
您需要申请的条件是检查该条件。
由于您只需要一行,因此if
非常适合。
if (rs.next()) {
...
requestStatus = rs.getString("STATUS");
...
}
请注意,可以通过应用DBMS依赖关键字来优化查询,例如将LIMIT
的{{1}}
MySQL