Reputation: 5
I am working with Model-View-Controller.
I have a Jframe
form where I insert customers data( nit, name, address....), the insertions works (so I am connected with the DB correctly). Then I need to be able to MODIFY some of that fields and save them into the DB. So I select one of the customers from my table (in the Jframe) changing the values (or data) and save it again but when I try to UPDATE my DB my PreparedStatement.executeUpdate()
returns 0
(nothing was changed...so no updates in the table of my jframe or the DB).
This is my ODA class (my DB name is datosclientes and the rest are the variables I have there). my class modelo only has my variables, methods setter, and getter.
public int modificar(modelo m){
int r = 0;
String sSQL="UPDATE datosclientes SET nit=?, nombre=?, direccion=?, telefono=?, fecha=?, tipo=?, comentario=? WHERE id=?";
try{
con=conectar.conectar();
ps=con.prepareStatement(sSQL);
ps.setInt(1,m.getId());
ps.setString(2,m.getNit());
ps.setString(3,m.getNombre());
ps.setString(4,m.getDireccion());
ps.setString(5,m.getTelefono());
ps.setString(6,m.getFecha());
ps.setString(7,m.getTipoSer());
ps.setString(8,m.getComentario());
r= ps.executeUpdate();
if(r==1){
return 1;
}else{
return 0;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "no se pudo modificar desde DOA" + e, " modeloODA ", JOptionPane.ERROR_MESSAGE);
}//end catch
return r;
}
in my controller class I have:
public void actualizar(){
int id=Integer.parseInt(mTickete.txtId.getText());
String nit=mTickete.txtNit.getText();
String nom= mTickete.txtNom.getText();
String dir=mTickete.txtDir.getText();
String tel= mTickete.txtTel.getText();
String fec= mTickete.txtFec.getText();
String tip= mTickete.cbxTipo.getSelectedItem().toString();
String com= mTickete.taCom.getSelectedText();
try{
Modelo.setId(id);
Modelo.setNit(nit);
Modelo.setNombre(nom);
Modelo.setDireccion(dir);
Modelo.setTelefono(tel);
Modelo.setFecha(fec);
Modelo.setTipoSer(tip);
Modelo.setComentario(com);
int r=dao.modificar(Modelo);
if(r==1){
JOptionPane.showMessageDialog(cTickete, "Usuarios actualizado correctamente");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "no se pudo modificar desde controlador" + e, " modeloODA ", JOptionPane.ERROR_MESSAGE);
}
}
Thank you in advance :)
Upvotes: 0
Views: 151
Reputation: 26926
The last parameter passed to the ps must be the id... at the moment it is the Comentario. Replace the code
ps.setInt(1,m.getId());
ps.setString(2,m.getNit());
ps.setString(3,m.getNombre());
ps.setString(4,m.getDireccion());
ps.setString(5,m.getTelefono());
ps.setString(6,m.getFecha());
ps.setString(7,m.getTipoSer());
ps.setString(8,m.getComentario());
with
ps.setString(1,m.getNit());
ps.setString(2,m.getNombre());
ps.setString(3,m.getDireccion());
ps.setString(4,m.getTelefono());
ps.setString(5,m.getFecha());
ps.setString(6,m.getTipoSer());
ps.setString(7,m.getComentario());
ps.setInt(8,m.getId()); // Moved at the end
Upvotes: 3