From 4f0007aef83764a8e6d6e8c2bb2e8adae7800e20 Mon Sep 17 00:00:00 2001 From: "felipe.garcia3" Date: Tue, 3 Nov 2026 17:08:59 +0100 Subject: [PATCH] Ejercicio Hecho --- .gitignore | 6 ++ pom.xml | 27 ++++++ src/org/palomafp/dam2/aadd/fgm/Coche.java | 55 +++++++++++ .../palomafp/dam2/aadd/fgm/Concesionario.java | 91 +++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/org/palomafp/dam2/aadd/fgm/Coche.java create mode 100644 src/org/palomafp/dam2/aadd/fgm/Concesionario.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64e22b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/bin/ +*.class +.classpath +.project +.settings/ +target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8e7adbc --- /dev/null +++ b/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + Actividad2.1 + Actividad2.1 + 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/dam2/aadd/fgm/Coche.java b/src/org/palomafp/dam2/aadd/fgm/Coche.java new file mode 100644 index 0000000..69af943 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/fgm/Coche.java @@ -0,0 +1,55 @@ +package org.palomafp.dam2.aadd.fgm; + +public class Coche { + + private int identificador; + private String marca; + private String modelo; + private double 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; + } + + + public Coche(int identificador, String marca, String modelo, double cilindrada) { + this.identificador = identificador; + this.marca = marca; + this.modelo = modelo; + this.cilindrada = cilindrada; + } + + + public Coche() {} + + + @Override + public String toString() { + return "Coche " + identificador + ": " + marca + " " + modelo + ", cilindrada " + cilindrada; + } + + + + +} diff --git a/src/org/palomafp/dam2/aadd/fgm/Concesionario.java b/src/org/palomafp/dam2/aadd/fgm/Concesionario.java new file mode 100644 index 0000000..545b064 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/fgm/Concesionario.java @@ -0,0 +1,91 @@ +package org.palomafp.dam2.aadd.fgm; + +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 Concesionario { + + private static final String URL = "jdbc:mysql://10.100.11.145:3306/concesionario"; + private static final String USUARIO = "root"; + private static final String PASSWORD = "1234"; + + public static List consultarCoches (Connection conexion) throws SQLException { + List listaDeCoches = new ArrayList<>(); + Statement sentencia = conexion.createStatement(); + ResultSet resultadoConsulta = sentencia.executeQuery("SELECT * FROM T_COCHE"); + + while (resultadoConsulta.next()) { + int identificador = resultadoConsulta.getInt("identificador"); + String marca = resultadoConsulta.getString("marca"); + String modelo = resultadoConsulta.getString("modelo"); + double cilindrada = resultadoConsulta.getDouble("cilindrada"); + + Coche coche = new Coche (identificador, marca, modelo, cilindrada); + listaDeCoches.add(coche); + } + + resultadoConsulta.close(); + sentencia.close(); + + return listaDeCoches; + } + + public static void borrarCoches (Connection conexion, List listaDeCoches) throws SQLException { + Statement sentencia = conexion.createStatement(); + for (Coche coche : listaDeCoches) { + sentencia.executeUpdate("DELETE FROM T_COCHE WHERE identificador = " + coche.getIdentificador()); + } + sentencia.close(); + + } + + public static void insertarCoches (Connection conexion, List listaDeCoches) throws SQLException { + Statement sentencia = conexion.createStatement(); + for (Coche coche : listaDeCoches) { + sentencia.executeUpdate("INSERT INTO T_COCHE VALUES (" + coche.getIdentificador() + ", '" + coche.getMarca() + "', '" + coche.getModelo() + "', " + coche.getCilindrada() + ")"); + } + sentencia.close(); + } + + public static void actualizarCoche (Connection conexion, int identificador, String modeloNuevo) throws SQLException { + Statement sentencia = conexion.createStatement(); + sentencia.executeUpdate("UPDATE T_COCHE SET modelo = '" + modeloNuevo + "' WHERE identificador = " + identificador); + sentencia.close(); + } + + + public static void main(String[] args) throws SQLException { + + Connection conexion = DriverManager.getConnection(URL, USUARIO, PASSWORD); + System.out.println("Conectado a la BBDD"); + + List listaDeCoches = consultarCoches(conexion); + System.out.println("LISTA DE COCHES"); + for (Coche coche : listaDeCoches) { + System.out.println(coche); + } + + borrarCoches (conexion, listaDeCoches); + System.out.println("Coches BORRADOS"); + + insertarCoches (conexion, listaDeCoches); + System.out.println("Coches INSERTADOS"); + + actualizarCoche(conexion, 5, "Ibiza Eco"); + System.out.println("El coche 5 se ha actualizado correctamente"); + + System.out.println("La lista final de coches es: "); + for (Coche coche: consultarCoches(conexion)) { + System.out.println(coche); + } + conexion.close(); + + } +}