Error must declare the scalar variable @ p3where, in JAVA, when trying to update data from the database using a button

0

Hello everyone I hope you can support me, I start in this JAVA and make connections with SQL and I am little by little with learning, I hope you can help me

I explain I'm doing practice and I started with an agenda, which everything was working fine until I hit the "UPDATE" button, I missed an error and I do not know exactly where the error is the code the button is as follows.

    JButton btnActualizar = new JButton("Actualizar");
        btnActualizar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


try {

                    String ActualizarSQL = "UPDATE contactos SET Nombre=?, Direccion=?, Telefono=?, Email=?"
                            + "WHERE ID=?";
                     int selectfila = table.getSelectedRow();
                     String dato = (String)table.getValueAt(selectfila,0);

                    PreparedStatement ps = agen.prepareStatement(ActualizarSQL);

                    ps.setString(1, txtNombre.getText());
                    ps.setString(2, txtDireccion.getText());
                    ps.setString(3, txtTelefono.getText());
                    ps.setString(4, txtEmail.getText());
                    ps.setString(5, dato);  


        // Se crea una variable que va almacenar un numero, EL CUAL SERA UN INDICADOR SI LOS DATOS SE AGREGARON O NO            
                    int n = ps.executeUpdate();
                    if(n>0) {
                        LlenarTabla();
                        JOptionPane.showMessageDialog(null,"Datos Guardados Correctamente");
                }



                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null,"Error: "+e.getMessage());
                }



            }
        });

In the same way in SQL, I have the DB created and the 5 fields are correct, such as Name, Address, Phone, Email and ID, as they are case-sensitive.

What I say in the code is that in the variable "data", the "value" of the ID field is saved, and then it is replaced in the QUERY,. in WHERE =? thus in this way the UPDATE is made to the fields related to that ID.

THE PROBLEM IS THAT I BOTH THE NEXT ERROR WHEN I WANT TO KEEP

"error must declare the scalar variable @ p3where" And the great thing is that I do not have any variable called @ p3Where, so I can not find the error in the whole code.

AS AN ADDITIONAL DATA, NEW ACTION AND SAVING WORK ME WITHOUT MUCH PROBLEMS

JButton btnGuardar = new JButton("Guardar");
        btnGuardar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                try {


                    String DatosSQL = ("INSERT INTO contactos VALUES(?,?,?,?,?)");



                    PreparedStatement ps = agen.prepareStatement(DatosSQL);
                    ps.setString(1, txtId.getText());                   
                    ps.setString(2, txtNombre.getText());
                    ps.setString(3, txtDireccion.getText());
                    ps.setString(4, txtTelefono.getText());
                    ps.setString(5, txtEmail.getText());



                    int n = ps.executeUpdate();
                    if(n>0) {
                        LlenarTabla();
                        JOptionPane.showMessageDialog(null,"Datos Guardados Correctamente");
                }



                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null,"Error: "+e.getMessage());
                }



            }
        });
        btnGuardar.setBounds(120, 438, 89, 23);
        contentPane.add(btnGuardar);

I hope you can support me, thank you.

    
asked by Alexis Taboada 21.12.2018 в 01:50
source

1 answer

0

Seeing your code the error seems to me to be the following ...

String ActualizarSQL = "UPDATE contactos SET Nombre=?, Direccion=?, Telefono=?, Email=?"
                        + "WHERE ID=?";

is exactly the same as

String ActualizarSQL = "UPDATE contactos SET Nombre=?, Direccion=?, Telefono=?, Email=?WHERE ID=?";

as you will see, there is no space before the WHERE and therefore it should fail.

    
answered by 28.12.2018 в 19:26