Error: "You have an error in your SQL syntax"

Asked

Viewed 213 times

2

I’m getting the following error while executing the change attempt in the BD:

You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'CODIGO= 0' at line 1

THE DAO

public void altera(Treino t){
    conectar();
    PreparedStatement alterarTreino = null;

    try {

    String sql = "UPDATE TREINOS "
            + "SET CPF=?, NOME=?, DATANASC=?, FREQUENCIA=?, CLASSIFICACAO=?, MUSCULACAO=?, ESTEIRA_BICICLETA=?, SPINNING=?, ZUMBA=?, ARTESMARCIAIS=?, TREINOFUNCIONAL=?"
            + "WHERE CODIGO= ? ";

    alterarTreino = con.prepareStatement(sql);

    alterarTreino.setString(1, t.getCPF());
    alterarTreino.setString(2, t.getNome());
    alterarTreino.setString(3, t.getDatanasc());
    alterarTreino.setString(4, t.getFrequencia());
    alterarTreino.setString(5, t.getClassificacao());
    alterarTreino.setInt(6, t.getMusculacao());
    alterarTreino.setInt(7, t.getEsteiraEBicicleta());
    alterarTreino.setInt(8, t.getSpinning());
    alterarTreino.setInt(9, t.getZumba());
    alterarTreino.setInt(10, t.getArtesMarciais());
    alterarTreino.setInt(11, t.getTreinoFuncional());
    alterarTreino.setInt(12, t.getTreinoFuncional());



    int r=alterarTreino.executeUpdate();

    if(r > 0){
        **//comando.executeUpdate(sql);**
        System.out.println("Alterado!");
    }

    }catch(SQLException e){
        imprimeErro("Erro ao alterar Treino", e.getMessage());
    }
     finally {
        fechar();}
    }


**O código atualizar**

    public void actionPerformed(ActionEvent arg0) {


                //Recupera da interface Gráfica
                String Nome = txtNome.getText();
                String CPF= txtCPF.getText();
                String Datanasc = txtDatanasc.getText();
                String Codigo = txtCodigo.getText();

                String Freq = (String) Frequencia.getSelectedItem();
                String Classific = (String) Classificacao.getSelectedItem();

                int isMusculacao = Musculacao.isSelected() ? 1 : 0;
                int isEsteiraEBicicleta = EsteiraEBicicleta.isSelected() ? 1 : 0;
                int isSpinning = Spinning.isSelected() ? 1 : 0;
                int isZumba =  Zumba.isSelected() ? 1 : 0;  
                int isArtesMarciais = ArtesMarciais.isSelected() ? 1 : 0;
                int isTreinoFuncional =  TreinoFuncional.isSelected() ? 1 : 0;  




            Treino t = new Treino();

            t.setCodigo(Codigo);
            t.setNome(Nome);
            t.setCPF(CPF);
            t.setDatanasc(Datanasc);
            t.setFrequencia(Freq);
            t.setClassificacao(Classific);
            t.setMusculacao(isMusculacao);
            t.setEsteiraEBicicleta(isEsteiraEBicicleta);
            t.setSpinning(isSpinning);
            t.setZumba (isZumba);
            t.setArtesMarciais (isArtesMarciais);
            t.setTreinoFuncional(isTreinoFuncional);



            DaoTreino gravar = new DaoTreino();

            gravar.altera(t);

                JOptionPane.showMessageDialog(null, "Gravado");


            }

IT SAYS THAT CODE IS NULL, BUT I PUT IN VALUES. Just comment the line in bold://command.executeUpdate(sql); and error solved. This was a comment in the code that ended up without the bars.

  • As the error itself says, you have an error in your SQL syntax. The entire code you have posted is virtually useless. Edit your question and place the stacktrace, because then you (and us) know exactly where this execation is being generated. Of two one, either some of their DAO are incorrectly implemented or improper parameters are being passed to the DAO.

  • Thanks, I’ll edit my question.

  • Where is the DAO? This ai code helps even less than the previous one. Add the class that connects to your mysql database, along with the querys.

  • I posted the update DAO.

  • Leticia, welcome to [en.so]! Also post the DDL of the table, please. This error may be caused because you are using setString to include a field of the type DATETIME or equivalent.

  • 1

    Thank you, I’ve solved the mistake by deconstructing a part that went unnoticed.

Show 1 more comment

1 answer

0

It seems that the error is because there is a missing space before the Where and when it concatenates, it’s like:

SET CPF=? , NAME=? , DATANASC=? , FREQUENCIA=? , CLASSIFICACAO=? , MUSCULACAO=? , ESTEIRA_BICICICLETA=? , SPINNING=? , ZUMBA=? , ARTESMARCIALS=?, TRAINING=? WHERE CODE= ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.