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?
– Guilherme Souza