Ejercicio Hecho
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
/bin/
|
||||
*.class
|
||||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
target/
|
||||
@@ -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>Actividad2.1</groupId>
|
||||
<artifactId>Actividad2.1</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,55 @@
|
||||
package org.palomafp.dam2.aadd.fgm;
|
||||
|
||||
public class Coche {
|
||||
|
||||
private int identificador;
|
||||
private String marca;
|
||||
private String modelo;
|
||||
private double cilindrada;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
public Coche(int identificador, String marca, String modelo, double cilindrada) {
|
||||
this.identificador = identificador;
|
||||
this.marca = marca;
|
||||
this.modelo = modelo;
|
||||
this.cilindrada = cilindrada;
|
||||
}
|
||||
|
||||
|
||||
public Coche() {}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Coche " + identificador + ": " + marca + " " + modelo + ", cilindrada " + cilindrada;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package org.palomafp.dam2.aadd.fgm;
|
||||
|
||||
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 Concesionario {
|
||||
|
||||
private static final String URL = "jdbc:mysql://10.100.11.145:3306/concesionario";
|
||||
private static final String USUARIO = "root";
|
||||
private static final String PASSWORD = "1234";
|
||||
|
||||
public static List<Coche> consultarCoches (Connection conexion) throws SQLException {
|
||||
List<Coche> listaDeCoches = new ArrayList<>();
|
||||
Statement sentencia = conexion.createStatement();
|
||||
ResultSet resultadoConsulta = sentencia.executeQuery("SELECT * FROM T_COCHE");
|
||||
|
||||
while (resultadoConsulta.next()) {
|
||||
int identificador = resultadoConsulta.getInt("identificador");
|
||||
String marca = resultadoConsulta.getString("marca");
|
||||
String modelo = resultadoConsulta.getString("modelo");
|
||||
double cilindrada = resultadoConsulta.getDouble("cilindrada");
|
||||
|
||||
Coche coche = new Coche (identificador, marca, modelo, cilindrada);
|
||||
listaDeCoches.add(coche);
|
||||
}
|
||||
|
||||
resultadoConsulta.close();
|
||||
sentencia.close();
|
||||
|
||||
return listaDeCoches;
|
||||
}
|
||||
|
||||
public static void borrarCoches (Connection conexion, List<Coche> listaDeCoches) throws SQLException {
|
||||
Statement sentencia = conexion.createStatement();
|
||||
for (Coche coche : listaDeCoches) {
|
||||
sentencia.executeUpdate("DELETE FROM T_COCHE WHERE identificador = " + coche.getIdentificador());
|
||||
}
|
||||
sentencia.close();
|
||||
|
||||
}
|
||||
|
||||
public static void insertarCoches (Connection conexion, List<Coche> listaDeCoches) throws SQLException {
|
||||
Statement sentencia = conexion.createStatement();
|
||||
for (Coche coche : listaDeCoches) {
|
||||
sentencia.executeUpdate("INSERT INTO T_COCHE VALUES (" + coche.getIdentificador() + ", '" + coche.getMarca() + "', '" + coche.getModelo() + "', " + coche.getCilindrada() + ")");
|
||||
}
|
||||
sentencia.close();
|
||||
}
|
||||
|
||||
public static void actualizarCoche (Connection conexion, int identificador, String modeloNuevo) throws SQLException {
|
||||
Statement sentencia = conexion.createStatement();
|
||||
sentencia.executeUpdate("UPDATE T_COCHE SET modelo = '" + modeloNuevo + "' WHERE identificador = " + identificador);
|
||||
sentencia.close();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
Connection conexion = DriverManager.getConnection(URL, USUARIO, PASSWORD);
|
||||
System.out.println("Conectado a la BBDD");
|
||||
|
||||
List<Coche> listaDeCoches = consultarCoches(conexion);
|
||||
System.out.println("LISTA DE COCHES");
|
||||
for (Coche coche : listaDeCoches) {
|
||||
System.out.println(coche);
|
||||
}
|
||||
|
||||
borrarCoches (conexion, listaDeCoches);
|
||||
System.out.println("Coches BORRADOS");
|
||||
|
||||
insertarCoches (conexion, listaDeCoches);
|
||||
System.out.println("Coches INSERTADOS");
|
||||
|
||||
actualizarCoche(conexion, 5, "Ibiza Eco");
|
||||
System.out.println("El coche 5 se ha actualizado correctamente");
|
||||
|
||||
System.out.println("La lista final de coches es: ");
|
||||
for (Coche coche: consultarCoches(conexion)) {
|
||||
System.out.println(coche);
|
||||
}
|
||||
conexion.close();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user