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