Ejercicios capítulo 9 bloque 1
This commit is contained in:
3
.idea/uiDesigner.xml
generated
3
.idea/uiDesigner.xml
generated
@@ -121,4 +121,7 @@
|
|||||||
</item>
|
</item>
|
||||||
</group>
|
</group>
|
||||||
</component>
|
</component>
|
||||||
|
<component name="uidesigner-configuration">
|
||||||
|
<option name="DEFAULT_LAYOUT_MANAGER" value="BorderLayout" />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
||||||
9
Ejercicios.eml
Normal file
9
Ejercicios.eml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<component inherit-compiler-output="true" inheritJdk="true">
|
||||||
|
<output-test url="file://$MODULE_DIR$/out/test/Ejercicios"/>
|
||||||
|
<exclude-output/>
|
||||||
|
<contentEntry url="file://$MODULE_DIR$"/>
|
||||||
|
<levels>
|
||||||
|
<level name="librerias" value="project"/>
|
||||||
|
</levels>
|
||||||
|
</component>
|
||||||
22
EjerciciosProgramacion.userlibraries
Normal file
22
EjerciciosProgramacion.userlibraries
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<eclipse-userlibraries>
|
||||||
|
<library name="librerias">
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/JDBC_Oracle_Connection_Pool/ucp.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/Driver_MySQL_Connector_J/mysql-connector-java-8.0.19.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/mongoDB-Java-driver/mongo-java-driver-3.12.12.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/eclipselink.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/jpa/org.eclipse.persistence.jpars_2.5.2.v20140319-9ad6abd.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/jpa/org.eclipse.persistence.jpa.modelgen_2.5.2.v20140319-9ad6abd.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/jpa/javax.persistence_2.1.0.v201304241213.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/moxy/javax.xml.stream_1.0.1.v201004272200.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/moxy/javax.xml.bind_2.2.0.v201105210648.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/moxy/javax.mail_1.4.0.v201005080615.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/moxy/javax.activation_1.1.0.v201108011116.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/moxy/com.sun.xml.bind_2.2.0.v201004141950.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/EclipseLink2.5.2/jlib/moxy/com.sun.tools.xjc_2.2.0.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/Jackson/jackson-databind-2.9.5.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/Jackson/jackson-core-2.9.5.jar" />
|
||||||
|
<archive path="/Users/h4ckx0r/IdeaProjects/EjerciciosProgramacion/librerias/Jackson/jackson-annotations-2.9.0.jar" />
|
||||||
|
</library>
|
||||||
|
</eclipse-userlibraries>
|
||||||
|
|
||||||
96
src/capitulo09/bloque01/controlador/ControladorCliente.java
Normal file
96
src/capitulo09/bloque01/controlador/ControladorCliente.java
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package capitulo09.bloque01.controlador;
|
||||||
|
|
||||||
|
import capitulo09.bloque01.modelo.Cliente;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
public class ControladorCliente {
|
||||||
|
public static Cliente getPrimerCliente() {
|
||||||
|
return getClienteInterno(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Cliente getUltimoCliente() {
|
||||||
|
return getClienteInterno(-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Cliente getCliente(int id) {
|
||||||
|
return getClienteInterno(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Cliente getClienteInterno(int id) {
|
||||||
|
Connection conn = PropiedadesBBDD.getConnection();
|
||||||
|
ResultSet res = null;
|
||||||
|
try {
|
||||||
|
PreparedStatement stId = conn.prepareStatement("select * from cliente where id = ? limit 1");
|
||||||
|
Statement stPrimeroUltimo = conn.createStatement();
|
||||||
|
|
||||||
|
if (id == -1) {
|
||||||
|
res = stPrimeroUltimo.executeQuery("select * from cliente order by id asc limit 1");
|
||||||
|
} else if (id == -2) {
|
||||||
|
res = stPrimeroUltimo.executeQuery("select * from cliente order by id desc limit 1");
|
||||||
|
} else {
|
||||||
|
stId.setInt(1, id);
|
||||||
|
res = stId.executeQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (res.next()) {
|
||||||
|
return new Cliente(res.getInt("id"), res.getString("nombre"), res.getString("apellidos"), res.getString("localidad"), res.getString("dniNie"), res.getDate("fechaNac"), res.getBoolean("activo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int actualizarCliente(Cliente cliente) {
|
||||||
|
Connection conn = PropiedadesBBDD.getConnection();
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = conn.prepareStatement("update cliente set nombre = ?, apellidos = ?, localidad = ?, dniNie = ?, fechaNac = ?, activo = ? where id = ?");
|
||||||
|
|
||||||
|
ps.setString(1, cliente.getNombre());
|
||||||
|
ps.setString(2, cliente.getApellidos());
|
||||||
|
ps.setString(3, cliente.getLocalidad());
|
||||||
|
ps.setString(4, cliente.getDniNie());
|
||||||
|
ps.setDate(5, new Date(cliente.getFechaNac().getTime()));
|
||||||
|
ps.setBoolean(6, cliente.isActivo());
|
||||||
|
ps.setInt(7, cliente.getId());
|
||||||
|
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int nuevoCliente(Cliente cliente) {
|
||||||
|
Connection conn = PropiedadesBBDD.getConnection();
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = conn.prepareStatement("insert into cliente (id, nombre, apellidos, localidad, dniNie, fechaNac, activo) values (?, ?, ?, ?, ?, ?, ?)");
|
||||||
|
|
||||||
|
ps.setInt(1, cliente.getId());
|
||||||
|
ps.setString(2, cliente.getNombre());
|
||||||
|
ps.setString(3, cliente.getApellidos());
|
||||||
|
ps.setString(4, cliente.getLocalidad());
|
||||||
|
ps.setString(5, cliente.getDniNie());
|
||||||
|
ps.setDate(6, new Date(cliente.getFechaNac().getTime()));
|
||||||
|
ps.setBoolean(7, cliente.isActivo());
|
||||||
|
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int eliminarCliente(int id) {
|
||||||
|
Connection conn = PropiedadesBBDD.getConnection();
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = conn.prepareStatement("delete from cliente where id = ?");
|
||||||
|
ps.setInt(1, id);
|
||||||
|
return ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
58
src/capitulo09/bloque01/controlador/PropiedadesBBDD.java
Normal file
58
src/capitulo09/bloque01/controlador/PropiedadesBBDD.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package capitulo09.bloque01.controlador;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class PropiedadesBBDD {
|
||||||
|
|
||||||
|
private static Properties propiedades;
|
||||||
|
private static Connection openConnection;
|
||||||
|
|
||||||
|
private static Properties getPropiedades() {
|
||||||
|
if (propiedades == null) {
|
||||||
|
propiedades = new Properties();
|
||||||
|
|
||||||
|
try {
|
||||||
|
File file = new File("./out/production/Ejercicios/capitulo09/bloque01/controlador/mysql.properties");
|
||||||
|
propiedades.load(new FileReader(file));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return propiedades;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getMySQLConnectionString() {
|
||||||
|
String host = getProperty("MYSQL_HOST");
|
||||||
|
String schema = getProperty("MYSQL_SCHEMA_NAME");
|
||||||
|
String properties = getProperty("MYSQL_PROPERTIES");
|
||||||
|
String user = getProperty("MYSQL_USER");
|
||||||
|
String password = getProperty("MYSQL_PASSWORD");
|
||||||
|
|
||||||
|
return "jdbc:mysql://" + user + ":" + password + "@" + host + "/" + schema + properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() {
|
||||||
|
if (openConnection == null) {
|
||||||
|
try {
|
||||||
|
openConnection = DriverManager.getConnection(getMySQLConnectionString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return openConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getMySQLDriver() {
|
||||||
|
return getProperty("MYSQL_DRIVER_CLASS");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getProperty(String nombrePropiedad) {
|
||||||
|
return getPropiedades().getProperty(nombrePropiedad);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
6
src/capitulo09/bloque01/controlador/mysql.properties
Normal file
6
src/capitulo09/bloque01/controlador/mysql.properties
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
MYSQL_DRIVER_CLASS=com.mysql.cj.jdbc.Driver
|
||||||
|
MYSQL_USER=root
|
||||||
|
MYSQL_PASSWORD=1234
|
||||||
|
MYSQL_HOST=127.0.0.1:3310
|
||||||
|
MYSQL_SCHEMA_NAME=tutorialjavacoches
|
||||||
|
MYSQL_PROPERTIES=?autoReconnect=true&serverTimezone=Europe/Madrid&useSSL=False&allowPublicKeyRetrieval=TRUE
|
||||||
95
src/capitulo09/bloque01/modelo/Cliente.java
Normal file
95
src/capitulo09/bloque01/modelo/Cliente.java
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package capitulo09.bloque01.modelo;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Cliente {
|
||||||
|
private int id;
|
||||||
|
private String nombre;
|
||||||
|
private String apellidos;
|
||||||
|
private String localidad;
|
||||||
|
private String dniNie;
|
||||||
|
private Date fechaNac;
|
||||||
|
private boolean activo;
|
||||||
|
|
||||||
|
public Cliente() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cliente(int id, String nombre, String apellidos, String localidad, String dniNie, Date fechaNac, boolean activo) {
|
||||||
|
this.id = id;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.apellidos = apellidos;
|
||||||
|
this.localidad = localidad;
|
||||||
|
this.dniNie = dniNie;
|
||||||
|
this.fechaNac = fechaNac;
|
||||||
|
this.activo = activo;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 getApellidos() {
|
||||||
|
return apellidos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApellidos(String apellidos) {
|
||||||
|
this.apellidos = apellidos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLocalidad() {
|
||||||
|
return localidad;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocalidad(String localidad) {
|
||||||
|
this.localidad = localidad;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDniNie() {
|
||||||
|
return dniNie;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDniNie(String dniNie) {
|
||||||
|
this.dniNie = dniNie;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getFechaNac() {
|
||||||
|
return fechaNac;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFechaNac(Date fechaNac) {
|
||||||
|
this.fechaNac = fechaNac;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isActivo() {
|
||||||
|
return activo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActivo(boolean activo) {
|
||||||
|
this.activo = activo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Cliente{" +
|
||||||
|
"id=" + id +
|
||||||
|
", nombre='" + nombre + '\'' +
|
||||||
|
", apellidos='" + apellidos + '\'' +
|
||||||
|
", localidad='" + localidad + '\'' +
|
||||||
|
", dniNie='" + dniNie + '\'' +
|
||||||
|
", fechaNac=" + fechaNac +
|
||||||
|
", activo=" + activo +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
381
src/capitulo09/bloque01/vista/GestionCliente.java
Normal file
381
src/capitulo09/bloque01/vista/GestionCliente.java
Normal file
@@ -0,0 +1,381 @@
|
|||||||
|
package capitulo09.bloque01.vista;
|
||||||
|
|
||||||
|
import capitulo09.bloque01.controlador.ControladorCliente;
|
||||||
|
import capitulo09.bloque01.modelo.Cliente;
|
||||||
|
|
||||||
|
import java.awt.EventQueue;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
|
import java.awt.GridBagConstraints;
|
||||||
|
import java.awt.Insets;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class GestionCliente extends JFrame {
|
||||||
|
|
||||||
|
Cliente clienteActual;
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private JPanel contentPane;
|
||||||
|
private JTextField jtfNombre;
|
||||||
|
private JTextField jtfApellidos;
|
||||||
|
private JTextField jtfLocalidad;
|
||||||
|
private JTextField jtfDniNie;
|
||||||
|
private JTextField jtfFechaNac;
|
||||||
|
private JTextField jtfId;
|
||||||
|
private JCheckBox checkBoxEstado;
|
||||||
|
|
||||||
|
private JButton btnPrimero;
|
||||||
|
private JButton btnAnterior;
|
||||||
|
private JButton btnSiguiente;
|
||||||
|
private JButton btnUltimo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Launch the application.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
EventQueue.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
GestionCliente frame = new GestionCliente();
|
||||||
|
frame.actualizarDatosEnPantalla(ControladorCliente.getPrimerCliente());
|
||||||
|
frame.setVisible(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void actualizarDatosEnPantalla(Cliente cliente) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
|
||||||
|
|
||||||
|
jtfId.setText("" + cliente.getId());
|
||||||
|
jtfNombre.setText(cliente.getNombre());
|
||||||
|
jtfApellidos.setText(cliente.getApellidos());
|
||||||
|
jtfLocalidad.setText(cliente.getLocalidad());
|
||||||
|
jtfDniNie.setText(cliente.getDniNie());
|
||||||
|
jtfFechaNac.setText(sdf.format(cliente.getFechaNac()));
|
||||||
|
checkBoxEstado.setSelected(cliente.isActivo());
|
||||||
|
clienteActual = cliente;
|
||||||
|
|
||||||
|
if (cliente.getId() == ControladorCliente.getPrimerCliente().getId()) {
|
||||||
|
btnPrimero.setEnabled(false);
|
||||||
|
btnAnterior.setEnabled(false);
|
||||||
|
btnSiguiente.setEnabled(true);
|
||||||
|
btnUltimo.setEnabled(true);
|
||||||
|
} else if (cliente.getId() == ControladorCliente.getUltimoCliente().getId()) {
|
||||||
|
btnPrimero.setEnabled(true);
|
||||||
|
btnAnterior.setEnabled(true);
|
||||||
|
btnSiguiente.setEnabled(false);
|
||||||
|
btnUltimo.setEnabled(false);
|
||||||
|
} else {
|
||||||
|
btnPrimero.setEnabled(true);
|
||||||
|
btnAnterior.setEnabled(true);
|
||||||
|
btnSiguiente.setEnabled(true);
|
||||||
|
btnUltimo.setEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the frame.
|
||||||
|
*/
|
||||||
|
public GestionCliente() {
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setBounds(100, 100, 500, 350);
|
||||||
|
contentPane = new JPanel();
|
||||||
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
|
setContentPane(contentPane);
|
||||||
|
contentPane.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
|
JPanel panel_1 = new JPanel();
|
||||||
|
contentPane.add(panel_1, BorderLayout.NORTH);
|
||||||
|
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
|
JLabel lblTitulo = new JLabel("Gestión de Clientes");
|
||||||
|
lblTitulo.setFont(new Font("Verdana", Font.BOLD, 18));
|
||||||
|
lblTitulo.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
panel_1.add(lblTitulo);
|
||||||
|
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
panel_1.add(panel);
|
||||||
|
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
|
||||||
|
|
||||||
|
btnPrimero = new JButton("Primero");
|
||||||
|
btnPrimero.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
actualizarDatosEnPantalla(ControladorCliente.getPrimerCliente());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
btnPrimero.setToolTipText("Muestra en pantalla el primer cliente de la base de datos");
|
||||||
|
panel.add(btnPrimero);
|
||||||
|
|
||||||
|
btnAnterior = new JButton("Anterior");
|
||||||
|
btnAnterior.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
actualizarDatosEnPantalla(ControladorCliente.getCliente((clienteActual.getId() - 1)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
btnAnterior.setToolTipText("Muestra en pantalla el cliente anterior al actual");
|
||||||
|
panel.add(btnAnterior);
|
||||||
|
|
||||||
|
btnSiguiente = new JButton("Siguiente");
|
||||||
|
btnSiguiente.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
actualizarDatosEnPantalla(ControladorCliente.getCliente((clienteActual.getId() + 1)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
btnSiguiente.setToolTipText("Muestra en pantalla el cliente siguiente al actual");
|
||||||
|
panel.add(btnSiguiente);
|
||||||
|
|
||||||
|
btnUltimo = new JButton("Último");
|
||||||
|
btnUltimo.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
actualizarDatosEnPantalla(ControladorCliente.getUltimoCliente());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
btnUltimo.setToolTipText("Muestra en pantalla el ultimo cliente de la base de datos");
|
||||||
|
panel.add(btnUltimo);
|
||||||
|
|
||||||
|
JSeparator separator = new JSeparator();
|
||||||
|
panel_1.add(separator);
|
||||||
|
|
||||||
|
JPanel panel_2 = new JPanel();
|
||||||
|
contentPane.add(panel_2, BorderLayout.CENTER);
|
||||||
|
GridBagLayout gbl_panel_2 = new GridBagLayout();
|
||||||
|
gbl_panel_2.columnWidths = new int[]{0, 0, 0, 0};
|
||||||
|
gbl_panel_2.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
|
||||||
|
gbl_panel_2.columnWeights = new double[]{0.8, 0.5, 1.0, 0.8};
|
||||||
|
gbl_panel_2.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
|
||||||
|
panel_2.setLayout(gbl_panel_2);
|
||||||
|
|
||||||
|
JLabel lblNewLabel = new JLabel("Id");
|
||||||
|
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
|
||||||
|
gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblNewLabel.gridx = 1;
|
||||||
|
gbc_lblNewLabel.gridy = 0;
|
||||||
|
panel_2.add(lblNewLabel, gbc_lblNewLabel);
|
||||||
|
|
||||||
|
jtfId = new JTextField();
|
||||||
|
jtfId.setEditable(false);
|
||||||
|
GridBagConstraints gbc_textField = new GridBagConstraints();
|
||||||
|
gbc_textField.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_textField.gridx = 2;
|
||||||
|
gbc_textField.gridy = 0;
|
||||||
|
panel_2.add(jtfId, gbc_textField);
|
||||||
|
jtfId.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblNewLabel_1 = new JLabel("Nombre");
|
||||||
|
lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||||
|
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
|
||||||
|
gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblNewLabel_1.gridx = 1;
|
||||||
|
gbc_lblNewLabel_1.gridy = 1;
|
||||||
|
panel_2.add(lblNewLabel_1, gbc_lblNewLabel_1);
|
||||||
|
|
||||||
|
jtfNombre = new JTextField();
|
||||||
|
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
|
||||||
|
gbc_textField_1.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_textField_1.gridx = 2;
|
||||||
|
gbc_textField_1.gridy = 1;
|
||||||
|
panel_2.add(jtfNombre, gbc_textField_1);
|
||||||
|
jtfNombre.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblNewLabel_2 = new JLabel("Apellidos");
|
||||||
|
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 = 1;
|
||||||
|
gbc_lblNewLabel_2.gridy = 2;
|
||||||
|
panel_2.add(lblNewLabel_2, gbc_lblNewLabel_2);
|
||||||
|
|
||||||
|
jtfApellidos = new JTextField();
|
||||||
|
GridBagConstraints gbc_textField_2 = new GridBagConstraints();
|
||||||
|
gbc_textField_2.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_textField_2.gridx = 2;
|
||||||
|
gbc_textField_2.gridy = 2;
|
||||||
|
panel_2.add(jtfApellidos, gbc_textField_2);
|
||||||
|
jtfApellidos.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblNewLabel_3 = new JLabel("Localidad");
|
||||||
|
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 = 1;
|
||||||
|
gbc_lblNewLabel_3.gridy = 3;
|
||||||
|
panel_2.add(lblNewLabel_3, gbc_lblNewLabel_3);
|
||||||
|
|
||||||
|
jtfLocalidad = new JTextField();
|
||||||
|
GridBagConstraints gbc_textField_3 = new GridBagConstraints();
|
||||||
|
gbc_textField_3.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_textField_3.gridx = 2;
|
||||||
|
gbc_textField_3.gridy = 3;
|
||||||
|
panel_2.add(jtfLocalidad, gbc_textField_3);
|
||||||
|
jtfLocalidad.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblNewLabel_4 = new JLabel("DNI/NIE");
|
||||||
|
GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
|
||||||
|
gbc_lblNewLabel_4.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblNewLabel_4.gridx = 1;
|
||||||
|
gbc_lblNewLabel_4.gridy = 4;
|
||||||
|
panel_2.add(lblNewLabel_4, gbc_lblNewLabel_4);
|
||||||
|
|
||||||
|
jtfDniNie = new JTextField();
|
||||||
|
GridBagConstraints gbc_textField_4 = new GridBagConstraints();
|
||||||
|
gbc_textField_4.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_textField_4.gridx = 2;
|
||||||
|
gbc_textField_4.gridy = 4;
|
||||||
|
panel_2.add(jtfDniNie, gbc_textField_4);
|
||||||
|
jtfDniNie.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblNewLabel_5 = new JLabel("Fecha de Nacimiento");
|
||||||
|
GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
|
||||||
|
gbc_lblNewLabel_5.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_lblNewLabel_5.gridx = 1;
|
||||||
|
gbc_lblNewLabel_5.gridy = 5;
|
||||||
|
panel_2.add(lblNewLabel_5, gbc_lblNewLabel_5);
|
||||||
|
|
||||||
|
jtfFechaNac = new JTextField();
|
||||||
|
GridBagConstraints gbc_textField_5 = new GridBagConstraints();
|
||||||
|
gbc_textField_5.insets = new Insets(0, 0, 5, 5);
|
||||||
|
gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
|
||||||
|
gbc_textField_5.gridx = 2;
|
||||||
|
gbc_textField_5.gridy = 5;
|
||||||
|
panel_2.add(jtfFechaNac, gbc_textField_5);
|
||||||
|
jtfFechaNac.setColumns(10);
|
||||||
|
|
||||||
|
JLabel lblNewLabel_6 = new JLabel("Estado");
|
||||||
|
GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
|
||||||
|
gbc_lblNewLabel_6.anchor = GridBagConstraints.EAST;
|
||||||
|
gbc_lblNewLabel_6.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_lblNewLabel_6.gridx = 1;
|
||||||
|
gbc_lblNewLabel_6.gridy = 6;
|
||||||
|
panel_2.add(lblNewLabel_6, gbc_lblNewLabel_6);
|
||||||
|
|
||||||
|
checkBoxEstado = new JCheckBox("");
|
||||||
|
GridBagConstraints gbc_chckbxNewCheckBox = new GridBagConstraints();
|
||||||
|
gbc_chckbxNewCheckBox.anchor = GridBagConstraints.WEST;
|
||||||
|
gbc_chckbxNewCheckBox.insets = new Insets(0, 0, 0, 5);
|
||||||
|
gbc_chckbxNewCheckBox.gridx = 2;
|
||||||
|
gbc_chckbxNewCheckBox.gridy = 6;
|
||||||
|
panel_2.add(checkBoxEstado, gbc_chckbxNewCheckBox);
|
||||||
|
|
||||||
|
JPanel panel_3 = new JPanel();
|
||||||
|
contentPane.add(panel_3, BorderLayout.SOUTH);
|
||||||
|
panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
|
JSeparator separator_1 = new JSeparator();
|
||||||
|
panel_3.add(separator_1);
|
||||||
|
|
||||||
|
JPanel panel_4 = new JPanel();
|
||||||
|
panel_3.add(panel_4);
|
||||||
|
panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.X_AXIS));
|
||||||
|
|
||||||
|
JButton btnNewButton = new JButton("Nuevo");
|
||||||
|
btnNewButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Cliente clienteVacio = new Cliente(ControladorCliente.getUltimoCliente().getId() + 1,
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
new Date(),
|
||||||
|
true);
|
||||||
|
actualizarDatosEnPantalla(clienteVacio);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
btnNewButton.setToolTipText("Limpia la pantalla para crear un nuevo cliente");
|
||||||
|
panel_4.add(btnNewButton);
|
||||||
|
btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
JButton btnNewButton_1 = new JButton("Guardar");
|
||||||
|
btnNewButton_1.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
|
||||||
|
Date fecha = null;
|
||||||
|
try {
|
||||||
|
fecha = sdf.parse(jtfFechaNac.getText());
|
||||||
|
} catch (ParseException ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "La fecha introducida no es correcta");
|
||||||
|
//throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
Cliente updateClient = new Cliente(clienteActual.getId(),
|
||||||
|
jtfNombre.getText(),
|
||||||
|
jtfApellidos.getText(),
|
||||||
|
jtfLocalidad.getText(),
|
||||||
|
jtfDniNie.getText(),
|
||||||
|
fecha,
|
||||||
|
checkBoxEstado.isSelected());
|
||||||
|
|
||||||
|
if (clienteActual.getId() > ControladorCliente.getUltimoCliente().getId()) {
|
||||||
|
if (ControladorCliente.nuevoCliente(updateClient) > 0) {
|
||||||
|
JOptionPane.showMessageDialog(null, "El cliente se ha añadido correctamente");
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "El cliente no se ha podido añadir");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (ControladorCliente.actualizarCliente(updateClient) > 0) {
|
||||||
|
JOptionPane.showMessageDialog(null, "El cliente se ha actualizado correctamente");
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "El cliente no se ha podido actualizar");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
actualizarDatosEnPantalla(ControladorCliente.getCliente(clienteActual.getId()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
btnNewButton_1.setToolTipText("Guarda los cambios realizados en el registro que se ve en pantalla");
|
||||||
|
panel_4.add(btnNewButton_1);
|
||||||
|
btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
JButton btnNewButton_2 = new JButton("Eliminar");
|
||||||
|
btnNewButton_2.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
int respuesta = JOptionPane.showConfirmDialog(null, "¿Seguro que desea eliminar el cliente?", "Confirmación", JOptionPane.YES_NO_OPTION);
|
||||||
|
if (respuesta == JOptionPane.YES_OPTION) {
|
||||||
|
|
||||||
|
Cliente clienteAnterior = ControladorCliente.getCliente(clienteActual.getId() - 1);
|
||||||
|
Cliente clienteSiguiente = ControladorCliente.getCliente(clienteActual.getId() + 1);
|
||||||
|
|
||||||
|
if (ControladorCliente.eliminarCliente(clienteActual.getId()) > 0) {
|
||||||
|
JOptionPane.showMessageDialog(null, "El cliente se ha eliminado correctamente");
|
||||||
|
if (clienteAnterior != null) {
|
||||||
|
actualizarDatosEnPantalla(clienteAnterior);
|
||||||
|
} else if (clienteSiguiente != null) {
|
||||||
|
actualizarDatosEnPantalla(clienteSiguiente);
|
||||||
|
} else {
|
||||||
|
btnNewButton.doClick();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "El cliente no se ha podido eliminar");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
btnNewButton_2.setToolTipText("Elimina el registro que se ve en pantalla");
|
||||||
|
panel_4.add(btnNewButton_2);
|
||||||
|
btnNewButton_2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user