ResultSet first does not work

1

I'm creating a Java application in Eclipse.

I'm having trouble making the First, Previous, Next, and Last buttons work.

I'm stuck on the First button because, I believe, the difference is just the .first() , .next() , .previous() and .last() .

The error that appears when I click the button is this:

  

java.sql.SQLException: Invalid operation to forward only   result set: first

The Aluno table has the fields ra and nome , both are varchar2 .

When I click the First button this will activate this method:

public void primeiro() {
    try {
        //con é meu objeto da classe ConexaoMVC
        con.conecta();

        ResultSet rs;
        String RA,NOME;

        rs = con.stm.executeQuery("Select * from Aluno");
        rs.first();

        RA = rs.getString("ra");
        NOME = rs.getString("nome");
        // visao é o objeto da minha classe de interface grafica
        visao.setRA(RA);
        visao.setNome(NOME);

    } catch(SQLException SqlExc){ //trata os erros
        System.out.println("Erro de SQL! \n"+SqlExc);      
    }
}

Code of my connection class:

package bd;

import java.sql.*;
import javax.swing.*;

public class ConexaoMVC {
    public Connection conexao;
    private String driver, url;
    Statement stm;

    public ConexaoMVC() {
        //driver="sun.jdbc.odbc.JdbcOdbcDriver";
        driver = "oracle.jdbc.driver.OracleDriver";
        url = "jdbc:oracle:thin:guilherme/1997@//localhost:1521/XE";
    }

    public void conecta() {
        try {
            // carrega o driver da ponte jdbc-odbc
            Class.forName(driver);
            // abre conexao com o banco de dados
            conexao = DriverManager.getConnection(url);
            System.out.println("Conexão executada com sucesso");

            stm = conexao.createStatement();
            //conexao.close();
        } catch (SQLException SqlExc) {
            System.out.println("Erro de SQL!");
        } catch (ClassNotFoundException exc) {
            System.out.println("Classe não encontrada!");
        }
    }

    public static void main(String args[]) {
        Conexao ins = new Conexao();
    }
}

Sets Code:

public void setRA(String ra) {
    jtfRa.setText(ra);
}

public void setNome(String nome) {
    jtfNome.setText(nome);
}
    
asked by anonymous 23.09.2016 / 15:24

1 answer

0
  

SQLException: Invalid operation to forward result set only: first

This happens because by default ResultSet is not and the cursor only advances, does not go back.

If you do not pass arguments to Connection.html#createStatement , it is assumed that the type of ResultSet will be TYPE_FORWARD_ONLY , whose cursor can only move forward, and the level of competition CONCUR_READ_ONLY , which indicates that ResultSet can not be upgraded.

Methods are not allowed first , last , previous , absolute " or relative .

No Connection.html#createStatement , you can change this behavior by doing so:

stm = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE);

When you type TYPE_SCROLL_INSENSITIVE , you you can move the cursor from ResultSet to wherever you want, using the methods mentioned above.

If you want ResultSet to be upgradeable, please indicate the level of competition CONCUR_UPDATABLE :

stm = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                              ResultSet.CONCUR_UPDATABLE);
    
23.09.2016 / 16:31