Primer commit Conectores
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,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>26</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module Conectores_01 {
|
||||||
|
requires java.sql;
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package org.palomafp.aadd.jx;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.jx.model.Coche;
|
||||||
|
|
||||||
|
public class AppPrincipal {
|
||||||
|
|
||||||
|
private static final String URL = "jdbc:mysql://10.100.11.133:3306/concesionario";
|
||||||
|
private static final String USUARIO = "root";
|
||||||
|
private static final String PASSWORD = "1234";
|
||||||
|
|
||||||
|
private static final String SQL_CONSULTAR = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE";
|
||||||
|
private static final String SQL_BORRAR = "DELETE FROM T_COCHE WHERE identificador = ?";
|
||||||
|
private static final String SQL_INSERTAR = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)";
|
||||||
|
private static final String SQL_ACTUALIZAR = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
try (Connection conexion = DriverManager.getConnection(URL, USUARIO, PASSWORD)) {
|
||||||
|
|
||||||
|
List<Coche> listaCoches = consultarCoches(conexion);
|
||||||
|
System.out.println("--- Lista inicial de coches ---");
|
||||||
|
for (Coche c : listaCoches) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
int borrados = borrarCoches(conexion, listaCoches);
|
||||||
|
System.out.println("Coches borrados: " + borrados);
|
||||||
|
|
||||||
|
int insertados = insertarCoches(conexion, listaCoches);
|
||||||
|
System.out.println("Coches insertados: " + insertados);
|
||||||
|
|
||||||
|
int actualizados = actualizarModelo(conexion, 5, "Ibiza Eco");
|
||||||
|
System.out.println("Coches actualizados: " + actualizados);
|
||||||
|
|
||||||
|
System.out.println("--- Lista final de coches ---");
|
||||||
|
for (Coche c : consultarCoches(conexion)) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error con la base de datos: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Coche> consultarCoches(Connection conexion) throws SQLException {
|
||||||
|
List<Coche> listaCoches = new ArrayList<>();
|
||||||
|
|
||||||
|
try (PreparedStatement sentencia = conexion.prepareStatement(SQL_CONSULTAR);
|
||||||
|
ResultSet resultado = sentencia.executeQuery()) {
|
||||||
|
|
||||||
|
while (resultado.next()) {
|
||||||
|
Coche coche = new Coche();
|
||||||
|
coche.setIdentificador(resultado.getInt("identificador"));
|
||||||
|
coche.setMarca(resultado.getString("marca"));
|
||||||
|
coche.setModelo(resultado.getString("modelo"));
|
||||||
|
coche.setCilindrada(resultado.getDouble("cilindrada"));
|
||||||
|
listaCoches.add(coche);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaCoches;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int borrarCoches(Connection conexion, List<Coche> listaCoches) throws SQLException {
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
try (PreparedStatement sentencia = conexion.prepareStatement(SQL_BORRAR)) {
|
||||||
|
for (Coche c : listaCoches) {
|
||||||
|
sentencia.setInt(1, c.getIdentificador());
|
||||||
|
total += sentencia.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int insertarCoches(Connection conexion, List<Coche> listaCoches) throws SQLException {
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
try (PreparedStatement sentencia = conexion.prepareStatement(SQL_INSERTAR)) {
|
||||||
|
for (Coche c : listaCoches) {
|
||||||
|
sentencia.setInt(1, c.getIdentificador());
|
||||||
|
sentencia.setString(2, c.getMarca());
|
||||||
|
sentencia.setString(3, c.getModelo());
|
||||||
|
sentencia.setDouble(4, c.getCilindrada());
|
||||||
|
total += sentencia.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int actualizarModelo(Connection conexion, int identificador, String nuevoModelo)
|
||||||
|
throws SQLException {
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
try (PreparedStatement sentencia = conexion.prepareStatement(SQL_ACTUALIZAR)) {
|
||||||
|
sentencia.setString(1, nuevoModelo);
|
||||||
|
sentencia.setInt(2, identificador);
|
||||||
|
total = sentencia.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.palomafp.aadd.jx.model;
|
||||||
|
|
||||||
|
public class Coche {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String marca;
|
||||||
|
private String modelo;
|
||||||
|
private double cilindrada;
|
||||||
|
|
||||||
|
public Coche() {
|
||||||
|
}
|
||||||
|
|
||||||
|
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=" + identificador + ", Marca=" + marca + ", Modelo=" + modelo
|
||||||
|
+ ", Cilindrada=" + cilindrada + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user