Estou com uma duvida na minha aplicação onde preciso inserir um dado em uma tabela se o mesmo dado não existir ainda, ou seja eu seleciono na minha tabela e se o meu result não encontrar o dado eu faço o insert.
minha duvida é justamente como comparar se meu result set não encontrou o dado fornecido.
Alguem sabe como poço fazer isso?
publicbooleanconsultaCobrador(Perf02Pagamentoperf02Pagamento)throwsClassNotFoundException,SQLException{ResultSetrs=pegaDeclaracao().executeQuery("select numeroContrato from tbl_fin_perf01_lote where numeroContrato='"+perf02Pagamento.getNumeroContrato()+"'");//aqui não sei como comparar se meu select não encontrou o contrato// em sql ele traz 0 rows fetched agora aqui não sei como interpretar if(rs!=null){while(rs.next()){//perf02Pagamento.setConsultaContrato(null);populaConsultaContrato(perf02Pagamento,rs);}}rs.close();returntrue;}
publicbooleanconsultaCobrador(Perf02Pagamentoperf02Pagamento)throwsClassNotFoundException,SQLException{booleanretorno=true;ResultSetrs=pegaDeclaracao().executeQuery("select numeroContrato from tbl_fin_perf01_lote where numeroContrato='"+perf02Pagamento.getNumeroContrato()+"'");if(rs!=null){//Verificaseorsretornoupelomenosumregistro.Casotenhaencontrado,colocaFALSEnavariavelRETORNOif(rs.next()){retorno=false;}}rs.close();returnretorno;}
erasmo_tec
resolvi o problema da seguinte forma
publicbooleanconsultaSeContratoExiste(Perf02Pagamentoperf02Pagamento)throwsClassNotFoundException,SQLException{ResultSetrs=pegaDeclaracao().executeQuery("select numeroContrato from "+"tbl_fin_perf01_lote where numeroContrato='"+perf02Pagamento.getNumeroContrato()+"'");if(rs.first()==false){PreparedStatementps=pegaDeclaracaoPreparada("insert into tbl_fin_perf01_lote "+"(cobradorPadrao,cpfCnpj,numeroContrato,statusAntigo)"+"values(?,?,?,?)");ps.setString(1,perf02Pagamento.getCobradorPadrao());ps.setString(2,perf02Pagamento.getCpfCnpj());ps.setString(3,perf02Pagamento.getNumeroContrato());ps.setString(4,perf02Pagamento.getStatusPagamento());booleantoReturn=ps.execute();ps.close();returntoReturn;}rs.close();returntrue;}
se o primeiro dado do resultSet for falso quer dizer que não encontrou registro nenhum e insere o registro na tabela…
yorgan
Só acho que você juntou muitas funções nesse método.
O ideal seria você ter um que verifica se o registro existe, outro que insere e um que valide as informações e chame os métodos. Assim você poderá reutilizar eles.
Algo +/- assim:
publicvoidinserirContrato(Perf02Pagamentoperf02Pagamento){booleancontratoExistente=contratoExistente(perf02Pagamento);if(!contratoExistente){inserir(perf02Pagamento);}}publicbooleancontratoExistente(Perf02Pagamentoperf02Pagamento)throwsClassNotFoundException,SQLException{booleanretorno=false;ResultSetrs=pegaDeclaracao().executeQuery("select numeroContrato from tbl_fin_perf01_lote where numeroContrato='"+perf02Pagamento.getNumeroContrato()+"'");if(rs!=null){//Verificaseorsretornoupelomenosumregistro.Casotenhaencontrado,colocaFALSEnavariavelRETORNOif(rs.next()){retorno=true;}}rs.close();returnretorno;}publicvoidinserir(Perf02Pagamentoperf02Pagamento)throwsClassNotFoundException,SQLException{PreparedStatementps=pegaDeclaracaoPreparada("insert into tbl_fin_perf01_lote "+"(cobradorPadrao,cpfCnpj,numeroContrato,statusAntigo)"+"values(?,?,?,?)");ps.setString(1,perf02Pagamento.getCobradorPadrao());ps.setString(2,perf02Pagamento.getCpfCnpj());ps.setString(3,perf02Pagamento.getNumeroContrato());ps.setString(4,perf02Pagamento.getStatusPagamento());ps.execute();ps.close();}