diff --git a/src/tutorialJava/capitulo8_Acceso_A_Datos/mysql/Ejemplo03_EjemplosInsertUpdateyDeleteConPreparedStatement.java b/src/tutorialJava/capitulo8_Acceso_A_Datos/mysql/Ejemplo03_EjemplosInsertUpdateyDeleteConPreparedStatement.java index e6cd673..f07f309 100644 --- a/src/tutorialJava/capitulo8_Acceso_A_Datos/mysql/Ejemplo03_EjemplosInsertUpdateyDeleteConPreparedStatement.java +++ b/src/tutorialJava/capitulo8_Acceso_A_Datos/mysql/Ejemplo03_EjemplosInsertUpdateyDeleteConPreparedStatement.java @@ -109,16 +109,21 @@ public class Ejemplo03_EjemplosInsertUpdateyDeleteConPreparedStatement { private static void realizaUpdate (Connection conn, String nombreMod, String localidadMod, int idMod) throws SQLException { - Statement s = (Statement) conn.createStatement(); + PreparedStatement ps = conn.prepareStatement("update tutorialjavacoches.concesionario " + + "set nombre = ?, " + + "localidad = ? " + + "where id = ?"); + ps.setString(1, "Concesionario kk"); + ps.setString(2, "kk"); + ps.setInt(3, 22); + - int filasAfectadas = s.executeUpdate("update tutorialjavacoches.concesionario " - + "set nombre = '" + nombreMod + "', " - + "localidad = '" + localidadMod + "'\r\n" - + "where id = " + idMod); + int filasAfectadas = ps.executeUpdate(); + System.out.println("Filas afectadas: " + filasAfectadas); - s.close(); + ps.close(); } @@ -129,15 +134,17 @@ public class Ejemplo03_EjemplosInsertUpdateyDeleteConPreparedStatement { */ private static void realizaDelete (Connection conn, int idMod) throws SQLException { - Statement s = (Statement) conn.createStatement(); - - int filasAfectadas = s.executeUpdate("Delete from " + PreparedStatement ps = conn.prepareStatement("Delete from " + "tutorialjavacoches.concesionario " - + "where id = " + idMod); + + "where id = ?"); + + ps.setInt(1, 22); + + int filasAfectadas = ps.executeUpdate(); System.out.println("Filas afectadas: " + filasAfectadas); - s.close(); + ps.close(); } diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/JFrameKK.java b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/JFrameKK.java new file mode 100644 index 0000000..864ee5a --- /dev/null +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/JFrameKK.java @@ -0,0 +1,42 @@ +package tutorialJava.capitulo9_AWT_SWING.ejemplos; + +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +public class JFrameKK extends JFrame { + + private static final long serialVersionUID = 1L; + private JPanel contentPane; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + JFrameKK frame = new JFrameKK(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public JFrameKK() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 450, 300); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + + setContentPane(contentPane); + } + +} 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 new file mode 100644 index 0000000..9f0351b --- /dev/null +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/ControladorFabricante.java @@ -0,0 +1,62 @@ +package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlador; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fabricante; + +public class ControladorFabricante { + + private Connection getConexion () throws Exception { + String driver = JDBCPropiedades.getProperty("JDBC_DRIVER_CLASS"); + String user = JDBCPropiedades.getProperty("JDBC_USER"); + String password = JDBCPropiedades.getProperty("JDBC_PASSWORD"); + String host = JDBCPropiedades.getProperty("JDBC_HOST"); + String schema = JDBCPropiedades.getProperty("JDBC_SCHEMA_NAME"); + String properties = JDBCPropiedades.getProperty("JDBC_PROPERTIES"); + + Class.forName(driver); + + Connection conexion = (Connection) DriverManager.getConnection ( + "jdbc:mysql://" + host + "/" + schema + properties, + user, password); + + return conexion; + } + + + public Fabricante getPrimerFabricante() { + try { + Connection conn = getConexion(); + + Statement s = conn.createStatement(); + ResultSet rs = + s.executeQuery("Select * from fabricante order " + + "by id asc limit 1"); + + if (rs.next()) { + Fabricante f = new Fabricante(); + f.setId(rs.getInt("id")); + f.setNombre(rs.getString("nombre")); + f.setCif(rs.getString("cif")); + return f; + } + } + catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } +} + + + + + + + + + + diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/JDBCPropiedades.java b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/JDBCPropiedades.java new file mode 100644 index 0000000..0a53ed0 --- /dev/null +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/JDBCPropiedades.java @@ -0,0 +1,69 @@ +package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlador; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Properties; + +public class JDBCPropiedades { + + private static Properties propiedades = null; + + public JDBCPropiedades () { + super(); + } + + /** + * + * @return + */ + private static Properties getPropiedades() { + if (propiedades == null) { + propiedades = new Properties(); + + try { + File file = new File("./src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/jdbc.properties"); + propiedades.load(new FileReader(file)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + return propiedades; + } + + /** + * + * @param nombrePropiedad + * @return + */ + public static String getProperty(String nombrePropiedad) { + return getPropiedades().getProperty(nombrePropiedad); + } + + + /** + * + * @param nombrePropiedad + * @return + */ + public static int getIntProperty (String nombrePropiedad) { + return Integer.parseInt(getPropiedades().getProperty(nombrePropiedad)); + } + + + /** + * + * @param nombrePropiedad + * @return + */ + public static Float getFloatProperty (String nombrePropiedad) { + return Float.parseFloat(getPropiedades().getProperty(nombrePropiedad)); + } + +} diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/jdbc.properties b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/jdbc.properties new file mode 100644 index 0000000..193ef37 --- /dev/null +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/controlador/jdbc.properties @@ -0,0 +1,7 @@ +# parámetros de conexión a BBDD +JDBC_DRIVER_CLASS=com.mysql.cj.jdbc.Driver +JDBC_USER=root +JDBC_PASSWORD=1234 +JDBC_HOST=localhost:3306 +JDBC_SCHEMA_NAME=tutorialjavacoches +JDBC_PROPERTIES=?autoReconnect=true&serverTimezone=Europe/Madrid&useSSL=False&allowPublicKeyRetrieval=TRUE diff --git a/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/modelo/Fabricante.java b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/modelo/Fabricante.java new file mode 100644 index 0000000..5f925c1 --- /dev/null +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/modelo/Fabricante.java @@ -0,0 +1,50 @@ +package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo; + +public class Fabricante { + + private int id; + private String nombre; + private String cif; + + public Fabricante() { + super(); + } + + public Fabricante(int id, String nombre, String cif) { + super(); + this.id = id; + this.nombre = nombre; + this.cif = cif; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + public String getCif() { + return cif; + } + + public void setCif(String cif) { + this.cif = cif; + } + + @Override + public String toString() { + return "Fabricante [id=" + id + ", nombre=" + nombre + ", cif=" + cif + "]"; + } + + +} 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 new file mode 100644 index 0000000..bd409d1 --- /dev/null +++ b/src/tutorialJava/capitulo9_AWT_SWING/ejemplos/ejemplo01_Fabricante/vista/VentanaGestionFabricante.java @@ -0,0 +1,176 @@ +package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.vista; + +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlador.ControladorFabricante; +import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fabricante; + +import java.awt.GridBagLayout; +import javax.swing.JLabel; +import java.awt.GridBagConstraints; +import java.awt.Font; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JTextField; +import javax.swing.JButton; + +public class VentanaGestionFabricante extends JFrame { + + private static final long serialVersionUID = 1L; + private JPanel contentPane; + private JTextField jtfId; + private JTextField jtfNombre; + private JTextField jtfCif; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + VentanaGestionFabricante frame = new VentanaGestionFabricante(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public VentanaGestionFabricante() { + setTitle("Gestión de Fabricantes"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 317, 300); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + + setContentPane(contentPane); + GridBagLayout gbl_contentPane = new GridBagLayout(); + gbl_contentPane.columnWidths = new int[]{0, 0, 0}; + gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0}; + gbl_contentPane.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE}; + gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE}; + contentPane.setLayout(gbl_contentPane); + + JLabel lblNewLabel = new JLabel("Gestión de Fabricantes"); + lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16)); + GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); + gbc_lblNewLabel.gridwidth = 2; + gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0); + gbc_lblNewLabel.gridx = 0; + gbc_lblNewLabel.gridy = 0; + contentPane.add(lblNewLabel, gbc_lblNewLabel); + + JPanel panel = new JPanel(); + GridBagConstraints gbc_panel = new GridBagConstraints(); + gbc_panel.gridwidth = 2; + gbc_panel.insets = new Insets(0, 0, 5, 0); + gbc_panel.fill = GridBagConstraints.BOTH; + gbc_panel.gridx = 0; + gbc_panel.gridy = 1; + contentPane.add(panel, gbc_panel); + + JButton btnNewButton = new JButton("Primero"); + btnNewButton.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()); + } + }); + panel.add(btnNewButton); + + JButton btnNewButton_1 = new JButton("Anterior"); + panel.add(btnNewButton_1); + + JButton btnNewButton_2 = new JButton("Siguiente"); + panel.add(btnNewButton_2); + + JButton btnNewButton_3 = new JButton("Último"); + panel.add(btnNewButton_3); + + JLabel lblNewLabel_1 = new JLabel("Id:"); + GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); + gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_1.gridx = 0; + gbc_lblNewLabel_1.gridy = 2; + contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1); + + jtfId = new JTextField(); + GridBagConstraints gbc_jtfId = new GridBagConstraints(); + gbc_jtfId.insets = new Insets(0, 0, 5, 0); + gbc_jtfId.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfId.gridx = 1; + gbc_jtfId.gridy = 2; + contentPane.add(jtfId, gbc_jtfId); + jtfId.setColumns(10); + + JLabel lblNewLabel_2 = new JLabel("Nombre:"); + GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints(); + gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_2.gridx = 0; + gbc_lblNewLabel_2.gridy = 3; + contentPane.add(lblNewLabel_2, gbc_lblNewLabel_2); + + jtfNombre = new JTextField(); + GridBagConstraints gbc_jtfNombre = new GridBagConstraints(); + gbc_jtfNombre.insets = new Insets(0, 0, 5, 0); + gbc_jtfNombre.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfNombre.gridx = 1; + gbc_jtfNombre.gridy = 3; + contentPane.add(jtfNombre, gbc_jtfNombre); + jtfNombre.setColumns(10); + + JLabel lblNewLabel_3 = new JLabel("CIF:"); + GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints(); + gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST; + gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5); + gbc_lblNewLabel_3.gridx = 0; + gbc_lblNewLabel_3.gridy = 4; + contentPane.add(lblNewLabel_3, gbc_lblNewLabel_3); + + jtfCif = new JTextField(); + GridBagConstraints gbc_jtfCif = new GridBagConstraints(); + gbc_jtfCif.insets = new Insets(0, 0, 5, 0); + gbc_jtfCif.fill = GridBagConstraints.HORIZONTAL; + gbc_jtfCif.gridx = 1; + gbc_jtfCif.gridy = 4; + contentPane.add(jtfCif, gbc_jtfCif); + jtfCif.setColumns(10); + + JPanel panel_1 = new JPanel(); + GridBagConstraints gbc_panel_1 = new GridBagConstraints(); + gbc_panel_1.weighty = 1.0; + gbc_panel_1.gridwidth = 2; + gbc_panel_1.insets = new Insets(0, 0, 0, 5); + gbc_panel_1.fill = GridBagConstraints.BOTH; + gbc_panel_1.gridx = 0; + gbc_panel_1.gridy = 5; + contentPane.add(panel_1, gbc_panel_1); + + JButton btnNewButton_4 = new JButton("Guardar"); + panel_1.add(btnNewButton_4); + + JButton btnNewButton_5 = new JButton("Nuevo"); + panel_1.add(btnNewButton_5); + + JButton btnNewButton_6 = new JButton("Eliminar"); + panel_1.add(btnNewButton_6); + } + +}