132 lines
3.5 KiB
Java
132 lines
3.5 KiB
Java
package org.palomafp.aadd.inm;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
import org.palomafp.aadd.inm.model.Coche;
|
|
|
|
/**
|
|
*
|
|
* AppPrincipal: Clase que realiza ....
|
|
*
|
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
|
* @date 28 oct 2026
|
|
*
|
|
*
|
|
*/
|
|
|
|
public class AppPrincipal {
|
|
|
|
public static void main(String[] args) {
|
|
AppPrincipal app = new AppPrincipal();
|
|
|
|
Connection conexion = app.obtenerConexionBBDD();
|
|
|
|
// 1. Obtener lista Coches
|
|
List<Coche> listaCoches = app.obtenerListaCoches(conexion);
|
|
System.out.println(listaCoches);
|
|
|
|
// 2. Borrar Coches
|
|
app.borrarCoches(conexion);
|
|
|
|
// 3. Insertar Coches
|
|
app.insertarCoches(conexion, listaCoches);
|
|
|
|
// 4. Actualizar Coche
|
|
app.actualizarCoche(conexion, 5, "Ibiza Eco");
|
|
}
|
|
|
|
private List<Coche> obtenerListaCoches(Connection conexion) {
|
|
List<Coche> listaCoches = null;
|
|
|
|
String sentenciaSQL = "SELECT * FROM T_COCHE";
|
|
try (Statement sentencia = conexion.createStatement();
|
|
ResultSet resultados = sentencia.executeQuery(sentenciaSQL);) {
|
|
while (resultados.next()) {
|
|
if (listaCoches == null) {
|
|
listaCoches = new ArrayList<Coche>();
|
|
}
|
|
int identificador = resultados.getInt("identificador");
|
|
String marca = resultados.getString("marca");
|
|
String modelo = resultados.getString("modelo");
|
|
float cilindrada = resultados.getFloat("cilindrada");
|
|
|
|
Coche coche = new Coche();
|
|
coche.setIdentificador(identificador);
|
|
coche.setMarca(marca);
|
|
coche.setModelo(modelo);
|
|
coche.setCilindrada(cilindrada);
|
|
|
|
listaCoches.add(coche);
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return listaCoches;
|
|
}
|
|
|
|
private void borrarCoches(Connection conexion) {
|
|
String sentenciaSQL = "DELETE FROM T_COCHE";
|
|
|
|
try (Statement sentencia = conexion.createStatement();) {
|
|
sentencia.execute(sentenciaSQL);
|
|
System.out.println(sentenciaSQL);
|
|
} catch (SQLException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
private void insertarCoches(Connection conexion, List<Coche> listaCoches) {
|
|
String sentenciaSQL = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (%d, '%s', '%s', %.2f)";
|
|
try (Statement sentencia = conexion.createStatement();) {
|
|
for (Coche coche : listaCoches) {
|
|
String sentenciaSQLRellena = String.format(Locale.US,sentenciaSQL, coche.getIdentificador(), coche.getMarca(),
|
|
coche.getModelo(), coche.getCilindrada());
|
|
|
|
sentencia.execute(sentenciaSQLRellena);
|
|
System.out.println(sentenciaSQLRellena);
|
|
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void actualizarCoche(Connection conexion, int idCoche, String modelo) {
|
|
String sentenciaSQL = "UPDATE T_COCHE SET modelo = '%s' WHERE identificador = %d";
|
|
|
|
try (Statement sentencia = conexion.createStatement();) {
|
|
sentenciaSQL = String.format(sentenciaSQL, modelo, idCoche);
|
|
|
|
sentencia.execute(sentenciaSQL);
|
|
System.out.println(sentenciaSQL);
|
|
} catch (SQLException e) {
|
|
System.err.println(e);
|
|
}
|
|
|
|
}
|
|
|
|
private Connection obtenerConexionBBDD() {
|
|
Connection conexion = null;
|
|
try {
|
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
|
|
|
conexion = DriverManager.getConnection("jdbc:mysql://192.168.1.36:3306/concesionario", "root", "mysql_123");
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return conexion;
|
|
}
|
|
|
|
}
|