Actividad 2.1 - CRUD sobre una base de datos en MySQL
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Compilación Java
|
||||||
|
bin/
|
||||||
|
target/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Sistema operativo
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<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>
|
||||||
|
<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.aadd.rmr;
|
||||||
|
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Coche - " + identificador + ": Marca: " + marca + ", Modelo: " + modelo + ", Cilindrada: "
|
||||||
|
+ cilindrada;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package org.palomafp.aadd.rmr;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class MainCoches {
|
||||||
|
|
||||||
|
private static final String URL = "jdbc:mysql://10.100.11.134:3307/concesionario";
|
||||||
|
private static final String USUARIO = "root";
|
||||||
|
private static final String PASS = "rootpass";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws SQLException {
|
||||||
|
|
||||||
|
Connection conexion = DriverManager.getConnection(URL, USUARIO, PASS);
|
||||||
|
System.out.println("Conectado a la BBDD");
|
||||||
|
|
||||||
|
// consulto e imprimo
|
||||||
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user