Error Parameter 5

Asked

Viewed 46 times

3

When I try to change the already performed record, issues the No value specified for parameter 5.

The code:

private void alterar() {

        String sql = "UPDATE tbusuarios SET usuario = ?, telefone = ?, login = ?, senha = ?, perfil = ? WHERE iduser = ?";

        try {
            pst = conexao.prepareStatement(sql);
            pst.setString(1, txt_nome.getText());
            pst.setString(2, txt_telefone.getText());
            pst.setString(3, txt_login.getText());
            pst.setString(4, psw_senha.getText());
            pst.setString(6, cb_perfil.getSelectedItem().toString());
            pst.setString(3, txt_id.getText());
            //Verificar campos vazios antes de adicionar
            if((txt_nome.getText().isEmpty()) || (psw_senha.getText().isEmpty()) || (txt_login.getText().isEmpty()) || (txt_id.getText().isEmpty()) || (cb_perfil.getSelectedItem().equals(""))) {
                JOptionPane.showMessageDialog(null, "Preencha os dados!");
            } else {
                limparCampos();
                int adicionado = pst.executeUpdate();
                //Se o adicionado for maior que zero, siginifica que teve um registro
                if (adicionado > 0) {
                    JOptionPane.showMessageDialog(null, "Registro alterado com sucesso!");
                }
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

How could I fix this?

1 answer

4


You did not inject the fifth parameter of your query. Instead, you placed the third parameter twice.

Replace that:

pst.setString(1, txt_nome.getText());
pst.setString(2, txt_telefone.getText());
pst.setString(3, txt_login.getText());
pst.setString(4, psw_senha.getText());
pst.setString(6, cb_perfil.getSelectedItem().toString());
pst.setString(3, txt_id.getText());

That’s why:

pst.setString(1, txt_nome.getText());
pst.setString(2, txt_telefone.getText());
pst.setString(3, txt_login.getText());
pst.setString(4, psw_senha.getText());
pst.setString(5, cb_perfil.getSelectedItem().toString());
pst.setString(6, txt_id.getText());
  • Really, I hadn’t paid attention to that detail... thanks! @Felipe Marinho

Browser other questions tagged

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