commit a7177bcb17816a0040d09d17c89b733d4f7f011d Author: SASI Date: Thu Oct 29 11:48:18 2026 +0100 Primer commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43e6c06 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/ +.settings/ +.classpath +.project \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b5ccdd2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + Dependencia_mysql + Dependencia_mysql + 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/ss/AppPrincipal.java b/src/org/palomafp/aadd/ss/AppPrincipal.java new file mode 100644 index 0000000..8e0b0cf --- /dev/null +++ b/src/org/palomafp/aadd/ss/AppPrincipal.java @@ -0,0 +1,131 @@ +package org.palomafp.aadd.ss; + +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 AppPrincipal { + + private static final String URL = "jdbc:mysql://192.168.10.189/concesionario"; + private static final String USUARIO = "root"; + private static final String CLAVE = "root"; + + public static void main(String[] args) { + + try(Connection c = DriverManager.getConnection(URL, USUARIO, CLAVE)) { + System.out.println("Conecxion a MySQL ha establecido"); + + System.out.println("--- 1. CONSULTA INICIAL ---"); + List listaCoches = consultarCoches(c); + + System.out.println("\n--- 2. BORRANDO COCHES ---"); + borrarConLista(c, listaCoches); + + System.out.println("\n--- 3. INSERTANDO COCHES ---"); + insertarConLista(c, listaCoches); + + System.out.println("\n--- 4. ACTUALIZANDO COCHE 5 ---"); + actualizarCoche(c, 5, "Ibiza Eco"); + + System.out.println("\n--- RESULTADO FINAL EN LA BBDD ---"); + consultarCoches(c); // Volvemos a leer para ver que el coche 5 ahora es "Ibiza Eco" + + + }catch(SQLException e){ + System.out.println("Error crítico con la Base de Datos: " + e.getMessage()); + } + + + } + + private static List consultarCoches(Connection c) throws SQLException { + List coches = new ArrayList<>(); + String sql = "SELECT * FROM T_COCHE"; + + // Primero creamos el mensajero y luego este le lleva la pregunta y nos devuelve un ResultSet + // El ResultSet es la hoja con las filas de respuesta + try(Statement sentencia = c.createStatement(); + ResultSet resultado = sentencia.executeQuery(sql)){ + // Recorremos las filas. Entre parentesis lo que hace es avanzar de linea y devuelve false cuando ya no quedan + while (resultado.next()) { +//resultado!!, mira los títulos de esta fila, busca la columna que tiene el letrero 'identificador' escrito, y saca el número entero (getInt) que hay dentro + Coche coche = new Coche( + resultado.getInt("identificador"), + resultado.getString("marca"), + resultado.getString("modelo"), + resultado.getDouble("cilindrada")); + + coches.add(coche); + System.out.println(coche.toString()); + } + } + return coches; + } + + + + private static void borrarConLista(Connection c, List listaCoches) throws SQLException { + + String sql = "DELETE FROM T_COCHE WHERE identificador = ?"; + // Usamos PreparedStatement porque tenemos un dato variable (?) + try(PreparedStatement sentencia = c.prepareStatement(sql)){ + + for(Coche coche:listaCoches) { + sentencia.setInt(1, coche.getIdentificador()); + + int filas = sentencia.executeUpdate(); + + System.out.println("Borrado el coche " + coche.getIdentificador() + + "filas afectadas: " + filas ); + + } + + } + + } + + + + private static void insertarConLista(Connection c, List listaCoches) throws SQLException { + + String sql = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)"; + + try(PreparedStatement sentencia = c.prepareStatement(sql)){ + + for (Coche coche : listaCoches) { + sentencia.setInt(1, coche.getIdentificador()); + sentencia.setString(2, coche.getMarca()); + sentencia.setString(3, coche.getModelo()); + sentencia.setDouble(4, coche.getCilindrada()); + + sentencia.executeUpdate(); + } + + System.out.println("Se ha insertado " + listaCoches.size() + "coches"); + + } + } + + private static void actualizarCoche(Connection c, int identificador, String modeloNuevo) throws SQLException { + + String sql = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?"; + + try(PreparedStatement sentencia = c.prepareStatement(sql)){ + + sentencia.setString(1, modeloNuevo); + sentencia.setInt(2, identificador); + + int filasAfectadas = sentencia.executeUpdate(); + + if (filasAfectadas > 0) { + System.out.println("El coche con identificador 5 ha sido actualizado a 'Ibiza Eco'."); + } + } + } + +} diff --git a/src/org/palomafp/aadd/ss/Coche.java b/src/org/palomafp/aadd/ss/Coche.java new file mode 100644 index 0000000..38a88ab --- /dev/null +++ b/src/org/palomafp/aadd/ss/Coche.java @@ -0,0 +1,64 @@ +package org.palomafp.aadd.ss; + +public class Coche { + private int identificador; + private String marca; + private String modelo; + private double cilindrada; + + public Coche(int identificador, String marca, String modelo,double cilindrada ) { + this.identificador = identificador; + this.marca = marca; + this.modelo = modelo; + this.cilindrada = cilindrada; + } + + public Coche() { + } + + 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/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..cc2533d --- /dev/null +++ b/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Java-Version: 25 +Build-Jdk-Spec: 25 +Created-By: Maven Integration for Eclipse + diff --git a/target/classes/META-INF/maven/Dependencia_mysql/Dependencia_mysql/pom.properties b/target/classes/META-INF/maven/Dependencia_mysql/Dependencia_mysql/pom.properties new file mode 100644 index 0000000..4949b38 --- /dev/null +++ b/target/classes/META-INF/maven/Dependencia_mysql/Dependencia_mysql/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Wed Oct 28 23:24:54 CET 2026 +artifactId=Dependencia_mysql +groupId=Dependencia_mysql +m2e.projectLocation=C\:\\Users\\sajed\\Desktop\\2DAM\\AADD\\eclipse-workspace\\aadd_conect01 +m2e.projectName=aadd_conect01 +version=0.0.1-SNAPSHOT diff --git a/target/classes/META-INF/maven/Dependencia_mysql/Dependencia_mysql/pom.xml b/target/classes/META-INF/maven/Dependencia_mysql/Dependencia_mysql/pom.xml new file mode 100644 index 0000000..b5ccdd2 --- /dev/null +++ b/target/classes/META-INF/maven/Dependencia_mysql/Dependencia_mysql/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + Dependencia_mysql + Dependencia_mysql + 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/target/classes/org/palomafp/aadd/ss/AppPrincipal.class b/target/classes/org/palomafp/aadd/ss/AppPrincipal.class new file mode 100644 index 0000000..43d60b6 Binary files /dev/null and b/target/classes/org/palomafp/aadd/ss/AppPrincipal.class differ diff --git a/target/classes/org/palomafp/aadd/ss/Coche.class b/target/classes/org/palomafp/aadd/ss/Coche.class new file mode 100644 index 0000000..837b45b Binary files /dev/null and b/target/classes/org/palomafp/aadd/ss/Coche.class differ