[RESOLVIDO] Contar registros no ResultSet

3 respostas
T

Boas!
Estou com um problema para contar os registros no ResultSet. Eu tentei pelos métodos last() e getRow(), mas recebo a mensagem “The ‘last()’ method is only allowed on scroll cursors”. Existe algo que eu possa fazer para conseguir contar os registros em minha tabela??

public int contaRegistros()throws SQLException{
            Statement stm = con.createStatement();
            ResultSet rst = stm.executeQuery("SELECT * FROM APP.LIVRO");
            rst.last();
            return rst.getRow();
        }

3 Respostas

asousaj

Assim serve?

int num_rows = 0; Statement stm = con.createStatement(); ResultSet rst = stm.executeQuery("SELECT * FROM APP.LIVRO"); while (rst.next()) { num_rows++; } System.out.println(num_rows+" rows");

T

Serve sim asousaj, valeu pela ajuda!

pmlm

Alguém precisa urgentemente de aprender SQL.
Correr todo o result set não é a solução.

public int contaRegistros()throws SQLException{  
        Statement stm = con.createStatement();  
        ResultSet rst = stm.executeQuery("SELECT COUNT(*) FROM APP.LIVRO");  
        rst.next();  
        return rst.getInt(1);  
    }
Criado 22 de novembro de 2013
Ultima resposta 22 de nov. de 2013
Respostas 3
Participantes 3