Ejercicios capítulo 8 bloque 2
This commit is contained in:
1
.idea/jpa.xml
generated
1
.idea/jpa.xml
generated
@@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="JpaBuddyIdeaProjectConfig">
|
<component name="JpaBuddyIdeaProjectConfig">
|
||||||
|
<option name="defaultUnitInitialized" value="true" />
|
||||||
<option name="renamerInitialized" value="true" />
|
<option name="renamerInitialized" value="true" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
1
.idea/libraries/librerias.xml
generated
1
.idea/libraries/librerias.xml
generated
@@ -3,7 +3,6 @@
|
|||||||
<CLASSES>
|
<CLASSES>
|
||||||
<root url="jar://$PROJECT_DIR$/librerias/JDBC_Oracle_Connection_Pool/ucp.jar!/" />
|
<root url="jar://$PROJECT_DIR$/librerias/JDBC_Oracle_Connection_Pool/ucp.jar!/" />
|
||||||
<root url="jar://$PROJECT_DIR$/librerias/Driver_MySQL_Connector_J/mysql-connector-java-8.0.19.jar!/" />
|
<root url="jar://$PROJECT_DIR$/librerias/Driver_MySQL_Connector_J/mysql-connector-java-8.0.19.jar!/" />
|
||||||
<root url="jar://$PROJECT_DIR$/librerias/Driver_MySQL_Connector_J/mysql-connector-java-5.1.47-bin.jar!/" />
|
|
||||||
<root url="jar://$PROJECT_DIR$/librerias/mongoDB-Java-driver/mongo-java-driver-3.12.12.jar!/" />
|
<root url="jar://$PROJECT_DIR$/librerias/mongoDB-Java-driver/mongo-java-driver-3.12.12.jar!/" />
|
||||||
<root url="jar://$PROJECT_DIR$/librerias/EclipseLink2.5.2/jlib/eclipselink.jar!/" />
|
<root url="jar://$PROJECT_DIR$/librerias/EclipseLink2.5.2/jlib/eclipselink.jar!/" />
|
||||||
<root url="jar://$PROJECT_DIR$/librerias/EclipseLink2.5.2/jlib/jpa/org.eclipse.persistence.jpars_2.5.2.v20140319-9ad6abd.jar!/" />
|
<root url="jar://$PROJECT_DIR$/librerias/EclipseLink2.5.2/jlib/jpa/org.eclipse.persistence.jpars_2.5.2.v20140319-9ad6abd.jar!/" />
|
||||||
|
|||||||
Binary file not shown.
@@ -1,76 +0,0 @@
|
|||||||
package capitulo08.bloque01;
|
|
||||||
|
|
||||||
import capitulo08.bloque01.operaciones.Delete;
|
|
||||||
import capitulo08.bloque01.operaciones.Insert;
|
|
||||||
import capitulo08.bloque01.operaciones.Select;
|
|
||||||
import capitulo08.bloque01.operaciones.Update;
|
|
||||||
|
|
||||||
import java.sql.*;
|
|
||||||
|
|
||||||
import static capitulo04.utils.Utils.solicitarIntScannerInline;
|
|
||||||
import static capitulo04.utils.Utils.solicitarStringScannerInline;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int opcion = 0;
|
|
||||||
Connection conn = null;
|
|
||||||
try {
|
|
||||||
conn = getConexion();
|
|
||||||
} catch (SQLException | ClassNotFoundException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
System.out.println(
|
|
||||||
"Seleccione una operación:\n" +
|
|
||||||
"0 - Salir del programa\n" +
|
|
||||||
"1 - Mostrar todos los fabricantes\n" +
|
|
||||||
"2 - Añadir un fabricante\n" +
|
|
||||||
"3 - Actualizar un fabricante\n" +
|
|
||||||
"4 - Eliminar un fabricante");
|
|
||||||
opcion = solicitarIntScannerInline("> ");
|
|
||||||
|
|
||||||
try {
|
|
||||||
switch (opcion) {
|
|
||||||
case 1: {
|
|
||||||
Select.mostrarTabla(conn, "fabricante");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 2: {
|
|
||||||
Insert.addFabricante(conn,
|
|
||||||
solicitarStringScannerInline("Introduzca el CIF: "),
|
|
||||||
solicitarStringScannerInline("Introduzca el Nombre: "));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 3: {
|
|
||||||
int id;
|
|
||||||
do {
|
|
||||||
id = solicitarIntScannerInline("Introduzca el ID del fabricante: ");
|
|
||||||
} while (!Select.isIdValid(conn, "fabricante", id));
|
|
||||||
String cifNuevo = solicitarStringScannerInline("Introduzca el nuevo CIF (en blanco para no cambiar): ");
|
|
||||||
String nombreNuevo = solicitarStringScannerInline("Introduzca el nuevo Nombre (en blanco para no cambiar): ");
|
|
||||||
Update.actualizarFabricante(conn, id, cifNuevo, nombreNuevo);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 4: {
|
|
||||||
int id;
|
|
||||||
do {
|
|
||||||
id = solicitarIntScannerInline("Introduzca el ID del fabricante: ");
|
|
||||||
} while (!Select.isIdValid(conn, "fabricante", id));
|
|
||||||
Delete.eliminarRegistro(conn, "fabricante", id);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (opcion != 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Connection getConexion() throws ClassNotFoundException, SQLException {
|
|
||||||
Class.forName(PropiedadesBBDD.getMySQLDriver());
|
|
||||||
return DriverManager.getConnection(PropiedadesBBDD.getMySQLConnectionString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package capitulo08.bloque01.operaciones;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class Insert {
|
|
||||||
public static int addFabricante(Connection conn, String cif, String nombre) throws SQLException {
|
|
||||||
Statement st = conn.createStatement();
|
|
||||||
return st.executeUpdate("insert into tutorialjavacoches.fabricante (id, cif, nombre) values (" + Select.getNextId(conn, "fabricante") + ",'" + cif + "','" + nombre + "');");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package capitulo08.bloque01.operaciones;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class Select {
|
|
||||||
public static void mostrarTabla(Connection conn, String tabla) throws SQLException {
|
|
||||||
Statement st = conn.createStatement();
|
|
||||||
ResultSet rs = st.executeQuery("select * from tutorialjavacoches." + tabla + ";");
|
|
||||||
|
|
||||||
switch (tabla) {
|
|
||||||
case "fabricante": {
|
|
||||||
System.out.println("ID\t | CIF\t\t\t | Nombre");
|
|
||||||
while (rs.next()) {
|
|
||||||
System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t | " + rs.getString(3));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getNextId(Connection conn, String tabla) throws SQLException {
|
|
||||||
Statement st = conn.createStatement();
|
|
||||||
ResultSet rs = st.executeQuery("select max(id) from tutorialjavacoches." + tabla + ";");
|
|
||||||
rs.next();
|
|
||||||
return (rs.getInt(1) + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isIdValid(Connection conn, String tabla, int id) throws SQLException {
|
|
||||||
Statement st = conn.createStatement();
|
|
||||||
ResultSet rs = st.executeQuery("select id from tutorialjavacoches." + tabla + " where id=" + id + ";");
|
|
||||||
if (rs.next()) {
|
|
||||||
return rs.getInt(1) == id;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package capitulo08.bloque01.operaciones;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class Update {
|
|
||||||
public static void actualizarFabricante(Connection conn, int id, String cif, String nombre) throws SQLException {
|
|
||||||
if (!(Objects.equals(cif, "") || cif == null)) {
|
|
||||||
actualizarCampo(conn, "fabricante", id, "cif", cif);
|
|
||||||
}
|
|
||||||
if (!(Objects.equals(nombre, "") || nombre == null)) {
|
|
||||||
actualizarCampo(conn, "fabricante", id, "nombre", nombre);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int actualizarCampo(Connection conn, String tabla, int id, String campo, String nuevoValor) throws SQLException {
|
|
||||||
Statement st = conn.createStatement();
|
|
||||||
return st.executeUpdate("update tutorialjavacoches." + tabla + " set " + campo + "='" + nuevoValor + "' where id=" + id + ";");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
389
src/capitulo08/bloque01y02/Main.java
Normal file
389
src/capitulo08/bloque01y02/Main.java
Normal file
@@ -0,0 +1,389 @@
|
|||||||
|
package capitulo08.bloque01y02;
|
||||||
|
|
||||||
|
import capitulo08.bloque01y02.operaciones.Delete;
|
||||||
|
import capitulo08.bloque01y02.operaciones.Insert;
|
||||||
|
import capitulo08.bloque01y02.operaciones.Select;
|
||||||
|
import capitulo08.bloque01y02.operaciones.Update;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import static capitulo04.utils.Utils.solicitarIntScannerInline;
|
||||||
|
import static capitulo04.utils.Utils.solicitarStringScannerInline;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int opcion = 0;
|
||||||
|
Connection conn = null;
|
||||||
|
try {
|
||||||
|
conn = getConexion();
|
||||||
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
System.out.println(
|
||||||
|
"\n¿Qué quieres administrar?:\n" +
|
||||||
|
"0 - Salir del programa\n" +
|
||||||
|
"1 - Coches\n" +
|
||||||
|
"2 - Fabricantes\n" +
|
||||||
|
"3 - Concesionarios\n" +
|
||||||
|
"4 - Clientes\n" +
|
||||||
|
"5 - Ventas");
|
||||||
|
opcion = solicitarIntScannerInline("> ");
|
||||||
|
|
||||||
|
switch (opcion) {
|
||||||
|
case 1:
|
||||||
|
menuCoches(conn);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
menuFabricantes(conn);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
menuConcesionarios(conn);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
menuClientes(conn);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
menuVentas(conn);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} while (opcion != 0);
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Connection getConexion() throws ClassNotFoundException, SQLException {
|
||||||
|
//Class.forName(PropiedadesBBDD.getMySQLDriver());
|
||||||
|
return DriverManager.getConnection(PropiedadesBBDD.getMySQLConnectionString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void menuFabricantes(Connection conn) {
|
||||||
|
int opcion = 0;
|
||||||
|
do {
|
||||||
|
System.out.println(
|
||||||
|
"\nSeleccione una operación:\n" +
|
||||||
|
"0 - Volver atrás\n" +
|
||||||
|
"1 - Mostrar todos los fabricantes\n" +
|
||||||
|
"2 - Añadir un fabricante\n" +
|
||||||
|
"3 - Actualizar un fabricante\n" +
|
||||||
|
"4 - Eliminar un fabricante");
|
||||||
|
opcion = solicitarIntScannerInline("> ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (opcion) {
|
||||||
|
case 1: {
|
||||||
|
Select.mostrarTabla(conn, "fabricante");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
Insert.addFabricante(conn,
|
||||||
|
solicitarStringScannerInline("Introduzca el CIF: "),
|
||||||
|
solicitarStringScannerInline("Introduzca el Nombre: "));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del fabricante: ");
|
||||||
|
} while (!Select.isIdValid(conn, "fabricante", id));
|
||||||
|
String cifNuevo = solicitarStringScannerInline("Introduzca el nuevo CIF (en blanco para no cambiar): ");
|
||||||
|
String nombreNuevo = solicitarStringScannerInline("Introduzca el nuevo Nombre (en blanco para no cambiar): ");
|
||||||
|
Update.actualizarFabricante(conn, id, cifNuevo, nombreNuevo);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 4: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del fabricante: ");
|
||||||
|
} while (!Select.isIdValid(conn, "fabricante", id));
|
||||||
|
Delete.eliminarRegistro(conn, "fabricante", id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (opcion != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void menuCoches(Connection conn) {
|
||||||
|
int opcion = 0;
|
||||||
|
do {
|
||||||
|
System.out.println(
|
||||||
|
"\nSeleccione una operación:\n" +
|
||||||
|
"0 - Volver atrás\n" +
|
||||||
|
"1 - Mostrar todos los coches\n" +
|
||||||
|
"2 - Añadir un coche\n" +
|
||||||
|
"3 - Actualizar un coche\n" +
|
||||||
|
"4 - Eliminar un coche");
|
||||||
|
opcion = solicitarIntScannerInline("> ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (opcion) {
|
||||||
|
case 1: {
|
||||||
|
Select.mostrarTabla(conn, "coche");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
Insert.addCoche(conn,
|
||||||
|
solicitarIntScannerInline("Introduzca el ID del fabricante: "),
|
||||||
|
solicitarStringScannerInline("Introduzca número del bastidor: "),
|
||||||
|
solicitarStringScannerInline("Introduzca el modelo: "),
|
||||||
|
solicitarStringScannerInline("Introduzca el color: "));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
int id;
|
||||||
|
int idFabricante;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del coche: ");
|
||||||
|
} while (!Select.isIdValid(conn, "coche", id));
|
||||||
|
do {
|
||||||
|
idFabricante = solicitarIntScannerInline("Introduzca el nuevo id del Fabricante (0 para no cambiar): ");
|
||||||
|
} while (!Select.isIdValid(conn, "fabricante", idFabricante) && !(idFabricante == 0));
|
||||||
|
String numBastidor = solicitarStringScannerInline("Introduzca el nuevo número de bastidor (en blanco para no cambiar): ");
|
||||||
|
String modelo = solicitarStringScannerInline("Introduzca el nuevo modelo (en blanco para no cambiar): ");
|
||||||
|
String color = solicitarStringScannerInline("Introduzca el nuevo color (en blanco para no cambiar): ");
|
||||||
|
Update.actualizarCoche(conn, id, idFabricante, numBastidor, modelo, color);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 4: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del coche: ");
|
||||||
|
} while (!Select.isIdValid(conn, "coche", id));
|
||||||
|
Delete.eliminarRegistro(conn, "coche", id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (opcion != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void menuConcesionarios(Connection conn) {
|
||||||
|
int opcion = 0;
|
||||||
|
do {
|
||||||
|
System.out.println(
|
||||||
|
"\nSeleccione una operación:\n" +
|
||||||
|
"0 - Volver atrás\n" +
|
||||||
|
"1 - Mostrar todos los concesionarios\n" +
|
||||||
|
"2 - Añadir un concesionario\n" +
|
||||||
|
"3 - Actualizar un concesionario\n" +
|
||||||
|
"4 - Eliminar un concesionario");
|
||||||
|
opcion = solicitarIntScannerInline("> ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (opcion) {
|
||||||
|
case 1: {
|
||||||
|
Select.mostrarTabla(conn, "concesionario");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
Insert.addConcesionario(conn,
|
||||||
|
solicitarStringScannerInline("Introduzca el CIF: "),
|
||||||
|
solicitarStringScannerInline("Introduzca el Nombre: "),
|
||||||
|
solicitarStringScannerInline("Introduzca la Localidad: "));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del concesionario: ");
|
||||||
|
} while (!Select.isIdValid(conn, "concesionario", id));
|
||||||
|
String cifNuevo = solicitarStringScannerInline("Introduzca el nuevo CIF (en blanco para no cambiar): ");
|
||||||
|
String nombreNuevo = solicitarStringScannerInline("Introduzca el nuevo Nombre (en blanco para no cambiar): ");
|
||||||
|
String localidadNueva = solicitarStringScannerInline("Introduzca la nueva Localidad (en blanco para no cambiar): ");
|
||||||
|
Update.actualizarConcesionario(conn, id, cifNuevo, nombreNuevo, localidadNueva);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 4: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del concesionario: ");
|
||||||
|
} while (!Select.isIdValid(conn, "concesionario", id));
|
||||||
|
Delete.eliminarRegistro(conn, "concesionario", id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (opcion != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void menuClientes(Connection conn) {
|
||||||
|
int opcion = 0;
|
||||||
|
do {
|
||||||
|
System.out.println(
|
||||||
|
"\nSeleccione una operación:\n" +
|
||||||
|
"0 - Volver atrás\n" +
|
||||||
|
"1 - Mostrar todos los clientes\n" +
|
||||||
|
"2 - Añadir un cliente\n" +
|
||||||
|
"3 - Actualizar un cliente\n" +
|
||||||
|
"4 - Eliminar un cliente");
|
||||||
|
opcion = solicitarIntScannerInline("> ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (opcion) {
|
||||||
|
case 1: {
|
||||||
|
Select.mostrarTabla(conn, "cliente");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
Insert.addCliente(conn,
|
||||||
|
solicitarStringScannerInline("Introduzca el Nombre: "),
|
||||||
|
solicitarStringScannerInline("Introduzca los Apellidos: "),
|
||||||
|
solicitarStringScannerInline("Introduzca la Localidad: "),
|
||||||
|
solicitarStringScannerInline("Introduzca el DNI/NIE: "),
|
||||||
|
solicitarFechaInline("fecha de nacimiento", "dd/MM/yyyy"),
|
||||||
|
solicitarIntScannerInline("Introduzca el estado (0 -> NO o 1 -> SÍ): "));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del cliente: ");
|
||||||
|
} while (!Select.isIdValid(conn, "cliente", id));
|
||||||
|
|
||||||
|
String nuevoNombre = solicitarStringScannerInline("Introduzca el nuevo Nombre (en blanco para no cambiar): ");
|
||||||
|
String nuevosApellidos = solicitarStringScannerInline("Introduzca los Apellidos (en blanco para no cambiar): ");
|
||||||
|
String nuevaLocalidad = solicitarStringScannerInline("Introduzca la Localidad (en blanco para no cambiar): ");
|
||||||
|
String nuevoDNI = solicitarStringScannerInline("Introduzca el DNI/NIE (en blanco para no cambiar): ");
|
||||||
|
Date nuevaFecha = solicitarFechaInline("fecha de nacimiento", "dd/MM/yyyy");
|
||||||
|
int nuevoEstado = solicitarIntScannerInline("Introduzca el estado (0 -> NO o 1 -> SÍ): ");
|
||||||
|
|
||||||
|
Update.actualizarCliente(conn, id, nuevoNombre, nuevosApellidos, nuevaLocalidad, nuevoDNI, nuevaFecha, nuevoEstado);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 4: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID del cliente: ");
|
||||||
|
} while (!Select.isIdValid(conn, "cliente", id));
|
||||||
|
Delete.eliminarRegistro(conn, "cliente", id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (opcion != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void menuVentas(Connection conn) {
|
||||||
|
int opcion = 0;
|
||||||
|
do {
|
||||||
|
System.out.println(
|
||||||
|
"\nSeleccione una operación:\n" +
|
||||||
|
"0 - Volver atrás\n" +
|
||||||
|
"1 - Mostrar todas las ventas\n" +
|
||||||
|
"2 - Añadir una venta\n" +
|
||||||
|
"3 - Actualizar una venta\n" +
|
||||||
|
"4 - Eliminar una venta");
|
||||||
|
opcion = solicitarIntScannerInline("> ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (opcion) {
|
||||||
|
case 1: {
|
||||||
|
Select.mostrarTabla(conn, "venta");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
int idCliente;
|
||||||
|
int idConcesionario;
|
||||||
|
int idCoche;
|
||||||
|
do {
|
||||||
|
idCliente = solicitarIntScannerInline("Introduzca el ID del Cliente: ");
|
||||||
|
} while (!Select.isIdValid(conn, "cliente", idCliente));
|
||||||
|
do {
|
||||||
|
idConcesionario = solicitarIntScannerInline("Introduzca el ID del Concesionario: ");
|
||||||
|
} while (!Select.isIdValid(conn, "concesionario", idConcesionario));
|
||||||
|
do {
|
||||||
|
idCoche = solicitarIntScannerInline("Introduzca el ID del Coche: ");
|
||||||
|
} while (!Select.isIdValid(conn, "coche", idCoche));
|
||||||
|
|
||||||
|
Insert.addVenta(conn,
|
||||||
|
idCliente,
|
||||||
|
idConcesionario,
|
||||||
|
idCoche,
|
||||||
|
solicitarFechaInline("fecha de venta", "dd/MM/yyyy"),
|
||||||
|
solicitarIntScannerInline("Introduzca el precio de Venta: "));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID de la venta: ");
|
||||||
|
} while (!Select.isIdValid(conn, "venta", id));
|
||||||
|
|
||||||
|
int nuevoIdCliente;
|
||||||
|
int nuevoIdConcesionario;
|
||||||
|
int nuevoIdCoche;
|
||||||
|
do {
|
||||||
|
nuevoIdCliente = solicitarIntScannerInline("Introduzca el nuevo ID del Cliente (0 para no cambiar): ");
|
||||||
|
} while (!(nuevoIdCliente == 0 || Select.isIdValid(conn, "cliente", nuevoIdCliente)));
|
||||||
|
do {
|
||||||
|
nuevoIdConcesionario = solicitarIntScannerInline("Introduzca el nuevo ID del Concesionario (0 para no cambiar): ");
|
||||||
|
} while (!(nuevoIdConcesionario == 0 || Select.isIdValid(conn, "concesionario", nuevoIdConcesionario)));
|
||||||
|
do {
|
||||||
|
nuevoIdCoche = solicitarIntScannerInline("Introduzca el nuevo ID del Coche (0 para no cambiar): ");
|
||||||
|
} while (!(nuevoIdCoche == 0 || Select.isIdValid(conn, "coche", nuevoIdCoche)));
|
||||||
|
|
||||||
|
Date nuevaFecha = solicitarFechaInline("fecha de nacimiento", "dd/MM/yyyy");
|
||||||
|
int nuevoPrecio = solicitarIntScannerInline("Introduzca el nuevo precio de Venta (0 para no cambiar): ");
|
||||||
|
|
||||||
|
Update.actualizarVenta(conn, id, nuevoIdCliente, nuevoIdConcesionario, nuevoIdCoche, nuevaFecha, nuevoPrecio);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 4: {
|
||||||
|
int id;
|
||||||
|
do {
|
||||||
|
id = solicitarIntScannerInline("Introduzca el ID de la venta: ");
|
||||||
|
} while (!Select.isIdValid(conn, "venta", id));
|
||||||
|
Delete.eliminarRegistro(conn, "venta", id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (opcion != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Date solicitarFechaInline(String solicitud, String formato) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
SimpleDateFormat formatoFecha = new SimpleDateFormat(formato);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
System.out.print("Introduzca una " + solicitud + " con este formato ( " + formato + " ) > ");
|
||||||
|
String fechaIntroducida = sc.nextLine();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return formatoFecha.parse(fechaIntroducida);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
System.out.println("\nVuelva a introducir la fecha, en el formato correcto: " + formato);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package capitulo08.bloque01;
|
package capitulo08.bloque01y02;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
@@ -14,7 +14,7 @@ public class PropiedadesBBDD {
|
|||||||
propiedades = new Properties();
|
propiedades = new Properties();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File file = new File("./out/production/Ejercicios/capitulo08/otros/mysql.properties");
|
File file = new File("./out/production/Ejercicios/capitulo08/bloque01y02/mysql.properties");
|
||||||
propiedades.load(new FileReader(file));
|
propiedades.load(new FileReader(file));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package capitulo08.bloque01.operaciones;
|
package capitulo08.bloque01y02.operaciones;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
46
src/capitulo08/bloque01y02/operaciones/Insert.java
Normal file
46
src/capitulo08/bloque01y02/operaciones/Insert.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package capitulo08.bloque01y02.operaciones;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Insert {
|
||||||
|
public static int addFabricante(Connection conn, String cif, String nombre) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
return st.executeUpdate("insert into tutorialjavacoches.fabricante (id, cif, nombre) values (" +
|
||||||
|
Select.getNextId(conn, "fabricante") + ",'" + cif + "','" + nombre + "');");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int addCoche(Connection conn, int idFabricante, String numBastidor, String modelo, String color) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
if (Select.isIdValid(conn, "fabricante", idFabricante)) {
|
||||||
|
return st.executeUpdate("insert into tutorialjavacoches.coche (id, idfabricante, bastidor, modelo, color) values (" +
|
||||||
|
Select.getNextId(conn, "coche") + "," + idFabricante + ",'" + numBastidor + "','" + modelo + "','" + color + "');");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int addConcesionario(Connection conn, String cif, String nombre, String localidad) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
return st.executeUpdate("insert into tutorialjavacoches.concesionario (id, cif, nombre, localidad) values (" +
|
||||||
|
Select.getNextId(conn, "concesionario") + ",'" + cif + "','" + nombre + "','" + localidad + "');");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int addCliente(Connection conn, String nombre, String apellidos, String localidad, String dni, Date fechaNac, int activo) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return st.executeUpdate("insert into tutorialjavacoches.cliente (id, nombre, apellidos, localidad, dniNie, fechaNac, activo) values (" +
|
||||||
|
Select.getNextId(conn, "cliente") + ",'" + nombre + "','" + apellidos + "','" + localidad + "','" + dni + "','" + sdf.format(fechaNac) + "'," + activo + ");");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int addVenta(Connection conn, int idCliente, int idConcesionario, int idCoche, Date fecha, int precioVenta) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return st.executeUpdate("insert into tutorialjavacoches.venta (id, idCliente, idConcesionario, idCoche, fecha, precioVenta) values (" +
|
||||||
|
Select.getNextId(conn, "venta") + "," + idCliente + "," + idConcesionario + "," + idCoche + ",'" + sdf.format(fecha) + "'," + precioVenta + ");");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
67
src/capitulo08/bloque01y02/operaciones/Select.java
Normal file
67
src/capitulo08/bloque01y02/operaciones/Select.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package capitulo08.bloque01y02.operaciones;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class Select {
|
||||||
|
public static void mostrarTabla(Connection conn, String tabla) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
ResultSet rs = st.executeQuery("select * from tutorialjavacoches." + tabla + ";");
|
||||||
|
|
||||||
|
switch (tabla) {
|
||||||
|
case "fabricante": {
|
||||||
|
System.out.println("ID\t | CIF\t\t\t | Nombre");
|
||||||
|
while (rs.next()) {
|
||||||
|
System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t | " + rs.getString(3));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "coche": {
|
||||||
|
System.out.println("ID\t | ID F\t | Nº Bastidor\t | Modelo\t | Color");
|
||||||
|
while (rs.next()) {
|
||||||
|
System.out.println(rs.getInt(1) + "\t | " + rs.getInt(2) + "\t | " + rs.getString(3) + "\t | " + rs.getString(4) + "\t | " + rs.getString(5));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "concesionario": {
|
||||||
|
System.out.println("ID\t | CIF\t\t\t | Nombre\t\t\t\t | Localidad");
|
||||||
|
while (rs.next()) {
|
||||||
|
System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t | " + rs.getString(3) + "\t | " + rs.getString(4));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "cliente": {
|
||||||
|
System.out.println("ID\t | Nombre\t\t | Apellidos\t | Localidad\t | DNI\t | Fecha Nacimiento\t | Activo");
|
||||||
|
while (rs.next()) {
|
||||||
|
System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t\t | " + rs.getString(3) + "\t | " + rs.getString(4) + "\t | " + rs.getString(5) + "\t | " + rs.getDate(6) + "\t | " + rs.getInt(7));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "venta": {
|
||||||
|
System.out.println("ID\t | ID Cliente\t | ID Concesionario\t | ID Coche\t | Fecha\t | Precio Venta");
|
||||||
|
while (rs.next()) {
|
||||||
|
System.out.println(rs.getInt(1) + "\t | " + rs.getInt(2) + "\t | " + rs.getInt(3) + "\t | " + rs.getInt(4) + "\t | " + rs.getDate(5) + "\t | " + rs.getInt(6));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getNextId(Connection conn, String tabla) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
ResultSet rs = st.executeQuery("select max(id) from tutorialjavacoches." + tabla + ";");
|
||||||
|
rs.next();
|
||||||
|
return (rs.getInt(1) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isIdValid(Connection conn, String tabla, int id) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
ResultSet rs = st.executeQuery("select id from tutorialjavacoches." + tabla + " where id=" + id + ";");
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt(1) == id;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
106
src/capitulo08/bloque01y02/operaciones/Update.java
Normal file
106
src/capitulo08/bloque01y02/operaciones/Update.java
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
package capitulo08.bloque01y02.operaciones;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Update {
|
||||||
|
public static void actualizarFabricante(Connection conn, int id, String cif, String nombre) throws SQLException {
|
||||||
|
if (!(Objects.equals(cif, "") || cif == null)) {
|
||||||
|
actualizarCampoString(conn, "fabricante", id, "cif", cif);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(nombre, "") || nombre == null)) {
|
||||||
|
actualizarCampoString(conn, "fabricante", id, "nombre", nombre);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void actualizarCoche(Connection conn, int id, int idFabricante, String numBastidor, String modelo, String color) throws SQLException {
|
||||||
|
if (idFabricante != 0) {
|
||||||
|
actualizarCampoInt(conn, "coche", id, "idfabricante", idFabricante);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(numBastidor, "") || numBastidor == null)) {
|
||||||
|
actualizarCampoString(conn, "coche", id, "bastidor", numBastidor);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(modelo, "") || modelo == null)) {
|
||||||
|
actualizarCampoString(conn, "coche", id, "modelo", modelo);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(color, "") || color == null)) {
|
||||||
|
actualizarCampoString(conn, "coche", id, "color", color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void actualizarConcesionario(Connection conn, int id, String cif, String nombre, String localidad) throws SQLException {
|
||||||
|
if (!(Objects.equals(cif, "") || cif == null)) {
|
||||||
|
actualizarCampoString(conn, "concesionario", id, "cif", cif);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(nombre, "") || nombre == null)) {
|
||||||
|
actualizarCampoString(conn, "concesionario", id, "nombre", nombre);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(localidad, "") || localidad == null)) {
|
||||||
|
actualizarCampoString(conn, "concesionario", id, "localidad", localidad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void actualizarCliente(Connection conn, int id, String nombre, String apellidos, String localidad, String dniNie, Date fechaNac, int activo) throws SQLException {
|
||||||
|
if (!(Objects.equals(nombre, "") || nombre == null)) {
|
||||||
|
actualizarCampoString(conn, "cliente", id, "nombre", nombre);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(apellidos, "") || apellidos == null)) {
|
||||||
|
actualizarCampoString(conn, "cliente", id, "apellidos", apellidos);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(localidad, "") || localidad == null)) {
|
||||||
|
actualizarCampoString(conn, "cliente", id, "localidad", localidad);
|
||||||
|
}
|
||||||
|
if (!(Objects.equals(dniNie, "") || dniNie == null)) {
|
||||||
|
actualizarCampoString(conn, "cliente", id, "dniNie", dniNie);
|
||||||
|
}
|
||||||
|
if (fechaNac != null) {
|
||||||
|
actualizarCampoDate(conn, "cliente", id, "fechaNac", fechaNac);
|
||||||
|
}
|
||||||
|
if ((activo == 0 || activo == 1)) {
|
||||||
|
actualizarCampoInt(conn, "cliente", id, "activo", activo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void actualizarVenta(Connection conn, int id, int idCliente, int idConcesionario, int idCoche, Date fecha, int precioVenta) throws SQLException {
|
||||||
|
if (idCliente != 0) {
|
||||||
|
actualizarCampoInt(conn, "venta", id, "idCliente", idCliente);
|
||||||
|
}
|
||||||
|
if (idConcesionario != 0) {
|
||||||
|
actualizarCampoInt(conn, "venta", id, "idConcesionario", idConcesionario);
|
||||||
|
}
|
||||||
|
if (idCoche != 0) {
|
||||||
|
actualizarCampoInt(conn, "venta", id, "idCoche", idCoche);
|
||||||
|
}
|
||||||
|
if (fecha != null) {
|
||||||
|
actualizarCampoDate(conn, "venta", id, "fecha", fecha);
|
||||||
|
}
|
||||||
|
if (precioVenta != 0) {
|
||||||
|
actualizarCampoInt(conn, "venta", id, "precioVenta", precioVenta);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static int actualizarCampoString(Connection conn, String tabla, int id, String campo, String nuevoValor) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
return st.executeUpdate("update tutorialjavacoches." + tabla + " set " + campo + "='" + nuevoValor + "' where id=" + id + ";");
|
||||||
|
}
|
||||||
|
private static int actualizarCampoInt(Connection conn, String tabla, int id, String campo, int nuevoValor) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
return st.executeUpdate("update tutorialjavacoches." + tabla + " set " + campo + "=" + nuevoValor + " where id=" + id + ";");
|
||||||
|
}
|
||||||
|
private static int actualizarCampoDate(Connection conn, String tabla, int id, String campo, Date nuevoValor) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return st.executeUpdate("update tutorialjavacoches." + tabla + " set " + campo + "='" + sdf.format(nuevoValor) + "' where id=" + id + ";");
|
||||||
|
}}
|
||||||
Reference in New Issue
Block a user