Problema durante execução de SELECT

1 resposta
L

Ae galera,

To com um probleminha aqui, que ja nao sei mais o que fazer!! heheheh
o código é o seguinte:

Classe Loja
public String PesquisarPessoa(String nome)  throws RemoteException, SQLException{
		int codigo;
		String nom, end, cpf , rg, tel;
		String sql = "SELECT * FROM `cliente` WHERE nome like '%" + nome + "%'";

		Statement stm = this.con.createStatement();
		ResultSet rs = stm.executeQuery(sql);
		String S = "";

		
			codigo = rs.getInt("codigo");
			nom   = rs.getString("nome");
			end    = rs.getString("endereco");
			cpf    = rs.getString("cpf");
			rg     = rs.getString("rg");
			tel    = rs.getString("tel");

			S = "Código: " + codigo + "\nNome: " + nome + "\nEndereco: " + end + "Cpf: " +
			cpf + "RG: " + rg + "Telefone: " + tel + "\n\n";
			rs.close();
			return S;
			
	}
A chamada do codigo: Método pesquisarCliente
try {
			Loja_IF loja = new Loja();
			memo.append( loja.PesquisarPessoa( (edtNome.getText() ) ));
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

Ele está gerando as exeções:

java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:720)
at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2617)
at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2749)
at br.com.unibratec.util.Loja.PesquisarPessoa(Loja.java:123)
at br.com.unibratec.gui.PesquisarCliente.actionPerformed(PesquisarCliente.java:110)

Alguem poderia me dar uma luz?

Obrigado :lol:

1 Resposta

J

Você tem que ir para o primeiro registro do ResultSet antes de tentar pegar os dados com os metodos get. Para isso use rs.next();

Criado 30 de maio de 2007
Ultima resposta 30 de mai. de 2007
Respostas 1
Participantes 2