Resultset first does not work

Asked

Viewed 1,014 times

1

I am creating a Java application in Eclipse.

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

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

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

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

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

When I click the button First 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();
    }
}

Set Code:

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

public void setNome(String nome) {
    jtfNome.setText(nome);
}
  • I know it worked because I created a println and the result came out right. But it’s not appearing in my Jtextfields, I edited the question and added my set methods, you could take a look?

1 answer

0


Sqlexception: Invalid operation to forward result set only: first

This happens because by default the ResultSet is not upgradable, and the cursor Just go, don’t go back.

If you don’t 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, indicating that the ResultSet cannot be upgradeable.

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

In the Connection.html#createStatement, you can change this behavior, do so:

stm = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE);

When indicating the type TYPE_SCROLL_INSENSITIVE, you can move the cursor of ResultSet to where you wish, using the methods mentioned above.

If you want the ResultSet is upgradable, indicates the level of competition CONCUR_UPDATABLE:

stm = conexao.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                              ResultSet.CONCUR_UPDATABLE);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.