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.