primer commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package org.palomafp.add.aqv;
|
||||
|
||||
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 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
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.palomafp.add.aqv;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainCRUD {
|
||||
|
||||
private static final String URL = "jdbc:mysql://10.100.11.140/";
|
||||
private static final String USER = "root";
|
||||
private static final String PASS = "1234";
|
||||
|
||||
public static void main(String[] args) {
|
||||
try (Connection conn = DriverManager.getConnection(URL, USER, PASS)) {
|
||||
|
||||
try (Statement st = conn.createStatement()) {
|
||||
st.executeUpdate("CREATE DATABASE IF NOT EXISTS Mapamundi");
|
||||
System.out.println("Base de datos Mapamundi creada/verificada.");
|
||||
}
|
||||
|
||||
conn.setCatalog("concesionario");
|
||||
|
||||
List<Coche> listaCoches = new ArrayList<>();
|
||||
String sqlSelect = "SELECT * FROM T_COCHE";
|
||||
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sqlSelect)) {
|
||||
while (rs.next()) {
|
||||
Coche c = new Coche(rs.getInt("identificador"), rs.getString("marca"), rs.getString("modelo"),
|
||||
rs.getDouble("cilindrada"));
|
||||
listaCoches.add(c);
|
||||
}
|
||||
}
|
||||
System.out.println("\n--- Lista inicial de coches ---");
|
||||
listaCoches.forEach(System.out::println);
|
||||
|
||||
String sqlDelete = "DELETE FROM T_COCHE WHERE identificador = ?";
|
||||
try (PreparedStatement pstDelete = conn.prepareStatement(sqlDelete)) {
|
||||
for (Coche c : listaCoches) {
|
||||
pstDelete.setInt(1, c.getIdentificador());
|
||||
pstDelete.executeUpdate();
|
||||
}
|
||||
System.out.println("\nCoches borrados iterando la lista obtenida.");
|
||||
}
|
||||
|
||||
String sqlInsert = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)";
|
||||
try (PreparedStatement pstInsert = conn.prepareStatement(sqlInsert)) {
|
||||
for (Coche c : listaCoches) {
|
||||
pstInsert.setInt(1, c.getIdentificador());
|
||||
pstInsert.setString(2, c.getMarca());
|
||||
pstInsert.setString(3, c.getModelo());
|
||||
pstInsert.setDouble(4, c.getCilindrada());
|
||||
pstInsert.executeUpdate();
|
||||
}
|
||||
System.out.println("Coches reinsertados a partir de la lista.");
|
||||
}
|
||||
|
||||
String sqlUpdate = "UPDATE T_COCHE SET modelo = 'Ibiza Eco' WHERE identificador = 5";
|
||||
try (Statement st = conn.createStatement()) {
|
||||
int filas = st.executeUpdate(sqlUpdate);
|
||||
if (filas > 0) {
|
||||
System.out.println("\nCoche 5 actualizado a 'Ibiza Eco'.");
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("\n--- Estado final de la tabla ---");
|
||||
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sqlSelect)) {
|
||||
while (rs.next()) {
|
||||
System.out.println("ID=" + rs.getInt("identificador") + ", Marca=" + rs.getString("marca")
|
||||
+ ", Modelo=" + rs.getString("modelo") + ", Cilindrada=" + rs.getDouble("cilindrada"));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error de base de datos: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user