Actividad 2.1
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
|||||||
|
<?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-25">
|
||||||
|
<attributes>
|
||||||
|
<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="src" path="target/generated-sources/annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="ignore_optional_problems" value="true"/>
|
||||||
|
<attribute name="m2e-apt" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="ignore_optional_problems" value="true"/>
|
||||||
|
<attribute name="m2e-apt" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
/bin/
|
||||||
|
/target/
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Conectores_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>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1793123414266</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.apt.aptEnabled=false
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=25
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=25
|
||||||
|
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=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.processAnnotations=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=25
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<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>Conectores_01</groupId>
|
||||||
|
<artifactId>Conectores_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>25</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package org.palomafp.aadd.aez;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.aez.model.Coche;
|
||||||
|
|
||||||
|
public class GestorCochesJDB {
|
||||||
|
|
||||||
|
private static final String URL = "jdbc:mysql://10.100.11.135:3306/concesionario?useSSL=false&allowPublicKeyRetrieval=true";
|
||||||
|
private static final String USUARIO = "Alejandro";
|
||||||
|
private static final String PASSWORD = "Combarro1!";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
try (Connection conn = DriverManager.getConnection(URL, USUARIO, PASSWORD)) {
|
||||||
|
System.out.println("Conexión con MySQL establecida correctamente.\n");
|
||||||
|
|
||||||
|
// 1. Consultar la lista de coches e imprimir por consola
|
||||||
|
System.out.println("Consultar cohes");
|
||||||
|
List<Coche> listaCoches = consultarCoches(conn);
|
||||||
|
listaCoches.forEach(System.out::println);
|
||||||
|
|
||||||
|
// 2. Borrar los registros usando la lista obtenida
|
||||||
|
System.out.println("\nBorrar Coches de la lista");
|
||||||
|
borrarCoches(conn, listaCoches);
|
||||||
|
|
||||||
|
// 3. Insertar cada coche de nuevo a partir de la lista inicial
|
||||||
|
System.out.println("\nInsertar coches");
|
||||||
|
insertarCoches(conn, listaCoches);
|
||||||
|
|
||||||
|
// 4. Actualizar el coche con código 5 (modelo pasa a ser "Ibiza Eco")
|
||||||
|
System.out.println("\nActualizar coches");
|
||||||
|
actualizarModeloCoche(conn, 5, "Ibiza Eco");
|
||||||
|
|
||||||
|
System.out.println("\nTabla final");
|
||||||
|
List<Coche> listaFinal = consultarCoches(conn);
|
||||||
|
listaFinal.forEach(System.out::println);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error durante las operaciones JDBC: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Coche> consultarCoches(Connection conn) throws SQLException {
|
||||||
|
List<Coche> lista = new ArrayList<>();
|
||||||
|
String sql = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE";
|
||||||
|
|
||||||
|
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
Coche coche = new Coche(rs.getInt("identificador"), rs.getString("marca"), rs.getString("modelo"),
|
||||||
|
rs.getDouble("cilindrada"));
|
||||||
|
lista.add(coche);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void borrarCoches(Connection conn, List<Coche> lista) throws SQLException {
|
||||||
|
String sql = "DELETE FROM T_COCHE WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
for (Coche coche : lista) {
|
||||||
|
pstmt.setInt(1, coche.getIdentificador());
|
||||||
|
int borrados = pstmt.executeUpdate();
|
||||||
|
System.out.println("Coche ID " + coche.getIdentificador() + " borrado (" + borrados + " registro).");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void insertarCoches(Connection conn, List<Coche> lista) throws SQLException {
|
||||||
|
String sql = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
for (Coche coche : lista) {
|
||||||
|
pstmt.setInt(1, coche.getIdentificador());
|
||||||
|
pstmt.setString(2, coche.getMarca());
|
||||||
|
pstmt.setString(3, coche.getModelo());
|
||||||
|
pstmt.setDouble(4, coche.getCilindrada());
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
System.out.println("Coche ID " + coche.getIdentificador() + " insertado.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void actualizarModeloCoche(Connection conn, int identificador, String nuevoModelo)
|
||||||
|
throws SQLException {
|
||||||
|
String sql = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
pstmt.setString(1, nuevoModelo);
|
||||||
|
pstmt.setInt(2, identificador);
|
||||||
|
int filas = pstmt.executeUpdate();
|
||||||
|
|
||||||
|
if (filas > 0) {
|
||||||
|
System.out.println("Coche ID " + identificador + " actualizado correctamente a '" + nuevoModelo + "'.");
|
||||||
|
} else {
|
||||||
|
System.out.println("No se encontró el coche con ID " + identificador);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package org.palomafp.aadd.aez.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clase Bean que representa la entidad T_COCHE.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters y Setters
|
||||||
|
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 + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user