Primer commit
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
# Compilados de Java
|
||||
*.class
|
||||
bin/
|
||||
|
||||
# Compilados de Maven
|
||||
target/
|
||||
|
||||
# Configuración de Eclipse
|
||||
.project
|
||||
.classpath
|
||||
.settings/
|
||||
@@ -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>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>21</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,159 @@
|
||||
package org.palomafp.aadd.smmb;
|
||||
|
||||
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;
|
||||
|
||||
import org.palomafp.aadd.smmb.model.Coche;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static final String URL = "jdbc:mysql://localhost:3306/concesionario";
|
||||
private static final String USUARIO = "root";
|
||||
private static final String CLAVE = "root1234";
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Abrimos la conexión dentro del parentesis para que se cierre solo
|
||||
try (Connection conexion = DriverManager.getConnection(URL, USUARIO, CLAVE)) {
|
||||
|
||||
System.out.println("Conexión establecida correctamente");
|
||||
|
||||
List<Coche> coches = consultarCoches(conexion);
|
||||
|
||||
for (Coche coche : coches) {
|
||||
System.out.println(coche);
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("--------------------------------------------------------------------------");
|
||||
System.out.println("");
|
||||
|
||||
borrarCoches(conexion, coches);
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("--------------------------------------------------------------------------");
|
||||
System.out.println("");
|
||||
|
||||
insertarCoches(conexion, coches);
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("--------------------------------------------------------------------------");
|
||||
System.out.println("");
|
||||
|
||||
actualizarModelo(conexion, 5, "Ibiza Eco");
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("--------------------------------------------------------------------------");
|
||||
System.out.println("");
|
||||
|
||||
List<Coche> cochesFinales = consultarCoches(conexion);
|
||||
for (Coche coche : cochesFinales) {
|
||||
System.out.println(coche);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.err.println("Error al conectar: " + e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Recibe la conexión abierta, asi no la abro cada vez
|
||||
private static List<Coche> consultarCoches(Connection conexion) throws SQLException {
|
||||
|
||||
List<Coche> coches = new ArrayList<>();
|
||||
|
||||
// Consulta a la base de datos
|
||||
String sql = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE";
|
||||
|
||||
// Primero creamos el mensajero y luego este le lleva la pregunta y nos devuelve un ResultSet
|
||||
// El ResultSet es la hoja con las filas de respuesta
|
||||
try (Statement sentencia = conexion.createStatement();
|
||||
ResultSet resultado = sentencia.executeQuery(sql)) {
|
||||
|
||||
// Recorremos las filas. Entre parentesis lo que hace es avanzar de linea y devuelve false cuando ya no quedan
|
||||
while (resultado.next()) {
|
||||
|
||||
Coche coche = new Coche();
|
||||
|
||||
// El getInt es el tipo de la variable en la que queremos recibir el dato que le estamos pidiendo
|
||||
coche.setIdentificador(resultado.getInt("identificador"));
|
||||
coche.setMarca(resultado.getString("marca"));
|
||||
coche.setModelo(resultado.getString("modelo"));
|
||||
coche.setCilindrada(resultado.getDouble("cilindrada"));
|
||||
|
||||
coches.add(coche);
|
||||
}
|
||||
}
|
||||
|
||||
// Devolvemos la lista vacía o rellena, pero la devolvemos
|
||||
return coches;
|
||||
}
|
||||
|
||||
private static void borrarCoches(Connection conexion, List<Coche> coches) throws SQLException {
|
||||
|
||||
// Esto es omo una plantilla en blanco, el valor lo dejamos en blanco y lo ponemos nosotros después en la siguiente línea
|
||||
String sql = "DELETE FROM T_COCHE WHERE identificador = ?";
|
||||
|
||||
// Creamos un mensajero y le mandamos la sentencia
|
||||
try (PreparedStatement sentencia = conexion.prepareStatement(sql)) {
|
||||
|
||||
for (Coche coche : coches) {
|
||||
|
||||
// Rellenamos el primer hueco ( que tiene esto ?) con el id del coche
|
||||
sentencia.setInt(1, coche.getIdentificador());
|
||||
|
||||
// Enviamos la orden a SQL, se ejecuta, y devuelve cuantas filas ha cambiado
|
||||
int filas = sentencia.executeUpdate();
|
||||
|
||||
// Imprimimos lo que se ha borrado
|
||||
System.out.println("Borrado el coche " + coche.getIdentificador() + " (filas afectadas: " + filas + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void insertarCoches(Connection conexion, List<Coche> coches) throws SQLException {
|
||||
|
||||
// Los ? se enumeran por orden de aparición
|
||||
String sql = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)";
|
||||
|
||||
try (PreparedStatement sentencia = conexion.prepareStatement(sql)) {
|
||||
|
||||
for (Coche coche : coches) {
|
||||
|
||||
// Ponemos el tipo que tiene que tener cada columna. Ponemos el orden de los ? y el valor que lo sacamos de la lista de coches que aun existe
|
||||
sentencia.setInt(1, coche.getIdentificador());
|
||||
sentencia.setString(2, coche.getMarca());
|
||||
sentencia.setString(3, coche.getModelo());
|
||||
sentencia.setDouble(4, coche.getCilindrada());
|
||||
|
||||
int filas = sentencia.executeUpdate();
|
||||
|
||||
System.out.println("Insertado el coche " + coche.getIdentificador() + " (filas afectadas: " + filas + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void actualizarModelo(Connection conexion, int identificador, String nuevoModelo) throws SQLException {
|
||||
|
||||
String sql = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?";
|
||||
|
||||
try (PreparedStatement sentencia = conexion.prepareStatement(sql)) {
|
||||
|
||||
// Le decimos que ponga el nuevo con el set donde sea el identificador del where
|
||||
sentencia.setString(1, nuevoModelo);
|
||||
sentencia.setInt(2, identificador);
|
||||
|
||||
int filas = sentencia.executeUpdate();
|
||||
|
||||
System.out.println("Actualizado el coche " + identificador + " (filas afectadas: " + filas + ")");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.palomafp.aadd.smmb.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Coche implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private int identificador;
|
||||
private String marca;
|
||||
private String modelo;
|
||||
private double cilindrada;
|
||||
|
||||
public Coche() {
|
||||
}
|
||||
|
||||
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 marca + " " + modelo + " (" + identificador + ") con cilindrada " + cilindrada;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user