1
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
@@ -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>Conectores_01</groupId>
|
||||||
|
<artifactId>Conectores_01</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<!-- Source: https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
|
||||||
|
<dependencies>
|
||||||
|
<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>26</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package scripts;
|
||||||
|
|
||||||
|
public class Coche {
|
||||||
|
private int codigo;
|
||||||
|
private String marca;
|
||||||
|
private String modelo;
|
||||||
|
private double precio;
|
||||||
|
|
||||||
|
public Coche(int codigo, String marca, String modelo, double precio) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.marca = marca;
|
||||||
|
this.modelo = modelo;
|
||||||
|
this.precio = precio;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
public int getCodigo() { return codigo; }
|
||||||
|
public String getMarca() { return marca; }
|
||||||
|
public String getModelo() { return modelo; }
|
||||||
|
public double getPrecio() { return precio; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Coche [Código=" + codigo + ", Marca=" + marca + ", Modelo=" + modelo + ", Precio=" + precio + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package scripts;
|
||||||
|
//10.100.11.138
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// 1. Datos de conexión directos (¡Recuerda cambiar localhost por la IP de tu M.V. si ejecutas desde fuera!)
|
||||||
|
String url = "jdbc:mysql://10.100.11.138:3306/concesionario?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
|
||||||
|
String user = "root";
|
||||||
|
String pass = "1234"; // Cambia por tu contraseña de la M.V.
|
||||||
|
|
||||||
|
// Conectar a la base de datos
|
||||||
|
Connection conexion = DriverManager.getConnection(url, user, pass);
|
||||||
|
Statement stmt = conexion.createStatement();
|
||||||
|
System.out.println("=== Conectado a MySQL ===\n");
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
// A. CONSULTAR LA LISTA DE COCHES E IMPRIMIRLOS
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
ResultSet rs = stmt.executeQuery("SELECT * FROM T_COCHE");
|
||||||
|
List<Integer> listaCodigosIniciales = new ArrayList<>();
|
||||||
|
|
||||||
|
System.out.println("--- 1. Consultando coches de T_COCHE ---");
|
||||||
|
while (rs.next()) {
|
||||||
|
int id = rs.getInt("identificador");
|
||||||
|
String marca = rs.getString("marca");
|
||||||
|
String modelo = rs.getString("modelo");
|
||||||
|
double cilindrada = rs.getDouble("cilindrada");
|
||||||
|
|
||||||
|
System.out.println("Coche -> ID: " + id + " | " + marca + " " + modelo + " | Cilindrada: " + cilindrada);
|
||||||
|
|
||||||
|
// Guardamos los identificadores en la lista para el borrado
|
||||||
|
listaCodigosIniciales.add(id);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
// B. BORRAR MEDIANTE LA LISTA DE COCHES
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
System.out.println("\n--- 2. Borrando coches mediante la lista ---");
|
||||||
|
PreparedStatement psDelete = conexion.prepareStatement("DELETE FROM T_COCHE WHERE identificador = ?");
|
||||||
|
for (int id : listaCodigosIniciales) {
|
||||||
|
psDelete.setInt(1, id);
|
||||||
|
psDelete.executeUpdate();
|
||||||
|
System.out.println("Borrado ID: " + id);
|
||||||
|
}
|
||||||
|
psDelete.close();
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
// C. RE-INSERTAR LOS COCHES DESDE LA LISTA INICIAL (Tu SQL Exacto)
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
System.out.println("\n--- 3. Reinsertando coches iniciales ---");
|
||||||
|
PreparedStatement psInsert = conexion.prepareStatement("INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)");
|
||||||
|
|
||||||
|
// Array con tus datos exactos para insertarlos limpiamente en un bucle corto
|
||||||
|
Object[][] datosCoches = {
|
||||||
|
{1, "Renault", "Megane", 1.5},
|
||||||
|
{2, "Seat", "León", 1.6},
|
||||||
|
{3, "Suzuki", "Vitara", 1.9},
|
||||||
|
{4, "Seat", "Clio", 1.4},
|
||||||
|
{5, "Seat", "Ibiza", 1.4}, // Este volverá a su estado inicial antes del update
|
||||||
|
{6, "Peugeot", "308", 2.0},
|
||||||
|
{7, "Renault", "Megane", 1.5}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (Object[] coche : datosCoches) {
|
||||||
|
psInsert.setInt(1, (int) coche[0]);
|
||||||
|
psInsert.setString(2, (String) coche[1]);
|
||||||
|
psInsert.setString(3, (String) coche[2]);
|
||||||
|
psInsert.setDouble(4, (double) coche[3]);
|
||||||
|
psInsert.executeUpdate();
|
||||||
|
System.out.println("Reinsertado ID: " + coche[0] + " (" + coche[2] + ")");
|
||||||
|
}
|
||||||
|
psInsert.close();
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
// D. ACTUALIZAR EL COCHE CON CÓDIGO 5 A "Ibiza Eco"
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
System.out.println("\n--- 4. Actualizando coche con identificador 5 ---");
|
||||||
|
PreparedStatement psUpdate = conexion.prepareStatement("UPDATE T_COCHE SET modelo = ? WHERE identificador = ?");
|
||||||
|
psUpdate.setString(1, "Ibiza Eco");
|
||||||
|
psUpdate.setInt(2, 5);
|
||||||
|
|
||||||
|
int filas = psUpdate.executeUpdate();
|
||||||
|
System.out.println("Filas actualizadas: " + filas + " (Modelo del ID 5 cambiado a 'Ibiza Eco')");
|
||||||
|
psUpdate.close();
|
||||||
|
|
||||||
|
// Cerrar recursos globales
|
||||||
|
stmt.close();
|
||||||
|
conexion.close();
|
||||||
|
System.out.println("\n=== Procesos CRUD finalizados con éxito ===");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Java-Version: 26
|
||||||
|
Build-Jdk-Spec: 25
|
||||||
|
Created-By: Maven Integration for Eclipse
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#Generated by Maven Integration for Eclipse
|
||||||
|
#Tue Oct 27 17:23:44 CET 2026
|
||||||
|
artifactId=Conectores_01
|
||||||
|
groupId=Conectores_01
|
||||||
|
m2e.projectLocation=C\:\\Users\\alumnoT\\eclipse-workspace\\Conectores_01
|
||||||
|
m2e.projectName=Conectores_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>Conectores_01</groupId>
|
||||||
|
<artifactId>Conectores_01</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<!-- Source: https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
|
||||||
|
<dependencies>
|
||||||
|
<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>26</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user