Actividad2.3 Manipulacion datos Continente-DAO
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
# Ignorar la carpeta de compilados
|
||||||
|
bin/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Ignorar archivos de configuración de Eclipse
|
||||||
|
.settings/
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<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_03</groupId>
|
||||||
|
<artifactId>Conectores_03</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<!-- aquí metemos las dependencias que tenga el proyecto -->
|
||||||
|
<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>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src</directory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
<configuration>
|
||||||
|
<release>26</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
# Información relativa a la base de datos
|
||||||
|
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
||||||
|
sgdb.conexion.url=jdbc:mysql://192.168.1.146:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=root
|
||||||
|
bbdd.clave=root
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.palomafp.aadd.ljl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
import org.palomafp.aadd.ljl.service.ContinenteService;
|
||||||
|
|
||||||
|
public class AppPrincipal {
|
||||||
|
|
||||||
|
private static final String COD_CONTINENTE = "04";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// al haber ido envolviendo las excepciones SQLException en MapapMundi ahora solo nos pueda dar MapaMundiException
|
||||||
|
try {
|
||||||
|
|
||||||
|
AppPrincipal app = new AppPrincipal();
|
||||||
|
app.realizarTratamientoContinentes();
|
||||||
|
|
||||||
|
} catch (MapaMundiException e) {
|
||||||
|
System.err.println("Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void realizarTratamientoContinentes() throws MapaMundiException {
|
||||||
|
|
||||||
|
ContinenteService service = new ContinenteService();
|
||||||
|
|
||||||
|
// 1. Listar continentes
|
||||||
|
List<Continente> continentes = service.obtenerListaContinentes();
|
||||||
|
for (Continente c: continentes) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
System.out.println("---------- Fin obtenerListaContinentes ----------");
|
||||||
|
|
||||||
|
// 2. Consultar el continente 04
|
||||||
|
Continente continente = service.obtenerContinentePorCodigo(COD_CONTINENTE);
|
||||||
|
System.out.println(continente);
|
||||||
|
System.out.println("---------- Fin obtenerContinentePorCodigo ----------");
|
||||||
|
|
||||||
|
if (continente != null) {
|
||||||
|
// 3. Borrar países del continente y el continente (una transacción)
|
||||||
|
service.borrarContinente(COD_CONTINENTE);
|
||||||
|
System.out.println("---------- Fin borrarContinente ----------");
|
||||||
|
|
||||||
|
// 4. Insertar el continente obtenido en el punto 2
|
||||||
|
service.insertarContinente(continente);
|
||||||
|
System.out.println("---------- Fin insertarContinente ----------");
|
||||||
|
|
||||||
|
// 5. Actualizar el nombre a "Paneuropa"
|
||||||
|
continente.setNombre("Paneuropa");
|
||||||
|
service.actualizarContinente(continente);
|
||||||
|
System.out.println("---------- Fin actualizarContinente ----------");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
List<Continente> listarContinentes() throws MapaMundiException;
|
||||||
|
|
||||||
|
Continente buscarContinentePorCodigo(String codigo) throws MapaMundiException;
|
||||||
|
|
||||||
|
int borrarContinente(String codigo) throws MapaMundiException;
|
||||||
|
|
||||||
|
int insertarContinente(Continente continente) throws MapaMundiException;
|
||||||
|
|
||||||
|
int actualizarContinente(Continente continente) throws MapaMundiException;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
// cambiamos los SQLException por MapaMundiException
|
||||||
|
// (ambas son checked, pero la MapaMundiException es personalizada, por si hay mas bases de datos)
|
||||||
|
|
||||||
|
List<Pais> listarPaises() throws MapaMundiException;
|
||||||
|
|
||||||
|
Pais buscarPaisPorId(int identificador) throws MapaMundiException;
|
||||||
|
|
||||||
|
int borrarPais(int identificador) throws MapaMundiException;
|
||||||
|
|
||||||
|
int borrarPaisesPorContinente(String codContinente) throws MapaMundiException;
|
||||||
|
|
||||||
|
int insertarPais(Pais pais) throws MapaMundiException;
|
||||||
|
|
||||||
|
int actualizarPais(Pais pais) throws MapaMundiException;
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao.impl;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoJDBC implements IContinenteDAO {
|
||||||
|
|
||||||
|
private static final String SELECT_BASE = "SELECT codigo, nombre_continente FROM T_CONTINENTE";
|
||||||
|
|
||||||
|
private final Connection conexionDB;
|
||||||
|
|
||||||
|
public ContinenteDaoJDBC(Connection conexionDB) {
|
||||||
|
this.conexionDB = conexionDB;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> listarContinentes() throws MapaMundiException {
|
||||||
|
|
||||||
|
List<Continente> continentes = new ArrayList<>();
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(SELECT_BASE);
|
||||||
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
continentes.add(mapear(rs));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al listar los continentes", e);
|
||||||
|
}
|
||||||
|
return continentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente buscarContinentePorCodigo(String codigo) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = SELECT_BASE + " WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, codigo);
|
||||||
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
return mapear(rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al buscar el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int borrarContinente(String codigo) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = "DELETE FROM T_CONTINENTE WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, codigo);
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int insertarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, continente.getCodigo());
|
||||||
|
ps.setString(2, continente.getNombre());
|
||||||
|
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int actualizarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = "UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, continente.getNombre());
|
||||||
|
ps.setString(2, continente.getCodigo());
|
||||||
|
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------ Método auxiliar ------------
|
||||||
|
private Continente mapear(ResultSet rs) throws SQLException {
|
||||||
|
return new Continente(rs.getString("codigo"), rs.getString("nombre_continente"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao.impl;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoJDBC implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String SELECT_BASE = "SELECT cod_continente, identificador, nombre_pais, capital FROM T_PAIS";
|
||||||
|
|
||||||
|
private final Connection conexionDB;
|
||||||
|
|
||||||
|
public PaisDaoJDBC(Connection conexionDB) {
|
||||||
|
this.conexionDB = conexionDB;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> listarPaises() throws MapaMundiException {
|
||||||
|
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(SELECT_BASE);
|
||||||
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
paises.add(mapear(rs));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al listar los países", e);
|
||||||
|
}
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais buscarPaisPorId(int identificador) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = SELECT_BASE + " WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
ps.setInt(1, identificador);
|
||||||
|
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return mapear(rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al buscar el país " + identificador, e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int borrarPais(int identificador) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = "DELETE FROM T_PAIS WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setInt(1, identificador);
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar el país " + identificador, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int borrarPaisesPorContinente(String codContinente) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = "DELETE FROM T_PAIS WHERE cod_continente = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, codContinente);
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar los países del continente " + codContinente, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int insertarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES (?, ?, ?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, pais.getCodContinente());
|
||||||
|
ps.setInt(2, pais.getIdentificador());
|
||||||
|
ps.setString(3, pais.getNombrePais());
|
||||||
|
ps.setString(4, pais.getCapital());
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar el país " + pais.getIdentificador(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int actualizarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
String sql = "UPDATE T_PAIS SET cod_continente = ?, nombre_pais = ?, capital = ? WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexionDB.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
ps.setString(1, pais.getCodContinente());
|
||||||
|
ps.setString(2, pais.getNombrePais());
|
||||||
|
ps.setString(3, pais.getCapital());
|
||||||
|
ps.setInt(4, pais.getIdentificador());
|
||||||
|
return ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar el país " + pais.getIdentificador(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------- MÉTODO AUXILIAR -------------------
|
||||||
|
private Pais mapear(ResultSet rs) throws SQLException {
|
||||||
|
return new Pais(rs.getString("cod_continente"), rs.getInt("identificador"), rs.getString("nombre_pais"),
|
||||||
|
rs.getString("capital"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.ljl.excepciones;
|
||||||
|
|
||||||
|
public class MapaMundiException extends Exception {
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
package org.palomafp.aadd.ljl.gestores;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
|
||||||
|
public class GestorConexionBBDD {
|
||||||
|
|
||||||
|
// Constructor privado: de esta manera conseguimos que esta clase no se puede instanciar.
|
||||||
|
private GestorConexionBBDD() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection obtenerConexion() throws MapaMundiException {
|
||||||
|
|
||||||
|
Connection conexion = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
String urlConexionCompleta = GestorConfiguracion.getConexionURL() + GestorConfiguracion.getNombreBBDD();
|
||||||
|
String usuarioBBDD=GestorConfiguracion.getUsuarioBBDD();
|
||||||
|
String claveBBDD=GestorConfiguracion.getClaveBBDD();
|
||||||
|
|
||||||
|
// crear una conexión con la base de datos
|
||||||
|
conexion = DriverManager.getConnection(urlConexionCompleta, usuarioBBDD, claveBBDD);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("No se ha podido obtener una conexión con el SGBD", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return conexion;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.palomafp.aadd.ljl.gestores;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
|
||||||
|
public class GestorConfiguracion {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||||
|
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||||
|
private static boolean cargado = false;
|
||||||
|
|
||||||
|
// Constructor privado: de esta manera conseguimos que esta clase no se pueda instanciar.
|
||||||
|
private GestorConfiguracion() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// este bloque se carga solo una vez cuando se acceda a las properties de algun elemento del application.properties
|
||||||
|
private static void cargarPropiedades() throws MapaMundiException {
|
||||||
|
|
||||||
|
if (!cargado) {
|
||||||
|
try (InputStream is = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)) {
|
||||||
|
|
||||||
|
if (is == null) {
|
||||||
|
throw new MapaMundiException("No se ha encontrado el fichero de configuración: " + RUTA_FICHERO_CONFIGURACION);
|
||||||
|
}
|
||||||
|
|
||||||
|
PROPIEDADES_CONFIGURACION.load(is);
|
||||||
|
cargado = true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new MapaMundiException("No se ha podido cargar el fichero de configuración", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getConexionURL() throws MapaMundiException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("sgdb.conexion.url");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getNombreBBDD() throws MapaMundiException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("bbdd.nombre");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUsuarioBBDD() throws MapaMundiException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("bbdd.usuario");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getClaveBBDD() throws MapaMundiException {
|
||||||
|
cargarPropiedades();
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("bbdd.clave");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.palomafp.aadd.ljl.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
// Constructores
|
||||||
|
public Continente() {
|
||||||
|
|
||||||
|
}
|
||||||
|
public Continente(String codigo, String nombre) {
|
||||||
|
super();
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Getters y setters
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generamos el método toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.palomafp.aadd.ljl.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private String codContinente;
|
||||||
|
private int identificador;
|
||||||
|
private String nombrePais;
|
||||||
|
private String capital;
|
||||||
|
|
||||||
|
// constructores
|
||||||
|
public Pais(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais(String codContinente, int identificador, String nombrePais, String capital) {
|
||||||
|
super();
|
||||||
|
this.codContinente = codContinente;
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombrePais = nombrePais;
|
||||||
|
this.capital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
// getters y setters
|
||||||
|
public String getCodContinente() {
|
||||||
|
return codContinente;
|
||||||
|
}
|
||||||
|
public int getIdentificador() {
|
||||||
|
return identificador;
|
||||||
|
}
|
||||||
|
public String getNombrePais() {
|
||||||
|
return nombrePais;
|
||||||
|
}
|
||||||
|
public String getCapital() {
|
||||||
|
return capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodContinente(String codContinente) {
|
||||||
|
this.codContinente = codContinente;
|
||||||
|
}
|
||||||
|
public void setIdentificador(int identificador) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
}
|
||||||
|
public void setNombrePais(String nombrePais) {
|
||||||
|
this.nombrePais = nombrePais;
|
||||||
|
}
|
||||||
|
public void setCapital(String capital) {
|
||||||
|
this.capital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
// sobrescribimos el método toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Pais [codContinente=" + codContinente + ", identificador=" + identificador + ", nombrePais="
|
||||||
|
+ nombrePais + ", capital=" + capital + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
package org.palomafp.aadd.ljl.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.ContinenteDaoJDBC;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.PaisDaoJDBC;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.ljl.gestores.GestorConexionBBDD;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteService {
|
||||||
|
|
||||||
|
public List<Continente> obtenerListaContinentes() throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
return new ContinenteDaoJDBC(conexion).listarContinentes();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente obtenerContinentePorCodigo(String codigo) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
return new ContinenteDaoJDBC(conexion).buscarContinentePorCodigo(codigo);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Borra los países del continente y después el continente, en una misma transacción.
|
||||||
|
* De normal las interacciones con bbdd hacen un commit
|
||||||
|
* si ponemos conexion.setAutoCommit(false) tenemos que hacer el commit manualmente con conexion.commit()
|
||||||
|
* y en caso de que falle -> rollback(conexion) */
|
||||||
|
public void borrarContinente(String codigo) throws MapaMundiException {
|
||||||
|
|
||||||
|
Connection conexion = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conexion = GestorConexionBBDD.obtenerConexion();
|
||||||
|
conexion.setAutoCommit(false);
|
||||||
|
|
||||||
|
IPaisDAO paisDAO = new PaisDaoJDBC(conexion);
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoJDBC(conexion);
|
||||||
|
|
||||||
|
// 1º parte de la transacción (si no estuviese el setAutoCommit(false) se irian eliminando y commiteando)
|
||||||
|
paisDAO.borrarPaisesPorContinente(codigo);
|
||||||
|
|
||||||
|
// 2º parte de la transacción (si diese error no se eliminan los paises porque no hehmos hecho el commit todavia)
|
||||||
|
int borradoContinente = continenteDAO.borrarContinente(codigo);
|
||||||
|
|
||||||
|
if (borradoContinente == 0) {
|
||||||
|
throw new MapaMundiException("No existe el continente " + codigo);
|
||||||
|
}
|
||||||
|
|
||||||
|
conexion.commit();
|
||||||
|
System.out.println("El continente y los paises se han borrado correctamente");
|
||||||
|
|
||||||
|
|
||||||
|
} catch (MapaMundiException e) {
|
||||||
|
rollback(conexion);
|
||||||
|
throw e;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
// en este catch solo se entra si da un error el conexion.setAutoCommit(false); y conexion.commit();
|
||||||
|
// el resto de posibles errores ya estan "envueltos" por el MapaMundiException (aunque fuesen un SQLException originalmente)
|
||||||
|
rollback(conexion);
|
||||||
|
throw new MapaMundiException("Error en la transacción de borrado del continente " + codigo, e);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
// siempre debemos cerrar la conexión
|
||||||
|
cerrar(conexion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
int codigoContinenteInsertado = new ContinenteDaoJDBC(conexion).insertarContinente(continente);
|
||||||
|
|
||||||
|
if (codigoContinenteInsertado == 0) {
|
||||||
|
System.out.println("No se ha insertado correctamente el continente");
|
||||||
|
}else {
|
||||||
|
System.out.println("Continente insertado correctamente");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
int codigoContinenteActualizado = new ContinenteDaoJDBC(conexion).actualizarContinente(continente);
|
||||||
|
|
||||||
|
if (codigoContinenteActualizado == 0) {
|
||||||
|
throw new MapaMundiException("No existe el continente " + continente.getCodigo());
|
||||||
|
}else {
|
||||||
|
System.out.println("Continente actualizado correctamente");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------- métodos auxiliares que utiliza el borrado para no llenarlo de try-catch ----------
|
||||||
|
private void rollback(Connection conexion) {
|
||||||
|
if (conexion != null) {
|
||||||
|
try {
|
||||||
|
conexion.rollback();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cerrar(Connection conexion) {
|
||||||
|
if (conexion != null) {
|
||||||
|
try {
|
||||||
|
conexion.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package org.palomafp.aadd.ljl.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.PaisDaoJDBC;
|
||||||
|
import org.palomafp.aadd.ljl.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.ljl.gestores.GestorConexionBBDD;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public class PaisService {
|
||||||
|
|
||||||
|
public List<Pais> obtenerListaPaises() throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
return new PaisDaoJDBC(conexion).listarPaises();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais obtenerPaisPorID(int idPais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
return new PaisDaoJDBC(conexion).buscarPaisPorId(idPais);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrarPais(int idPais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
IPaisDAO dao = new PaisDaoJDBC(conexion);
|
||||||
|
|
||||||
|
if (dao.borrarPais(idPais) == 0) {
|
||||||
|
throw new MapaMundiException("No existe el país " + idPais);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
new PaisDaoJDBC(conexion).insertarPais(pais);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
if (new PaisDaoJDBC(conexion).actualizarPais(pais) == 0) {
|
||||||
|
throw new MapaMundiException("No existe el país " + pais.getIdentificador());
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al cerrar la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Java-Version: 26
|
||||||
|
Build-Jdk-Spec: 25
|
||||||
|
Created-By: Maven Integration for Eclipse
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#Generated by Maven Integration for Eclipse
|
||||||
|
#Mon Nov 02 17:44:08 CET 2026
|
||||||
|
artifactId=Conectores_03
|
||||||
|
groupId=Conectores_03
|
||||||
|
m2e.projectLocation=C\:\\Users\\lauuj\\Desktop\\DAM\\Segundo curso\\Acceso a datos\\workspaceAD\\Conectores_03
|
||||||
|
m2e.projectName=Conectores_03
|
||||||
|
version=0.0.1-SNAPSHOT
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<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_03</groupId>
|
||||||
|
<artifactId>Conectores_03</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<!-- aquí metemos las dependencias que tenga el proyecto -->
|
||||||
|
<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>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src</directory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
<configuration>
|
||||||
|
<release>26</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
# Información relativa a la base de datos
|
||||||
|
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
||||||
|
sgdb.conexion.url=jdbc:mysql://192.168.1.146:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=root
|
||||||
|
bbdd.clave=root
|
||||||
Reference in New Issue
Block a user