diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/ControladorFabricante.java b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/ControladorFabricante.java index 2893b39..d1193e9 100644 --- a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/ControladorFabricante.java +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/ControladorFabricante.java @@ -2,7 +2,9 @@ package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlad import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; 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 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 { Connection conn = ConnectionManager.getConnection(); Statement s = conn.createStatement(); ResultSet rs = - s.executeQuery("Select * from fabricante order " - + "by id asc limit 1"); + s.executeQuery(sql); if (rs.next()) { Fabricante f = new Fabricante(); @@ -32,6 +53,68 @@ public class ControladorFabricante { } 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; + } + } diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/vista/VentanaGestionFabricante.java b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/vista/VentanaGestionFabricante.java index bd409d1..2d8809c 100644 --- a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/vista/VentanaGestionFabricante.java +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/vista/VentanaGestionFabricante.java @@ -11,6 +11,8 @@ import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fab import java.awt.GridBagLayout; import javax.swing.JLabel; +import javax.swing.JOptionPane; + import java.awt.GridBagConstraints; import java.awt.Font; import java.awt.Insets; @@ -27,6 +29,10 @@ public class VentanaGestionFabricante extends JFrame { private JTextField jtfId; private JTextField jtfNombre; private JTextField jtfCif; + private JButton btnPrimero; + private JButton btnAnterior; + private JButton btnSiguiente; + private JButton btnUltimo; /** * Launch the application. @@ -80,27 +86,43 @@ public class VentanaGestionFabricante extends JFrame { gbc_panel.gridy = 1; contentPane.add(panel, gbc_panel); - JButton btnNewButton = new JButton("Primero"); - btnNewButton.addActionListener(new ActionListener() { + btnPrimero = new JButton("Primero"); + btnPrimero.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - Fabricante fab = - new ControladorFabricante().getPrimerFabricante(); - jtfId.setText("" + fab.getId()); - jtfNombre.setText(fab.getNombre()); - jtfCif.setText(fab.getCif()); - System.out.println("Fab nombre: " + fab.getNombre()); + mostrarFabricanteEnPantalla( + ControladorFabricante.getPrimero()); } }); - panel.add(btnNewButton); + panel.add(btnPrimero); - JButton btnNewButton_1 = new JButton("Anterior"); - panel.add(btnNewButton_1); + btnAnterior = new JButton("Anterior"); + 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"); - panel.add(btnNewButton_2); + btnSiguiente = new JButton("Siguiente"); + 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"); - panel.add(btnNewButton_3); + btnUltimo = new JButton("Último"); + btnUltimo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + mostrarFabricanteEnPantalla( + ControladorFabricante.getUltimo()); + } + }); + panel.add(btnUltimo); JLabel lblNewLabel_1 = new JLabel("Id:"); GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); @@ -111,6 +133,7 @@ public class VentanaGestionFabricante extends JFrame { contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1); jtfId = new JTextField(); + jtfId.setEnabled(false); GridBagConstraints gbc_jtfId = new GridBagConstraints(); gbc_jtfId.insets = new Insets(0, 0, 5, 0); gbc_jtfId.fill = GridBagConstraints.HORIZONTAL; @@ -164,13 +187,105 @@ public class VentanaGestionFabricante extends JFrame { contentPane.add(panel_1, gbc_panel_1); JButton btnNewButton_4 = new JButton("Guardar"); + btnNewButton_4.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + guardar(); + } + }); panel_1.add(btnNewButton_4); JButton btnNewButton_5 = new JButton("Nuevo"); + btnNewButton_5.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + nuevo(); + } + }); panel_1.add(btnNewButton_5); JButton btnNewButton_6 = new JButton("Eliminar"); 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(""); + } + } + + + + + + + + + + +