java.sql.SQLException:仅转发结果集的无效操作:isLast

时间:2011-11-07 02:38:52

标签: java connection resultset sqlexception

最初我给了:

 Statement replystmt = connection.createStatement;

我改为

Statement replystmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

ResultSet replyMessage = replystmt.executeQuery("SELECT * from REPLYMAIL where PARENTMESSAGEID = '" + parentMessage.getString("MESSAGEID") + "'");

我仍然收到以下错误:

        java.sql.SQLException: Invalid operation for forward only resultset : isLast
            at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
            at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
            at oracle.jdbc.driver.OracleResultSetImpl.isLast(OracleResultSetImpl.java:390)
            at inbox.InboxReader.getImportance(InboxReader.java:249)
            at inbox.InboxReader.main(InboxReader.java:39)

编辑:

static long getImportance(String username) throws Exception
    {
        Connection connection = connectToDatabase();
        Statement parentstmt = connection.createStatement();
        ResultSet parentMessage = parentstmt.executeQuery("SELECT * from MAIL");
        long time1 = 0, sign = 0;

        while(parentMessage.next())
        {
            System.out.println("Parent message : " + parentMessage.getString("MESSAGEID"));
            int c = 0, evenc = 0;
            String j = "";

            Statement replystmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            //stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)

            ResultSet replyMessage = replystmt.executeQuery("SELECT * from REPLYMAIL where PARENTMESSAGEID = '" + parentMessage.getString("MESSAGEID") + "'");

            if(!(parentMessage.getString("SENDERNAME").contains(username)))   // When user receives a message first
            {
                while(replyMessage.next())
                {
                    if(c==0)   // for calculating the time between his reply time - message received time  
                    {
                        time1 = replyMessage.getTimestamp("SENDDATE").getTime() - parentMessage.getTimestamp("RECEIVEDATE").getTime();
                        System.out.println("IF time in if c=0, " + time1);
                        c++;
                        sign = 1; 
                    }
                    else   
                    {

                        evenc++; 
                        if(sign == 1) {
                            sign = -1;
                            j = "RECEIVEDATE";
                            System.out.println("IF receivedate : " + replyMessage.getTimestamp(j).getTime());
                        }
                        else if(sign != 1) {
                            sign = 1;
                            j = "SENDDATE";
                            System.out.println("IF senddate : " + replyMessage.getTimestamp(j).getTime());
                        }   

                        if(replyMessage.isLast() && (evenc%2)!=0) {
                            System.out.println("IF skip");
                        }
                        else {
                            time1 = time1 + (sign * replyMessage.getTimestamp(j).getTime());
                        }
                        System.out.println("IF time in if c>0 , "+time1);
                    }
                }
            }
            else {             // When user sends a message first
                sign = -1;
                while(replyMessage.next()) {
                    evenc++;
                    if(sign == 1) {
                        sign = -1;
                        j = "RECEIVEDATE";
                        System.out.println("ELSE receivedate : " + replyMessage.getTimestamp(j).getTime());
                    }
                    else {
                        sign = 1;
                        j = "SENDDATE";
                        System.out.println("ELSE senddate : " + replyMessage.getTimestamp(j).getTime());
                    }   

                    if(replyMessage.isLast() && (evenc%2)!=0) {
                        System.out.println("ELSE skip");
                    }
                    else {
                        time1 = time1 + (sign * replyMessage.getTimestamp(j).getTime());
                    }
                    System.out.println("ELSE time in if c>0 , "+time1);
                }
            }   
        }
        connection.close();
        return time1;
    }

我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

link表示Oracle可能不支持isLast()方法用于存储过程ResultSet读取。 作为解决方法,您可以计算首先返回的行数。然后在处理过程中实际遍历所有行时知道哪一行是最后一行。