Error parameter 5

3

When I try to change the record already done, it sends 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 solve this?

    
asked by anonymous 07.02.2017 / 00:13

1 answer

4

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

Replace this:

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());

So:

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());
    
07.02.2017 / 00:19