Error trying to delete a record in the database from Java

0

Hello everyone I am new to programming, I have a problem, my system connects to mysql and uploads data to the database, but I miss an error when trying to delete, I hope you can help me because I do not I see the error.

private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int fila = tabladatos.getSelectedRow();
    String valor = tabladatos.getValueAt(fila, 0).toString();
    String sql = "DELETE FROM clientes WHERE Id_cliente = ?";
    if (fila >= 0) {
        try {
            PreparedStatement ps = cn.prepareStatement(sql + (valor));
            ps.setString(1, id.getText());
            ps.setString(2, nombre.getText());
            ps.setString(3, domicilio.getText());
            ps.setString(4, telefono.getText());
            ps.executeUpdate();
            mostrartabla();
            JOptionPane.showMessageDialog(null, "Datos eliminados");
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
} 

The error that marks me is this:

  

java.sql.SQLException: Parameter index out of range (2 > number of   parameters, which is 1)

    
asked by Pablo P 10.07.2017 в 16:31
source

2 answers

1

The number of parameters to pass must be equal to the number of parameters of the query. In your case your query expects 1 but you spend many more.

Simply delete those that are not necessary.

private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int fila = tabladatos.getSelectedRow();

    String valor = tabladatos.getValueAt(fila, 0).toString();

    String sql = "DELETE FROM clientes WHERE Id_cliente = ?";

    if (fila >= 0) {
        try {
            PreparedStatement ps = cn.prepareStatement(sql + (valor));
            ps.setString(1, id.getText());
            ps.executeUpdate();
            mostrartabla();
            JOptionPane.showMessageDialog(null, "Datos eliminados");
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
} 

Apart I see strange things in your code:

PreparedStatement ps = cn.prepareStatement(sql + (valor));

What sense has value in that sentence?

String valor = tabladatos.getValueAt(fila, 0).toString();
if (fila >= 0) {

Here you check if row is greater than or equal to zero but you have used it before so you can get an error in the getValueAt and the toString later.

ps.setString(1, id.getText());

Where does it come from? It is not declared in the method and I have the impression that id.getText () is really valuable.

    
answered by 10.07.2017 в 16:49
0

Your error is here:

PreparedStatement ps = cn.prepareStatement(sql + (valor));
                ps.setString(1, id.getText());
                ps.setString(2, nombre.getText());
                ps.setString(3, domicilio.getText());
                ps.setString(4, telefono.getText());

since in your SQL string is the following:

  

String sql="DELETE FROM clients WHERE Customer_ID =?";

As you can see, in your chain SQL you only receive the Customer_ID parameter, and you are sending the ID, name, address and phone number. If you delete all the parameters that you send, except for the client_ID, your chain should work:

PreparedStatement ps = cn.prepareStatement(sql + (valor));
                ps.setString(1, id.getText());
                ps.executeUpdate();
    
answered by 10.07.2017 в 16:50