Actividad 2.1 CRUD sobre una base de datos en MySQL

This commit is contained in:
gonzalo.diaz18
2026-10-29 16:04:18 +01:00
commit 631956afa3
12 changed files with 307 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
+23
View File
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>bbdd_01</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
+12
View File
@@ -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
+4
View File
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
+27
View File
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bbdd_01</groupId>
<artifactId>bbdd_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Source: https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>26.10.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>21</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
+60
View File
@@ -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 + "]";
}
}
+122
View File
@@ -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<Coche> consultarCoches(Connection conexion) throws SQLException {
List<Coche> 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<Coche> 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<Coche> 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());
}
}
}
}
}
+4
View File
@@ -0,0 +1,4 @@
Manifest-Version: 1.0
Build-Jdk-Spec: 21
Created-By: Maven Integration for Eclipse
@@ -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
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bbdd_01</groupId>
<artifactId>bbdd_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Source: https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>26.10.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>21</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
Binary file not shown.
Binary file not shown.