简体   繁体   中英

Error SQL in java: Illegal operation on empty result set

I have a problem with an empty result set, which are throwing some errors. But it is working fine as long as it is not empty.

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

and the error is Illegal operation on empty result set . How shall I fix this?

StackTrace:

Illegal operation on empty result set. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841) at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656) at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576) at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616) at messages.GetMessages.doGet(GetMessages.java:60) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:534) etc..

我在MySQL JDBC驱动程序中发现了很多错误, 这些错误似乎导致了这个异常。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM