Primer commit

This commit is contained in:
SASI
2026-10-29 11:48:18 +01:00
commit a7177bcb17
9 changed files with 265 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
bin/
.settings/
.classpath
.project
+27
View File
@@ -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>Dependencia_mysql</groupId>
<artifactId>Dependencia_mysql</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>
+131
View File
@@ -0,0 +1,131 @@
package org.palomafp.aadd.ss;
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 AppPrincipal {
private static final String URL = "jdbc:mysql://192.168.10.189/concesionario";
private static final String USUARIO = "root";
private static final String CLAVE = "root";
public static void main(String[] args) {
try(Connection c = DriverManager.getConnection(URL, USUARIO, CLAVE)) {
System.out.println("Conecxion a MySQL ha establecido");
System.out.println("--- 1. CONSULTA INICIAL ---");
List<Coche> listaCoches = consultarCoches(c);
System.out.println("\n--- 2. BORRANDO COCHES ---");
borrarConLista(c, listaCoches);
System.out.println("\n--- 3. INSERTANDO COCHES ---");
insertarConLista(c, listaCoches);
System.out.println("\n--- 4. ACTUALIZANDO COCHE 5 ---");
actualizarCoche(c, 5, "Ibiza Eco");
System.out.println("\n--- RESULTADO FINAL EN LA BBDD ---");
consultarCoches(c); // Volvemos a leer para ver que el coche 5 ahora es "Ibiza Eco"
}catch(SQLException e){
System.out.println("Error crítico con la Base de Datos: " + e.getMessage());
}
}
private static List<Coche> consultarCoches(Connection c) throws SQLException {
List<Coche> coches = new ArrayList<>();
String sql = "SELECT * 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 = c.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()) {
//resultado!!, mira los títulos de esta fila, busca la columna que tiene el letrero 'identificador' escrito, y saca el número entero (getInt) que hay dentro
Coche coche = new Coche(
resultado.getInt("identificador"),
resultado.getString("marca"),
resultado.getString("modelo"),
resultado.getDouble("cilindrada"));
coches.add(coche);
System.out.println(coche.toString());
}
}
return coches;
}
private static void borrarConLista(Connection c, List<Coche> listaCoches) throws SQLException {
String sql = "DELETE FROM T_COCHE WHERE identificador = ?";
// Usamos PreparedStatement porque tenemos un dato variable (?)
try(PreparedStatement sentencia = c.prepareStatement(sql)){
for(Coche coche:listaCoches) {
sentencia.setInt(1, coche.getIdentificador());
int filas = sentencia.executeUpdate();
System.out.println("Borrado el coche " + coche.getIdentificador() +
"filas afectadas: " + filas );
}
}
}
private static void insertarConLista(Connection c, List<Coche> listaCoches) throws SQLException {
String sql = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)";
try(PreparedStatement sentencia = c.prepareStatement(sql)){
for (Coche coche : listaCoches) {
sentencia.setInt(1, coche.getIdentificador());
sentencia.setString(2, coche.getMarca());
sentencia.setString(3, coche.getModelo());
sentencia.setDouble(4, coche.getCilindrada());
sentencia.executeUpdate();
}
System.out.println("Se ha insertado " + listaCoches.size() + "coches");
}
}
private static void actualizarCoche(Connection c, int identificador, String modeloNuevo) throws SQLException {
String sql = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?";
try(PreparedStatement sentencia = c.prepareStatement(sql)){
sentencia.setString(1, modeloNuevo);
sentencia.setInt(2, identificador);
int filasAfectadas = sentencia.executeUpdate();
if (filasAfectadas > 0) {
System.out.println("El coche con identificador 5 ha sido actualizado a 'Ibiza Eco'.");
}
}
}
}
+64
View File
@@ -0,0 +1,64 @@
package org.palomafp.aadd.ss;
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 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 "Coche [identificador=" + identificador + ", marca=" + marca + ", modelo=" + modelo + ", cilindrada=" + cilindrada
+ "]";
}
}
+5
View File
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Java-Version: 25
Build-Jdk-Spec: 25
Created-By: Maven Integration for Eclipse
@@ -0,0 +1,7 @@
#Generated by Maven Integration for Eclipse
#Wed Oct 28 23:24:54 CET 2026
artifactId=Dependencia_mysql
groupId=Dependencia_mysql
m2e.projectLocation=C\:\\Users\\sajed\\Desktop\\2DAM\\AADD\\eclipse-workspace\\aadd_conect01
m2e.projectName=aadd_conect01
version=0.0.1-SNAPSHOT
@@ -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>Dependencia_mysql</groupId>
<artifactId>Dependencia_mysql</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>
Binary file not shown.