primero
This commit is contained in:
+90
-9
@@ -2,24 +2,105 @@ package anp;
|
||||
|
||||
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 mainclass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static 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 String getMarca() { return marca; }
|
||||
public String getModelo() { return modelo; }
|
||||
public double getCilindrada() { return cilindrada; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Coche [ID=" + identificador + ", Marca=" + marca + ", Modelo=" + modelo + ", Cilindrada=" + cilindrada + "]";
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String url = "jdbc:mysql://10.100.11.150:3306/concesionario?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
|
||||
String usuario = "root";
|
||||
String password = "1234";
|
||||
|
||||
try {
|
||||
java.sql.Connection conexion = java.sql.DriverManager.getConnection(url, usuario, password);
|
||||
System.out.println("¡Conexión exitosa!");
|
||||
} catch (java.sql.SQLException e) {
|
||||
try (Connection conexion = DriverManager.getConnection(url, usuario, password)) {
|
||||
System.out.println("¡Conexión exitosa!\n");
|
||||
|
||||
Statement stmt = conexion.createStatement();
|
||||
List<Coche> listaCoches = new ArrayList<>();
|
||||
|
||||
System.out.println("=== 1. CONSULTA DE COCHES DESDE LA BD ===");
|
||||
String sqlSelect = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE";
|
||||
try (ResultSet rs = stmt.executeQuery(sqlSelect)) {
|
||||
while (rs.next()) {
|
||||
Coche coche = new Coche(
|
||||
rs.getInt("identificador"),
|
||||
rs.getString("marca"),
|
||||
rs.getString("modelo"),
|
||||
rs.getDouble("cilindrada")
|
||||
);
|
||||
listaCoches.add(coche);
|
||||
System.out.println(coche);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\n=== 2. ACTUALIZANDO EL COCHE CON CÓDIGO 5 ===");
|
||||
String sqlUpdate = "UPDATE T_COCHE SET modelo = 'Ibiza Eco' WHERE identificador = 5";
|
||||
int filasActualizadas = stmt.executeUpdate(sqlUpdate);
|
||||
if (filasActualizadas > 0) {
|
||||
System.out.println("-> Coche con código 5 actualizado correctamente a 'Ibiza Eco'.");
|
||||
} else {
|
||||
System.out.println("-> No se encontró el coche con código 5.");
|
||||
}
|
||||
|
||||
System.out.println("\n=== 3. BORRANDO MEDIANTE LA LISTA DE COCHES ===");
|
||||
String sqlDelete = "DELETE FROM T_COCHE WHERE identificador = ?";
|
||||
try (PreparedStatement pstmtDelete = conexion.prepareStatement(sqlDelete)) {
|
||||
for (Coche c : listaCoches) {
|
||||
pstmtDelete.setInt(1, c.getIdentificador());
|
||||
pstmtDelete.executeUpdate();
|
||||
}
|
||||
System.out.println("-> Se han borrado de la base de datos los coches contenidos en la lista.");
|
||||
}
|
||||
|
||||
System.out.println("\n=== 4. INSERTANDO LOS COCHES A PARTIR DE LA LISTA ===");
|
||||
String sqlInsert = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)";
|
||||
try (PreparedStatement pstmtInsert = conexion.prepareStatement(sqlInsert)) {
|
||||
for (Coche c : listaCoches) {
|
||||
pstmtInsert.setInt(1, c.getIdentificador());
|
||||
pstmtInsert.setString(2, c.getMarca());
|
||||
|
||||
String modeloFinal = (c.getIdentificador() == 5) ? "Ibiza Eco" : c.getModelo();
|
||||
pstmtInsert.setString(3, modeloFinal);
|
||||
|
||||
pstmtInsert.setDouble(4, c.getCilindrada());
|
||||
pstmtInsert.executeUpdate();
|
||||
}
|
||||
System.out.println("-> Se han insertado todos los coches de la lista correctamente.");
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#Generated by Maven Integration for Eclipse
|
||||
#Tue Oct 27 18:06:54 CET 2026
|
||||
#Thu Oct 29 15:47:29 CET 2026
|
||||
artifactId=Conectores_01
|
||||
groupId=Conectores_01
|
||||
m2e.projectLocation=C\:\\Users\\alumnoT\\eclipse-workspace\\Conectores_01
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user