mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat(ch 9): ex 1 finished - Gestión Fabricante
This commit is contained in:
@@ -45,6 +45,7 @@ public class ControladorFabricante {
|
||||
f.setId(rs.getInt("id"));
|
||||
f.setNombre(rs.getString("nombre"));
|
||||
f.setCif(rs.getString("cif"));
|
||||
rs.getDate("fechaNac")
|
||||
return f;
|
||||
}
|
||||
}
|
||||
@@ -95,7 +96,11 @@ public class ControladorFabricante {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param f
|
||||
* @return
|
||||
*/
|
||||
public static int insertaFabricante(Fabricante f) {
|
||||
try {
|
||||
Connection conn = ConnectionManager.getConnection();
|
||||
@@ -115,6 +120,28 @@ public class ControladorFabricante {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param f
|
||||
* @return
|
||||
*/
|
||||
public static int eliminaFabricante(int idFabricante) {
|
||||
try {
|
||||
Connection conn = ConnectionManager.getConnection();
|
||||
String sql = "delete from fabricante where id = ?";
|
||||
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ps.setInt(1, idFabricante);
|
||||
|
||||
return ps.executeUpdate();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -203,6 +203,11 @@ public class VentanaGestionFabricante extends JFrame {
|
||||
panel_1.add(btnNewButton_5);
|
||||
|
||||
JButton btnNewButton_6 = new JButton("Eliminar");
|
||||
btnNewButton_6.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
eliminar();
|
||||
}
|
||||
});
|
||||
panel_1.add(btnNewButton_6);
|
||||
|
||||
|
||||
@@ -277,6 +282,49 @@ public class VentanaGestionFabricante extends JFrame {
|
||||
this.jtfCif.setText("");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void eliminar() {
|
||||
String [] opciones ={"Sí","No"};
|
||||
int eleccion = JOptionPane.showOptionDialog(null,"¿Desea eliminar el registro?",
|
||||
"Confirmación",
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE, null, opciones, "No");
|
||||
if (eleccion == JOptionPane.YES_OPTION) {
|
||||
// Realmente elimino
|
||||
int idFabricante = Integer.parseInt(this.jtfId.getText());
|
||||
|
||||
if (ControladorFabricante.eliminaFabricante(idFabricante) > 0) {
|
||||
// Navego al anterior, si existe
|
||||
Fabricante anterior =
|
||||
ControladorFabricante.getAnterior(idFabricante);
|
||||
if (anterior != null) { // Existe un anterior
|
||||
mostrarFabricanteEnPantalla(anterior);
|
||||
}
|
||||
else { // No existe anterior
|
||||
Fabricante siguiente =
|
||||
ControladorFabricante.getSiguiente(idFabricante);
|
||||
if (siguiente != null) { // Existe un siguiente
|
||||
mostrarFabricanteEnPantalla(siguiente);
|
||||
}
|
||||
else { // No existe siguiente ni anterior, no queda na
|
||||
nuevo();
|
||||
}
|
||||
}
|
||||
|
||||
JOptionPane.showMessageDialog(null, "Registro eliminado");
|
||||
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Error en la eliminación");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user