commit ec312b91e863d19ce847ca8da8d4d8500bb89a8c Author: alumnoT Date: Tue Oct 27 17:39:59 2026 +0100 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..04d1370 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.settings/ +bin/ +.classpath +.project \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..31f2b4d --- /dev/null +++ b/pom.xml @@ -0,0 +1,27 @@ + + 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 + + 26 + + + + + \ No newline at end of file diff --git a/src/scripts/Coche.java b/src/scripts/Coche.java new file mode 100644 index 0000000..222bd37 --- /dev/null +++ b/src/scripts/Coche.java @@ -0,0 +1,26 @@ +package scripts; + +public class Coche { + private int codigo; + private String marca; + private String modelo; + private double precio; + + public Coche(int codigo, String marca, String modelo, double precio) { + this.codigo = codigo; + this.marca = marca; + this.modelo = modelo; + this.precio = precio; + } + + // Getters + public int getCodigo() { return codigo; } + public String getMarca() { return marca; } + public String getModelo() { return modelo; } + public double getPrecio() { return precio; } + + @Override + public String toString() { + return "Coche [Código=" + codigo + ", Marca=" + marca + ", Modelo=" + modelo + ", Precio=" + precio + "]"; + } +} diff --git a/src/scripts/Main.java b/src/scripts/Main.java new file mode 100644 index 0000000..717838e --- /dev/null +++ b/src/scripts/Main.java @@ -0,0 +1,96 @@ +package scripts; +//10.100.11.138 +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class Main { + + public static void main(String[] args) throws Exception { + // 1. Datos de conexión directos (¡Recuerda cambiar localhost por la IP de tu M.V. si ejecutas desde fuera!) + String url = "jdbc:mysql://10.100.11.138:3306/concesionario?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; + String user = "root"; + String pass = "1234"; // Cambia por tu contraseña de la M.V. + + // Conectar a la base de datos + Connection conexion = DriverManager.getConnection(url, user, pass); + Statement stmt = conexion.createStatement(); + System.out.println("=== Conectado a MySQL ===\n"); + + // ----------------------------------------------------------------- + // A. CONSULTAR LA LISTA DE COCHES E IMPRIMIRLOS + // ----------------------------------------------------------------- + ResultSet rs = stmt.executeQuery("SELECT * FROM T_COCHE"); + List listaCodigosIniciales = new ArrayList<>(); + + System.out.println("--- 1. Consultando coches de T_COCHE ---"); + while (rs.next()) { + int id = rs.getInt("identificador"); + String marca = rs.getString("marca"); + String modelo = rs.getString("modelo"); + double cilindrada = rs.getDouble("cilindrada"); + + System.out.println("Coche -> ID: " + id + " | " + marca + " " + modelo + " | Cilindrada: " + cilindrada); + + // Guardamos los identificadores en la lista para el borrado + listaCodigosIniciales.add(id); + } + rs.close(); + + // ----------------------------------------------------------------- + // B. BORRAR MEDIANTE LA LISTA DE COCHES + // ----------------------------------------------------------------- + System.out.println("\n--- 2. Borrando coches mediante la lista ---"); + PreparedStatement psDelete = conexion.prepareStatement("DELETE FROM T_COCHE WHERE identificador = ?"); + for (int id : listaCodigosIniciales) { + psDelete.setInt(1, id); + psDelete.executeUpdate(); + System.out.println("Borrado ID: " + id); + } + psDelete.close(); + + // ----------------------------------------------------------------- + // C. RE-INSERTAR LOS COCHES DESDE LA LISTA INICIAL (Tu SQL Exacto) + // ----------------------------------------------------------------- + System.out.println("\n--- 3. Reinsertando coches iniciales ---"); + PreparedStatement psInsert = conexion.prepareStatement("INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)"); + + // Array con tus datos exactos para insertarlos limpiamente en un bucle corto + Object[][] datosCoches = { + {1, "Renault", "Megane", 1.5}, + {2, "Seat", "León", 1.6}, + {3, "Suzuki", "Vitara", 1.9}, + {4, "Seat", "Clio", 1.4}, + {5, "Seat", "Ibiza", 1.4}, // Este volverá a su estado inicial antes del update + {6, "Peugeot", "308", 2.0}, + {7, "Renault", "Megane", 1.5} + }; + + for (Object[] coche : datosCoches) { + psInsert.setInt(1, (int) coche[0]); + psInsert.setString(2, (String) coche[1]); + psInsert.setString(3, (String) coche[2]); + psInsert.setDouble(4, (double) coche[3]); + psInsert.executeUpdate(); + System.out.println("Reinsertado ID: " + coche[0] + " (" + coche[2] + ")"); + } + psInsert.close(); + + // ----------------------------------------------------------------- + // D. ACTUALIZAR EL COCHE CON CÓDIGO 5 A "Ibiza Eco" + // ----------------------------------------------------------------- + System.out.println("\n--- 4. Actualizando coche con identificador 5 ---"); + PreparedStatement psUpdate = conexion.prepareStatement("UPDATE T_COCHE SET modelo = ? WHERE identificador = ?"); + psUpdate.setString(1, "Ibiza Eco"); + psUpdate.setInt(2, 5); + + int filas = psUpdate.executeUpdate(); + System.out.println("Filas actualizadas: " + filas + " (Modelo del ID 5 cambiado a 'Ibiza Eco')"); + psUpdate.close(); + + // Cerrar recursos globales + stmt.close(); + conexion.close(); + System.out.println("\n=== Procesos CRUD finalizados con éxito ==="); + } +} \ No newline at end of file diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..802a2c6 --- /dev/null +++ b/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Java-Version: 26 +Build-Jdk-Spec: 25 +Created-By: Maven Integration for Eclipse + diff --git a/target/classes/META-INF/maven/Conectores_01/Conectores_01/pom.properties b/target/classes/META-INF/maven/Conectores_01/Conectores_01/pom.properties new file mode 100644 index 0000000..4162cb7 --- /dev/null +++ b/target/classes/META-INF/maven/Conectores_01/Conectores_01/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Tue Oct 27 17:23:44 CET 2026 +artifactId=Conectores_01 +groupId=Conectores_01 +m2e.projectLocation=C\:\\Users\\alumnoT\\eclipse-workspace\\Conectores_01 +m2e.projectName=Conectores_01 +version=0.0.1-SNAPSHOT diff --git a/target/classes/META-INF/maven/Conectores_01/Conectores_01/pom.xml b/target/classes/META-INF/maven/Conectores_01/Conectores_01/pom.xml new file mode 100644 index 0000000..31f2b4d --- /dev/null +++ b/target/classes/META-INF/maven/Conectores_01/Conectores_01/pom.xml @@ -0,0 +1,27 @@ + + 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 + + 26 + + + + + \ No newline at end of file diff --git a/target/classes/scripts/Coche.class b/target/classes/scripts/Coche.class new file mode 100644 index 0000000..e1d0ad1 Binary files /dev/null and b/target/classes/scripts/Coche.class differ diff --git a/target/classes/scripts/Main.class b/target/classes/scripts/Main.class new file mode 100644 index 0000000..1dbc87d Binary files /dev/null and b/target/classes/scripts/Main.class differ