commit e874b228f02aa0201b30c5917662f71e19614bfe Author: rocio.montero1 Date: Wed Oct 28 12:19:34 2026 +0100 Actividad 2.1 - CRUD sobre una base de datos en MySQL 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..5ee5ed7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + 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 + + 25 + + + + + \ No newline at end of file diff --git a/src/org/palomafp/aadd/rmr/Coche.java b/src/org/palomafp/aadd/rmr/Coche.java new file mode 100644 index 0000000..8d52977 --- /dev/null +++ b/src/org/palomafp/aadd/rmr/Coche.java @@ -0,0 +1,55 @@ +package org.palomafp.aadd.rmr; + +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 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 + ": Marca: " + marca + ", Modelo: " + modelo + ", Cilindrada: " + + cilindrada; + } + +} diff --git a/src/org/palomafp/aadd/rmr/MainCoches.java b/src/org/palomafp/aadd/rmr/MainCoches.java new file mode 100644 index 0000000..24a14df --- /dev/null +++ b/src/org/palomafp/aadd/rmr/MainCoches.java @@ -0,0 +1,90 @@ +package org.palomafp.aadd.rmr; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +public class MainCoches { + + private static final String URL = "jdbc:mysql://10.100.11.134:3307/concesionario"; + private static final String USUARIO = "root"; + private static final String PASS = "rootpass"; + + public static void main(String[] args) throws SQLException { + + Connection conexion = DriverManager.getConnection(URL, USUARIO, PASS); + System.out.println("Conectado a la BBDD"); + + // consulto e imprimo + 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(); + + } + + 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(); + } + +}