commit 631956afa35a71226f57a7c3659c6111bde5178d Author: gonzalo.diaz18 Date: Thu Oct 29 16:04:18 2026 +0100 Actividad 2.1 CRUD sobre una base de datos en MySQL diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..0ff5410 --- /dev/null +++ b/.classpath @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..4f5968a --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + bbdd_01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..a9cb8e3 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=21 +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=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 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..10aadb0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + bbdd_01 + bbdd_01 + 0.0.1-SNAPSHOT + + + + com.mysql + mysql-connector-j + 26.10.0 + compile + + + + src + + + maven-compiler-plugin + 3.13.0 + + 21 + + + + + \ No newline at end of file diff --git a/src/org/lapalomafp/aadd/gdo/Coche.java b/src/org/lapalomafp/aadd/gdo/Coche.java new file mode 100644 index 0000000..91b1aea --- /dev/null +++ b/src/org/lapalomafp/aadd/gdo/Coche.java @@ -0,0 +1,60 @@ +package org.lapalomafp.aadd.gdo; + +public class Coche { + + + private int identificador; + private String marca; + private String modelo; + private double cilindrada; + + public Coche() { + super(); + } + + public Coche(int identificador, String marca, String modelo, double cilindrada) { + super(); + this.identificador = identificador; + this.marca = marca; + this.modelo = modelo; + this.cilindrada = cilindrada; + } + + 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 + "]"; + } +} diff --git a/src/org/lapalomafp/aadd/gdo/Gestor.java b/src/org/lapalomafp/aadd/gdo/Gestor.java new file mode 100644 index 0000000..7552424 --- /dev/null +++ b/src/org/lapalomafp/aadd/gdo/Gestor.java @@ -0,0 +1,122 @@ +package org.lapalomafp.aadd.gdo; + +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; + +public class Gestor { + + private static final String URL = "jdbc:mysql://10.100.11.146:3306/concesionario"; + private static final String USUARIO = "root"; + private static final String CLAVE = "1234"; + + private static Connection conectar() throws SQLException, ClassNotFoundException { + Class.forName("com.mysql.cj.jdbc.Driver"); + return DriverManager.getConnection(URL, USUARIO, CLAVE); + } + + private static List consultarCoches(Connection conexion) throws SQLException { + List coches = new ArrayList<>(); + String sql = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE"; + + Statement sentencia = conexion.createStatement(); + ResultSet resultado = sentencia.executeQuery(sql); + + System.out.println("\n=== LISTA DE COCHES ==="); + while (resultado.next()) { + Coche coche = new Coche( + resultado.getInt("identificador"), + resultado.getString("marca"), + resultado.getString("modelo"), + resultado.getDouble("cilindrada") + ); + System.out.println(coche); + coches.add(coche); + } + + resultado.close(); + sentencia.close(); + return coches; + } + + // ─── 2. BORRAR ─────────────────────────────────────────────────────────── + private static void borrarCoches(Connection conexion) throws SQLException { + String sql = "DELETE FROM T_COCHE"; + + Statement sentencia = conexion.createStatement(); + int filas = sentencia.executeUpdate(sql); + System.out.println("\n=== BORRADO === " + filas + " coches eliminados."); + + sentencia.close(); + } + + private static void insertarCoches(Connection conexion, List coches) throws SQLException { + String sql = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)"; + + PreparedStatement sentenciaPreparada = conexion.prepareStatement(sql); + + for (Coche coche : coches) { + sentenciaPreparada.setInt (1, coche.getIdentificador()); + sentenciaPreparada.setString(2, coche.getMarca()); + sentenciaPreparada.setString(3, coche.getModelo()); + sentenciaPreparada.setDouble(4, coche.getCilindrada()); + sentenciaPreparada.executeUpdate(); + } + System.out.println("\n=== INSERCIÓN === " + coches.size() + " coches insertados."); + + sentenciaPreparada.close(); + } + + private static void actualizarCoche(Connection conexion) throws SQLException { + String sql = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?"; + + PreparedStatement sentenciaPreparada = conexion.prepareStatement(sql); + sentenciaPreparada.setString(1, "Ibiza Eco"); + sentenciaPreparada.setInt (2, 5); + int filas = sentenciaPreparada.executeUpdate(); + System.out.println("\n=== ACTUALIZACIÓN === " + filas + " fila(s) modificada(s). (id=5 → Ibiza Eco)"); + + sentenciaPreparada.close(); + } + + public static void main(String[] args) { + + Connection conexion = null; + + try { + conexion = conectar(); + System.out.println("Conexión establecida con MySQL."); + + List listaInicial = consultarCoches(conexion); + + borrarCoches(conexion); + + insertarCoches(conexion, listaInicial); + + actualizarCoche(conexion); + + System.out.println("\n=== ESTADO FINAL ==="); + consultarCoches(conexion); + + } catch (ClassNotFoundException e) { + System.err.println("Driver no encontrado: " + e.getMessage()); + } catch (SQLException e) { + System.err.println("Error SQL: " + e.getMessage()); + e.printStackTrace(); + } finally { + if (conexion != null) { + try { + conexion.close(); + System.out.println("\nConexión cerrada."); + } catch (SQLException e) { + System.err.println("Error al cerrar: " + e.getMessage()); + } + } + } + } +} diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..9bdf3b1 --- /dev/null +++ b/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Build-Jdk-Spec: 21 +Created-By: Maven Integration for Eclipse + diff --git a/target/classes/META-INF/maven/bbdd_01/bbdd_01/pom.properties b/target/classes/META-INF/maven/bbdd_01/bbdd_01/pom.properties new file mode 100644 index 0000000..74a414b --- /dev/null +++ b/target/classes/META-INF/maven/bbdd_01/bbdd_01/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Thu Oct 29 15:39:58 CET 2026 +artifactId=bbdd_01 +groupId=bbdd_01 +m2e.projectLocation=C\:\\Users\\gonza\\eclipse-workspace\\bbdd_01 +m2e.projectName=bbdd_01 +version=0.0.1-SNAPSHOT diff --git a/target/classes/META-INF/maven/bbdd_01/bbdd_01/pom.xml b/target/classes/META-INF/maven/bbdd_01/bbdd_01/pom.xml new file mode 100644 index 0000000..10aadb0 --- /dev/null +++ b/target/classes/META-INF/maven/bbdd_01/bbdd_01/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + bbdd_01 + bbdd_01 + 0.0.1-SNAPSHOT + + + + com.mysql + mysql-connector-j + 26.10.0 + compile + + + + src + + + maven-compiler-plugin + 3.13.0 + + 21 + + + + + \ No newline at end of file diff --git a/target/classes/org/lapalomafp/aadd/gdo/Coche.class b/target/classes/org/lapalomafp/aadd/gdo/Coche.class new file mode 100644 index 0000000..1ea478c Binary files /dev/null and b/target/classes/org/lapalomafp/aadd/gdo/Coche.class differ diff --git a/target/classes/org/lapalomafp/aadd/gdo/Gestor.class b/target/classes/org/lapalomafp/aadd/gdo/Gestor.class new file mode 100644 index 0000000..5d16638 Binary files /dev/null and b/target/classes/org/lapalomafp/aadd/gdo/Gestor.class differ