commit 7b1d5a2697ed381cb4c027d03e3e4b8420b3f171 Author: jxu4 Date: Tue Oct 27 18:09:30 2026 +0100 Primer commit Conectores diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbaae6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Eclipse +.classpath +.project +.settings/ + +# Compilación Java +bin/ +target/ +*.class + +# Logs +*.log + +# Sistema operativo +.DS_Store +Thumbs.db \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b0a2c16 --- /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/module-info.java b/src/module-info.java new file mode 100644 index 0000000..35a1322 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,9 @@ +/** + * + */ +/** + * + */ +module Conectores_01 { + requires java.sql; +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/jx/AppPrincipal.java b/src/org/palomafp/aadd/jx/AppPrincipal.java new file mode 100644 index 0000000..0723c5b --- /dev/null +++ b/src/org/palomafp/aadd/jx/AppPrincipal.java @@ -0,0 +1,113 @@ +package org.palomafp.aadd.jx; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.palomafp.aadd.jx.model.Coche; + +public class AppPrincipal { + + private static final String URL = "jdbc:mysql://10.100.11.133:3306/concesionario"; + private static final String USUARIO = "root"; + private static final String PASSWORD = "1234"; + + private static final String SQL_CONSULTAR = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE"; + private static final String SQL_BORRAR = "DELETE FROM T_COCHE WHERE identificador = ?"; + private static final String SQL_INSERTAR = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)"; + private static final String SQL_ACTUALIZAR = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?"; + + public static void main(String[] args) { + + try (Connection conexion = DriverManager.getConnection(URL, USUARIO, PASSWORD)) { + + List listaCoches = consultarCoches(conexion); + System.out.println("--- Lista inicial de coches ---"); + for (Coche c : listaCoches) { + System.out.println(c); + } + + int borrados = borrarCoches(conexion, listaCoches); + System.out.println("Coches borrados: " + borrados); + + int insertados = insertarCoches(conexion, listaCoches); + System.out.println("Coches insertados: " + insertados); + + int actualizados = actualizarModelo(conexion, 5, "Ibiza Eco"); + System.out.println("Coches actualizados: " + actualizados); + + System.out.println("--- Lista final de coches ---"); + for (Coche c : consultarCoches(conexion)) { + System.out.println(c); + } + + } catch (SQLException e) { + System.out.println("Error con la base de datos: " + e.getMessage()); + } + } + + private static List consultarCoches(Connection conexion) throws SQLException { + List listaCoches = new ArrayList<>(); + + try (PreparedStatement sentencia = conexion.prepareStatement(SQL_CONSULTAR); + ResultSet resultado = sentencia.executeQuery()) { + + while (resultado.next()) { + Coche coche = new Coche(); + coche.setIdentificador(resultado.getInt("identificador")); + coche.setMarca(resultado.getString("marca")); + coche.setModelo(resultado.getString("modelo")); + coche.setCilindrada(resultado.getDouble("cilindrada")); + listaCoches.add(coche); + } + } + + return listaCoches; + } + + private static int borrarCoches(Connection conexion, List listaCoches) throws SQLException { + int total = 0; + + try (PreparedStatement sentencia = conexion.prepareStatement(SQL_BORRAR)) { + for (Coche c : listaCoches) { + sentencia.setInt(1, c.getIdentificador()); + total += sentencia.executeUpdate(); + } + } + + return total; + } + + private static int insertarCoches(Connection conexion, List listaCoches) throws SQLException { + int total = 0; + + try (PreparedStatement sentencia = conexion.prepareStatement(SQL_INSERTAR)) { + for (Coche c : listaCoches) { + sentencia.setInt(1, c.getIdentificador()); + sentencia.setString(2, c.getMarca()); + sentencia.setString(3, c.getModelo()); + sentencia.setDouble(4, c.getCilindrada()); + total += sentencia.executeUpdate(); + } + } + + return total; + } + + private static int actualizarModelo(Connection conexion, int identificador, String nuevoModelo) + throws SQLException { + int total = 0; + + try (PreparedStatement sentencia = conexion.prepareStatement(SQL_ACTUALIZAR)) { + sentencia.setString(1, nuevoModelo); + sentencia.setInt(2, identificador); + total = sentencia.executeUpdate(); + } + + return total; + } +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/jx/model/Coche.java b/src/org/palomafp/aadd/jx/model/Coche.java new file mode 100644 index 0000000..72938d3 --- /dev/null +++ b/src/org/palomafp/aadd/jx/model/Coche.java @@ -0,0 +1,57 @@ +package org.palomafp.aadd.jx.model; + +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; + } + + 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