Primer commit
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>24</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
package org.lapaloma.aadd.inm;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.lapaloma.aadd.inm.model.Coche;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* AppPrincipal: Clase que realiza ....
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 28 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class AppPrincipal {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
AppPrincipal app = new AppPrincipal();
|
||||||
|
|
||||||
|
Connection conexion = app.obtenerConexionBBDD();
|
||||||
|
|
||||||
|
// 1. Obtener lista Coches
|
||||||
|
List<Coche> listaCoches = app.obtenerListaCoches(conexion);
|
||||||
|
System.out.println(listaCoches);
|
||||||
|
|
||||||
|
// 2. Borrar Coches
|
||||||
|
app.borrarCoches(conexion);
|
||||||
|
|
||||||
|
// 3. Insertar Coches
|
||||||
|
app.insertarCoches(conexion, listaCoches);
|
||||||
|
|
||||||
|
// 4. Actualizar Coche
|
||||||
|
app.actualizarCoche(conexion, 5, "Ibiza Eco");
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Coche> obtenerListaCoches(Connection conexion) {
|
||||||
|
List<Coche> listaCoches = null;
|
||||||
|
|
||||||
|
String sentenciaSQL = "SELECT * FROM T_COCHE";
|
||||||
|
try (Statement sentencia = conexion.createStatement();
|
||||||
|
ResultSet resultados = sentencia.executeQuery(sentenciaSQL);) {
|
||||||
|
while (resultados.next()) {
|
||||||
|
if (listaCoches == null) {
|
||||||
|
listaCoches = new ArrayList<Coche>();
|
||||||
|
}
|
||||||
|
int identificador = resultados.getInt("identificador");
|
||||||
|
String marca = resultados.getString("marca");
|
||||||
|
String modelo = resultados.getString("modelo");
|
||||||
|
float cilindrada = resultados.getFloat("cilindrada");
|
||||||
|
|
||||||
|
Coche coche = new Coche();
|
||||||
|
coche.setIdentificador(identificador);
|
||||||
|
coche.setMarca(marca);
|
||||||
|
coche.setModelo(modelo);
|
||||||
|
coche.setCilindrada(cilindrada);
|
||||||
|
|
||||||
|
listaCoches.add(coche);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaCoches;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void borrarCoches(Connection conexion) {
|
||||||
|
String sentenciaSQL = "DELETE FROM T_COCHE";
|
||||||
|
|
||||||
|
try (Statement sentencia = conexion.createStatement();) {
|
||||||
|
sentencia.execute(sentenciaSQL);
|
||||||
|
System.out.println(sentenciaSQL);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertarCoches(Connection conexion, List<Coche> listaCoches) {
|
||||||
|
String sentenciaSQL = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (%d, '%s', '%s', %.2f)";
|
||||||
|
try (Statement sentencia = conexion.createStatement();) {
|
||||||
|
for (Coche coche : listaCoches) {
|
||||||
|
String sentenciaSQLRellena = String.format(Locale.US,sentenciaSQL, coche.getIdentificador(), coche.getMarca(),
|
||||||
|
coche.getModelo(), coche.getCilindrada());
|
||||||
|
|
||||||
|
sentencia.execute(sentenciaSQLRellena);
|
||||||
|
System.out.println(sentenciaSQLRellena);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void actualizarCoche(Connection conexion, int idCoche, String modelo) {
|
||||||
|
String sentenciaSQL = "UPDATE T_COCHE SET modelo = '%s' WHERE identificador = %d";
|
||||||
|
|
||||||
|
try (Statement sentencia = conexion.createStatement();) {
|
||||||
|
sentenciaSQL = String.format(sentenciaSQL, modelo, idCoche);
|
||||||
|
|
||||||
|
sentencia.execute(sentenciaSQL);
|
||||||
|
System.out.println(sentenciaSQL);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection obtenerConexionBBDD() {
|
||||||
|
Connection conexion = null;
|
||||||
|
try {
|
||||||
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
|
||||||
|
conexion = DriverManager.getConnection("jdbc:mysql://192.168.1.36:3306/concesionario", "root", "mysql_123");
|
||||||
|
} catch (ClassNotFoundException | SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.lapaloma.aadd.inm.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Coche: Clase que representa la clase para los datos de persistencia de un
|
||||||
|
* coche.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 28 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Coche {
|
||||||
|
private int identificador;
|
||||||
|
private String marca;
|
||||||
|
private String modelo;
|
||||||
|
private float 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 float getCilindrada() {
|
||||||
|
return cilindrada;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCilindrada(float cilindrada) {
|
||||||
|
this.cilindrada = cilindrada;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Coche [identificador=" + identificador + ", marca=" + marca + ", modelo=" + modelo + ", cilindrada="
|
||||||
|
+ cilindrada + "]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user