我遇到了一个空结果集的问题,这会导致一些错误。但只要它不是空的,它就能正常工作。
String sql = "SELECT M2.fromProfileId, profiles.profileMiniature, profiles.firstName, profiles.lastName, profiles.timeFormat, lastMessages.message, lastMessages.timeStamp " +
"FROM (" +
" SELECT M1.fromProfileId, " +
" max(M1.timeStamp) AS lastMessageTime " +
" FROM messages AS M1 " +
" WHERE M1.toProfileId = ? " +
" GROUP BY M1.fromProfileId " +
" ORDER BY max(M1.timeStamp) DESC " +
" LIMIT 10 " +//line 60
" ) AS M2 " +
"INNER JOIN messages AS lastMessages " +
"ON (" +
" lastMessages.timeStamp = M2.lastMessageTime " +
"AND lastMessages.fromProfileId = M2.fromProfileId" +
" )" +
"INNER JOIN profiles " +
"ON M2.fromProfileId = profiles.profileId ";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, profileId);
ResultSet result = statement.executeQuery();
JSONArray messages = new JSONArray();
while(result.next()){
JSONObject message = new JSONObject();
message.put("fromProfileId", result.getString("fromProfileId"));
message.put("profileMiniature", result.getString("profileMiniature"));
message.put("firstName", result.getString("firstName"));
message.put("lastName", result.getString("lastName"));
message.put("lastMessage", result.getString("message"));
message.put("lastMessageTime", result.getString("timeStamp"));
message.put("timeFormat", result.getString("timeFormat"));
messages.put(message);
}
,错误为Illegal operation on empty result set
。我该如何解决这个问题?
堆栈跟踪:
空结果集上的非法操作。 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) 在 com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841) 在 com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656) 在 com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576) 在 com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616) 在 messages.GetMessages.doGet(GetMessages.java:60) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:707) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 在 org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:534) 等。