Ejercicios capítulo 8 bloque 1
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Datos del usuario en uso
|
# Datos del usuario en uso
|
||||||
NOMBRE=Natanael
|
NOMBRE=Natanael
|
||||||
APELLIDOS=Gómez Ortiz
|
APELLIDOS=G<EFBFBD>mez Ortiz
|
||||||
EDAD=18
|
EDAD=18
|
||||||
ALTURA=160.6
|
ALTURA=160.6
|
||||||
SOLTERO=true
|
SOLTERO=true
|
||||||
76
src/capitulo08/bloque01/Main.java
Normal file
76
src/capitulo08/bloque01/Main.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
44
src/capitulo08/bloque01/PropiedadesBBDD.java
Normal file
44
src/capitulo08/bloque01/PropiedadesBBDD.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package capitulo08.bloque01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class PropiedadesBBDD {
|
||||||
|
|
||||||
|
private static Properties propiedades;
|
||||||
|
|
||||||
|
private static Properties getPropiedades() {
|
||||||
|
if (propiedades == null) {
|
||||||
|
propiedades = new Properties();
|
||||||
|
|
||||||
|
try {
|
||||||
|
File file = new File("./out/production/Ejercicios/capitulo08/otros/mysql.properties");
|
||||||
|
propiedades.load(new FileReader(file));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return propiedades;
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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 String getMySQLDriver() {
|
||||||
|
return getProperty("MYSQL_DRIVER_CLASS");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getProperty(String nombrePropiedad) {
|
||||||
|
return getPropiedades().getProperty(nombrePropiedad);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
6
src/capitulo08/bloque01/mysql.properties
Normal file
6
src/capitulo08/bloque01/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
|
||||||
12
src/capitulo08/bloque01/operaciones/Delete.java
Normal file
12
src/capitulo08/bloque01/operaciones/Delete.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package capitulo08.bloque01.operaciones;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class Delete {
|
||||||
|
public static int eliminarRegistro(Connection conn, String tabla, int id) throws SQLException {
|
||||||
|
Statement st = conn.createStatement();
|
||||||
|
return st.executeUpdate("delete from tutorialjavacoches." + tabla + " where id=" + id + ";");
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/capitulo08/bloque01/operaciones/Insert.java
Normal file
12
src/capitulo08/bloque01/operaciones/Insert.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
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 + "');");
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/capitulo08/bloque01/operaciones/Select.java
Normal file
41
src/capitulo08/bloque01/operaciones/Select.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
23
src/capitulo08/bloque01/operaciones/Update.java
Normal file
23
src/capitulo08/bloque01/operaciones/Update.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
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 + ";");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user