Problemas com Sql

3 respostas
douglasmendes

Estou montando um Sistema de Gerenciamento de Biblioteca e montei várias telas de cadastro consulta edição e exclusão
o meu problema inicia exatamente na classe DaoAluno nessa classe de persistência, faço os comando de Insert into funciona blz, mas faço entra no update
me apresenta o seguinte erro

public void save(Aluno aluno){
        Database database = null;
        try{
            database = Database.getInstance();
            Statement stmt = database.getConnection().createStatement();
            String sql ="";
            if((aluno.getId()== null) ||(aluno.getId()==0)){
                sql = "insert into aluno(nome,sobrenome,data_nascimento,rg,cpf,nome_mae,nome_pai,responsavel,telefone,celular,endereco,num_casa,bairro) values(" + "'" + aluno.getNome() + "','" + aluno.getSobrenome() + "','" + aluno.getData_nascimento()+"','" + aluno.getRg()+
                        "','"+aluno.getCpf()+"','"+aluno.getNome_mae()+"','"+aluno.getNome_pai()+"','"+aluno.getResponsavel()+"','"+aluno.getTelefone_contato()+"','" +aluno.getCelular() +"','"+aluno.getEndereco()+ "','" + aluno.getNumero_casa()+"','"+aluno.getBairro()+ "')";  
                stmt.execute(sql,Statement.RETURN_GENERATED_KEYS);
                ResultSet rst = stmt.getGeneratedKeys();
                if(rst.next()){
                    aluno.setId(rst.getInt(1));
                }
                
            }else{
                    sql = "update aluno set nome = '" + aluno.getNome() + "', "
                            + "sobrenome = '" + aluno.getSobrenome() + "', " 
                            + "data_nascimento = '" + aluno.getData_nascimento() + "', "
                            + "rg = '" + aluno.getRg() + "', " 
                            + "cpf = '" + aluno.getCpf() + "', " 
                            + "nome_mae = '" + aluno.getNome_mae() + "', "
                            + "nome_pai = '" + aluno.getNome_pai() + "', "
                            + "responsavel = '" + aluno.getResponsavel() + "', "
                            + "telefone = '" + aluno.getTelefone_contato() + "', "
                            + "celular = '" + aluno.getCelular() + "', "
                            + "endereco = '" + aluno.getEndereco() + ", "
                            + "num_casa = '" + aluno.getNumero_casa() + "'"
                            + "bairro = '" + aluno.getBairro() + "'"                           
                            + "where idaluno = " + aluno.getId();
                    stmt.execute(sql);	                    
                }
                System.out.println(sql);
                database.getConnection().commit();
            
        }catch(SQLException e){
            try{
                database.getConnection().rollback();
            }catch(SQLException e1){
                e1.printStackTrace();
            }
            
           e.printStackTrace(); 
        }
    }
"55" nesse caso é o getNumero_casa que eu incluo no momento de edição

[color=red]org.postgresql.util.PSQLException: ERRO: erro de sintaxe em ou próximo a "55"
Posição: 283
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2077)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1810)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:498)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:372)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:364)
at sistema_biblioteca.dao.DaoAluno.save(DaoAluno.java:52)
at sistema_biblioteca.session.SessionAluno.save(SessionAluno.java:21)
at sistema_biblioteca.gui.ManterAlunoGUI.salvar(ManterAlunoGUI.java:370)
at sistema_biblioteca.gui.ManterAlunoGUI$TrataEventoBotao.mouseClicked(ManterAlunoGUI.java:47)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)[/color]

só retomando, ele salva normalmente mas no momento em que eu vou editar o campo numero, ou se eu comento esse campo o campo bairro apresenta este erro indicado
alguém manda um help ai =D

vlw

3 Respostas

pmlm

Falta uma virgula no teu sql.

Para evitar esses problema (e outros mais graves), usa PreparedStatement.

douglasmendes

na verdade nao faltava nao , =D
é que eu para testar o código comentei o bairro e coloquei o " ’ "
dai para mostrar o código aqui eu descomentei e esqueci de colocar a virgula no numero_casa

o mais engraçado é que a minha DaoFuncionario esta escrita no mesmo padrão e consigo fazer a edição
e na DaoAluno não

douglasmendes

Pronto resolvi o meu problema… acho que de tanto ver código eu nao tinha visto uma virgula faltando MALDIÇÃO =X
mas agora resolvi e ta filé vou partir para o proximo passo iniciar Emprestimos e devoluções =D

Criado 15 de novembro de 2011
Ultima resposta 16 de nov. de 2011
Respostas 3
Participantes 2