feat(ch 9): ex 1 más avanzado. Falta eliminar

This commit is contained in:
Rafa Muñoz
2025-03-11 15:00:09 +01:00
parent c054a8c34b
commit d8c4fa5e4b
2 changed files with 216 additions and 18 deletions

View File

@@ -2,7 +2,9 @@ package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlad
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fabricante; import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fabricante;
@@ -10,14 +12,33 @@ import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fab
public class ControladorFabricante { public class ControladorFabricante {
public Fabricante getPrimerFabricante() { public static Fabricante getPrimero() {
return getFabricante("Select * from fabricante order by id asc limit 1");
}
public static Fabricante getUltimo() {
return getFabricante("Select * from fabricante order by id desc limit 1");
}
public static Fabricante getSiguiente(int idActual) {
return getFabricante("Select * from fabricante "
+ "where id > " + idActual + " order by id asc limit 1");
}
public static Fabricante getAnterior(int idActual) {
return getFabricante("Select * from fabricante "
+ "where id < " + idActual + " order by id desc limit 1");
}
public static Fabricante getFabricante(String sql) {
try { try {
Connection conn = ConnectionManager.getConnection(); Connection conn = ConnectionManager.getConnection();
Statement s = conn.createStatement(); Statement s = conn.createStatement();
ResultSet rs = ResultSet rs =
s.executeQuery("Select * from fabricante order " s.executeQuery(sql);
+ "by id asc limit 1");
if (rs.next()) { if (rs.next()) {
Fabricante f = new Fabricante(); Fabricante f = new Fabricante();
@@ -32,6 +53,68 @@ public class ControladorFabricante {
} }
return null; return null;
} }
public static int modificaFabricante(Fabricante f) {
try {
Connection conn = ConnectionManager.getConnection();
String sql = "update tutorialjavacoches.fabricante "
+ "set nombre = ?, cif = ? "
+ "where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, f.getNombre());
ps.setString(2, f.getCif());
ps.setInt(3, f.getId());
return ps.executeUpdate();
}
catch (Exception ex) {
ex.printStackTrace();
}
return 0;
}
/**
*
* @return
* @throws SQLException
*/
private static int getSiguienteIdValido(Connection conn) throws SQLException {
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select max(id) as maximoId "
+ "from fabricante");
if (rs.next()) {
return rs.getInt(1) + 1;
}
return 1;
}
public static int insertaFabricante(Fabricante f) {
try {
Connection conn = ConnectionManager.getConnection();
String sql = "insert into fabricante (id, nombre, cif) "
+ "values (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, getSiguienteIdValido(conn));
ps.setString(2, f.getNombre());
ps.setString(3, f.getCif());
return ps.executeUpdate();
}
catch (Exception ex) {
ex.printStackTrace();
}
return 0;
}
} }

View File

@@ -11,6 +11,8 @@ import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fab
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.Font; import java.awt.Font;
import java.awt.Insets; import java.awt.Insets;
@@ -27,6 +29,10 @@ public class VentanaGestionFabricante extends JFrame {
private JTextField jtfId; private JTextField jtfId;
private JTextField jtfNombre; private JTextField jtfNombre;
private JTextField jtfCif; private JTextField jtfCif;
private JButton btnPrimero;
private JButton btnAnterior;
private JButton btnSiguiente;
private JButton btnUltimo;
/** /**
* Launch the application. * Launch the application.
@@ -80,27 +86,43 @@ public class VentanaGestionFabricante extends JFrame {
gbc_panel.gridy = 1; gbc_panel.gridy = 1;
contentPane.add(panel, gbc_panel); contentPane.add(panel, gbc_panel);
JButton btnNewButton = new JButton("Primero"); btnPrimero = new JButton("Primero");
btnNewButton.addActionListener(new ActionListener() { btnPrimero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Fabricante fab = mostrarFabricanteEnPantalla(
new ControladorFabricante().getPrimerFabricante(); ControladorFabricante.getPrimero());
jtfId.setText("" + fab.getId());
jtfNombre.setText(fab.getNombre());
jtfCif.setText(fab.getCif());
System.out.println("Fab nombre: " + fab.getNombre());
} }
}); });
panel.add(btnNewButton); panel.add(btnPrimero);
JButton btnNewButton_1 = new JButton("Anterior"); btnAnterior = new JButton("Anterior");
panel.add(btnNewButton_1); btnAnterior.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mostrarFabricanteEnPantalla(
ControladorFabricante.getAnterior(
Integer.parseInt(jtfId.getText())));
}
});
panel.add(btnAnterior);
JButton btnNewButton_2 = new JButton("Siguiente"); btnSiguiente = new JButton("Siguiente");
panel.add(btnNewButton_2); btnSiguiente.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mostrarFabricanteEnPantalla(
ControladorFabricante.getSiguiente(
Integer.parseInt(jtfId.getText())));
}
});
panel.add(btnSiguiente);
JButton btnNewButton_3 = new JButton("Último"); btnUltimo = new JButton("Último");
panel.add(btnNewButton_3); btnUltimo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mostrarFabricanteEnPantalla(
ControladorFabricante.getUltimo());
}
});
panel.add(btnUltimo);
JLabel lblNewLabel_1 = new JLabel("Id:"); JLabel lblNewLabel_1 = new JLabel("Id:");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
@@ -111,6 +133,7 @@ public class VentanaGestionFabricante extends JFrame {
contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1); contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);
jtfId = new JTextField(); jtfId = new JTextField();
jtfId.setEnabled(false);
GridBagConstraints gbc_jtfId = new GridBagConstraints(); GridBagConstraints gbc_jtfId = new GridBagConstraints();
gbc_jtfId.insets = new Insets(0, 0, 5, 0); gbc_jtfId.insets = new Insets(0, 0, 5, 0);
gbc_jtfId.fill = GridBagConstraints.HORIZONTAL; gbc_jtfId.fill = GridBagConstraints.HORIZONTAL;
@@ -164,13 +187,105 @@ public class VentanaGestionFabricante extends JFrame {
contentPane.add(panel_1, gbc_panel_1); contentPane.add(panel_1, gbc_panel_1);
JButton btnNewButton_4 = new JButton("Guardar"); JButton btnNewButton_4 = new JButton("Guardar");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
guardar();
}
});
panel_1.add(btnNewButton_4); panel_1.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("Nuevo"); JButton btnNewButton_5 = new JButton("Nuevo");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nuevo();
}
});
panel_1.add(btnNewButton_5); panel_1.add(btnNewButton_5);
JButton btnNewButton_6 = new JButton("Eliminar"); JButton btnNewButton_6 = new JButton("Eliminar");
panel_1.add(btnNewButton_6); panel_1.add(btnNewButton_6);
mostrarFabricanteEnPantalla(ControladorFabricante.getPrimero());
} }
/**
*
* @param f
*/
private void mostrarFabricanteEnPantalla(Fabricante f) {
if (f != null) {
jtfId.setText("" + f.getId());
jtfNombre.setText(f.getNombre());
jtfCif.setText(f.getCif());
// Compruebo si existe un siguiente
boolean existeSiguiente =
ControladorFabricante.getSiguiente(f.getId()) != null;
this.btnSiguiente.setEnabled(existeSiguiente);
this.btnUltimo.setEnabled(existeSiguiente);
// Compruebo si existe un anterior
boolean existeAnterior =
ControladorFabricante.getAnterior(f.getId()) != null;
this.btnAnterior.setEnabled(existeAnterior);
this.btnPrimero.setEnabled(existeAnterior);
}
}
private Fabricante getDesdePantalla() {
Fabricante f = new Fabricante();
f.setId(Integer.parseInt(this.jtfId.getText()));
f.setNombre(this.jtfNombre.getText());
f.setCif(this.jtfCif.getText());
return f;
}
private void guardar() {
int regModificados;
Fabricante f = getDesdePantalla();
if (f.getId() == 0) { // Es una inserción
regModificados =
ControladorFabricante.insertaFabricante(getDesdePantalla());
mostrarFabricanteEnPantalla(ControladorFabricante.getUltimo());
}
else {
regModificados =
ControladorFabricante.modificaFabricante(getDesdePantalla());
}
if (regModificados > 0) {
JOptionPane.showMessageDialog(null, "Registro modificado");
}
else {
JOptionPane.showMessageDialog(null, "Error en la modificación");
}
}
/**
*
*/
private void nuevo() {
this.jtfId.setText("0");
this.jtfNombre.setText("");
this.jtfCif.setText("");
}
} }