Erro ao inserir dados no sql server

4 respostas
F

Galera eu to tentando fazer uma inserção no banco mas os campos que eu vou pegar do bean vao para o banco como o null, eu ja debuguei e aparece o bean preenchido???
Alguma ideia??

public void inserirDados(Collection list) {
 				
 		Sindical_Bean beanInterno= new Sindical_Bean();
 		Iterator it= list.iterator();
 		
 		
 		String sql=("Insert Into tblBaixaSindical (idPagamento,cnpj" +
 				",valorJuro,valorMulta,valorDaGuia,exercicio,tipo,meio" +
 				",banco,entidadeSindical,usuario,capitalSocial,dataVencimento" +
 				",dataPagamento,dataBaixa) values(000,"+beanInterno.getCnpj()+",0,0,"
 				+beanInterno.getValorContribuicao()+","+beanInterno.getExercicio()+
 				",'guia','normal','bb','Fecomercio',63,0,"+beanInterno.getDataVencimento()+","
 				+beanInterno.getDataPagamento()+","+beanInterno.getDataBaixa()+");");
 		//conexaoSQLServer = con.conectarBancoSqlServer();
 		
 		
 		try {
 			stmt =conexaoSQLServer.createStatement();
 			//rs= stmt.executeQuery(sql);
 			while(it.hasNext()){
 				beanInterno=(Sindical_Bean) it.next();
 				System.out.println(beanInterno.getCnpj());
 				
 				rs= stmt.executeQuery(sql);
 			}
 			
 			} catch (SQLException e) {
 			e.printStackTrace();
 		}
 		
 		
 		
 	
 	}

Falow!!!1

4 Respostas

F

Ele retorna algum erro, ou simplesmente executa normalmente mas não inclui o registro!?

J

Você não pode usar executeQuery() para alterações, só para consultas…

use executeUpdate()…

e tente usar o PreparedStatement no lugar de Statement, porque essa sua expressão sql está muito feia desse jeito (só sugestão)…

veja o video JDBC em:

http://jairelton.com

F

Achei o erro , eu tinha que colocar execute somente e sem retorno nenhum111

Valeu1111

J

execute() tambem não deve ser usado, ele é para fins muito especificos, onde são retornados multiplos ResultSet’s, como na execução de certas Stored Procedures, o que deve ser usado é executeUpdate(), veja na documentação da API:

“Documentação”:
Executes the given SQL statement, which may return multiple results. In some (uncommon) situations, a single SQL statement may return multiple result sets and/or update counts. Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results or (2) you are dynamically executing an unknown SQL string.

The execute method executes an SQL statement and indicates the form of the first result. You must then use the methods getResultSet or getUpdateCount to retrieve the result, and getMoreResults to move to any subsequent result(s).

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html#execute(java.lang.String)

Criado 17 de maio de 2006
Ultima resposta 17 de mai. de 2006
Respostas 4
Participantes 3