commit 18afba0dd23c39ca11654558ed3fbe83b088c680 Author: juanperoh11-lgtm. Date: Tue Oct 27 18:50:40 2026 +0100 Actividad 2.1 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..e56bba2 --- /dev/null +++ b/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09e3bc9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/target/ diff --git a/.project b/.project new file mode 100644 index 0000000..1e9809a --- /dev/null +++ b/.project @@ -0,0 +1,34 @@ + + + Conectores_01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + + + 1793123414266 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/.settings/org.eclipse.jdt.apt.core.prefs b/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..d59c201 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,13 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=25 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=25 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=25 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5ee5ed7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + Conectores_01 + Conectores_01 + 0.0.1-SNAPSHOT + + + + com.mysql + mysql-connector-j + 26.10.0 + compile + + + + src + + + maven-compiler-plugin + 3.13.0 + + 25 + + + + + \ No newline at end of file diff --git a/src/org/palomafp/aadd/aez/GestorCochesJDB.java b/src/org/palomafp/aadd/aez/GestorCochesJDB.java new file mode 100644 index 0000000..006e90c --- /dev/null +++ b/src/org/palomafp/aadd/aez/GestorCochesJDB.java @@ -0,0 +1,110 @@ +package org.palomafp.aadd.aez; + +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 java.util.ArrayList; +import java.util.List; + +import org.palomafp.aadd.aez.model.Coche; + +public class GestorCochesJDB { + + private static final String URL = "jdbc:mysql://10.100.11.135:3306/concesionario?useSSL=false&allowPublicKeyRetrieval=true"; + private static final String USUARIO = "Alejandro"; + private static final String PASSWORD = "Combarro1!"; + + public static void main(String[] args) { + + try (Connection conn = DriverManager.getConnection(URL, USUARIO, PASSWORD)) { + System.out.println("Conexión con MySQL establecida correctamente.\n"); + + // 1. Consultar la lista de coches e imprimir por consola + System.out.println("Consultar cohes"); + List listaCoches = consultarCoches(conn); + listaCoches.forEach(System.out::println); + + // 2. Borrar los registros usando la lista obtenida + System.out.println("\nBorrar Coches de la lista"); + borrarCoches(conn, listaCoches); + + // 3. Insertar cada coche de nuevo a partir de la lista inicial + System.out.println("\nInsertar coches"); + insertarCoches(conn, listaCoches); + + // 4. Actualizar el coche con código 5 (modelo pasa a ser "Ibiza Eco") + System.out.println("\nActualizar coches"); + actualizarModeloCoche(conn, 5, "Ibiza Eco"); + + System.out.println("\nTabla final"); + List listaFinal = consultarCoches(conn); + listaFinal.forEach(System.out::println); + + } catch (SQLException e) { + System.err.println("Error durante las operaciones JDBC: " + e.getMessage()); + e.printStackTrace(); + } + } + + private static List consultarCoches(Connection conn) throws SQLException { + List lista = new ArrayList<>(); + String sql = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE"; + + try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { + + while (rs.next()) { + Coche coche = new Coche(rs.getInt("identificador"), rs.getString("marca"), rs.getString("modelo"), + rs.getDouble("cilindrada")); + lista.add(coche); + } + } + return lista; + } + + private static void borrarCoches(Connection conn, List lista) throws SQLException { + String sql = "DELETE FROM T_COCHE WHERE identificador = ?"; + + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { + for (Coche coche : lista) { + pstmt.setInt(1, coche.getIdentificador()); + int borrados = pstmt.executeUpdate(); + System.out.println("Coche ID " + coche.getIdentificador() + " borrado (" + borrados + " registro)."); + } + } + } + + private static void insertarCoches(Connection conn, List lista) throws SQLException { + String sql = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)"; + + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { + for (Coche coche : lista) { + pstmt.setInt(1, coche.getIdentificador()); + pstmt.setString(2, coche.getMarca()); + pstmt.setString(3, coche.getModelo()); + pstmt.setDouble(4, coche.getCilindrada()); + pstmt.executeUpdate(); + System.out.println("Coche ID " + coche.getIdentificador() + " insertado."); + } + } + } + + private static void actualizarModeloCoche(Connection conn, int identificador, String nuevoModelo) + throws SQLException { + String sql = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?"; + + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { + pstmt.setString(1, nuevoModelo); + pstmt.setInt(2, identificador); + int filas = pstmt.executeUpdate(); + + if (filas > 0) { + System.out.println("Coche ID " + identificador + " actualizado correctamente a '" + nuevoModelo + "'."); + } else { + System.out.println("No se encontró el coche con ID " + identificador); + } + } + } +} diff --git a/src/org/palomafp/aadd/aez/model/Coche.java b/src/org/palomafp/aadd/aez/model/Coche.java new file mode 100644 index 0000000..8dbe0d6 --- /dev/null +++ b/src/org/palomafp/aadd/aez/model/Coche.java @@ -0,0 +1,60 @@ +package org.palomafp.aadd.aez.model; + +/** + * Clase Bean que representa la entidad T_COCHE. + */ +public class Coche { + private int identificador; + private String marca; + private String modelo; + private double cilindrada; + + public Coche() { + } + + public Coche(int identificador, String marca, String modelo, double cilindrada) { + this.identificador = identificador; + this.marca = marca; + this.modelo = modelo; + this.cilindrada = cilindrada; + } + + // Getters y Setters + public int getIdentificador() { + return identificador; + } + + public void setIdentificador(int identificador) { + this.identificador = identificador; + } + + public String getMarca() { + return marca; + } + + public void setMarca(String marca) { + this.marca = marca; + } + + public String getModelo() { + return modelo; + } + + public void setModelo(String modelo) { + this.modelo = modelo; + } + + public double getCilindrada() { + return cilindrada; + } + + public void setCilindrada(double cilindrada) { + this.cilindrada = cilindrada; + } + + @Override + public String toString() { + return "Coche [identificador=" + identificador + ", marca=" + marca + ", modelo=" + modelo + ", cilindrada=" + + cilindrada + "]"; + } +} \ No newline at end of file