Actvidad 2.3
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-25">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="Config"/>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,2 @@
|
||||
/bin/
|
||||
/target/
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Conectores_03</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1793377072059</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.apt.aptEnabled=false
|
||||
@@ -0,0 +1,13 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=25
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=25
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.processAnnotations=disabled
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=25
|
||||
@@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
@@ -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://10.100.11.135:3306/
|
||||
bbdd.nombre=Mapa_Mundi
|
||||
bbdd.usuario=Alejandro
|
||||
bbdd.clave=Combarro1!
|
||||
@@ -0,0 +1,30 @@
|
||||
<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>
|
||||
<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>
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.palomafp.aadd.aez;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.exception.MapaMundiException;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
import org.palomafp.aadd.aez.service.impl.MapaMundiServiceImpl;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MapaMundiServiceImpl servicio = new MapaMundiServiceImpl();
|
||||
|
||||
try {
|
||||
System.out.println("=== 1. Consultar lista de continentes ===");
|
||||
List<Continente> continentes = servicio.listarContinentes();
|
||||
continentes.forEach(System.out::println);
|
||||
|
||||
System.out.println("\n=== 2. Consultar el continente con código '04' ===");
|
||||
Continente europa = servicio.buscarContinentePorCodigo("04");
|
||||
System.out.println("Obtenido: " + europa);
|
||||
|
||||
if (europa != null) {
|
||||
System.out.println("\n=== 3. Borrar el continente '04' y sus países (Transacción) ===");
|
||||
servicio.eliminarContinenteYsusPaises("04");
|
||||
|
||||
System.out.println("\n=== 4. Insertar el continente a partir del objeto del punto 2 ===");
|
||||
servicio.insertarContinente(europa);
|
||||
System.out.println("Continente '04' insertado nuevamente.");
|
||||
|
||||
System.out.println("\n=== 5. Actualizar el continente '04' a 'Paneuropa' ===");
|
||||
europa.setNombre("Paneuropa");
|
||||
servicio.actualizarContinente(europa);
|
||||
|
||||
Continente europaActualizada = servicio.buscarContinentePorCodigo("04");
|
||||
System.out.println("Resultado tras actualizar: " + europaActualizada);
|
||||
}
|
||||
|
||||
} catch (MapaMundiException e) {
|
||||
System.err.println("Error de aplicación: " + e.getMessage());
|
||||
if (e.getCause() != null) {
|
||||
System.err.println("Causa original: " + e.getCause().getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.palomafp.aadd.aez.config;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
private static final Properties properties = new Properties();
|
||||
private static final String RUTA_CONFIG = "config/application.properties";
|
||||
|
||||
static {
|
||||
try (InputStream input = new FileInputStream(RUTA_CONFIG)) {
|
||||
properties.load(input);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al cargar la configuración: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getProperty(String clave) {
|
||||
return properties.getProperty(clave);
|
||||
}
|
||||
|
||||
public static String getUrlConexion() {
|
||||
return getProperty("sgdb.conexion.url") + getProperty("bbdd.nombre");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.palomafp.aadd.aez.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.exception.MapaMundiException;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
List<Continente> obtenerTodos() throws MapaMundiException;
|
||||
|
||||
Continente obtenerPorCodigo(String codigo) throws MapaMundiException;
|
||||
|
||||
boolean insertar(Continente continente) throws MapaMundiException;
|
||||
|
||||
boolean actualizar(Continente continente) throws MapaMundiException;
|
||||
|
||||
boolean eliminar(String codigo, Connection conn) throws MapaMundiException;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.palomafp.aadd.aez.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.exception.MapaMundiException;
|
||||
import org.palomafp.aadd.aez.model.Pais;
|
||||
|
||||
import com.sun.jdi.connect.spi.Connection;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
List<Pais> obtenerTodos() throws MapaMundiException;
|
||||
|
||||
List<Pais> obtenerPorContinente(String codContinente) throws MapaMundiException;
|
||||
|
||||
boolean eliminarPorContinente(String codContinente, java.sql.Connection conn) throws MapaMundiException;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package org.palomafp.aadd.aez.dao.impl;
|
||||
|
||||
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.aez.config.GestorConfiguracion;
|
||||
import org.palomafp.aadd.aez.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.aez.exception.MapaMundiException;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
|
||||
public class ContinenteDaoJDBC implements IContinenteDAO {
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerTodos() throws MapaMundiException {
|
||||
List<Continente> lista = new ArrayList<>();
|
||||
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE";
|
||||
|
||||
try (Connection conn = obtenerConexion();
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
lista.add(new Continente(rs.getString("codigo"), rs.getString("nombre_continente")));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar la lista de continentes", e);
|
||||
}
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerPorCodigo(String codigo) throws MapaMundiException {
|
||||
|
||||
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE WHERE codigo = ?";
|
||||
Continente continente = null;
|
||||
|
||||
try (Connection conn = obtenerConexion(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, codigo);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
continente = new Continente(rs.getString("codigo"), rs.getString("nombre_continente"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar la lista de continentes", e);
|
||||
}
|
||||
return continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertar(Continente continente) throws MapaMundiException {
|
||||
String sql = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)";
|
||||
|
||||
try (Connection conn = obtenerConexion(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setString(1, continente.getCodigo());
|
||||
ps.setString(2, continente.getNombre());
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al insertar el continente: " + continente.getCodigo(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizar(Continente continente) throws MapaMundiException {
|
||||
String sql = "UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?";
|
||||
|
||||
try (Connection conn = obtenerConexion(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setString(1, continente.getNombre());
|
||||
ps.setString(2, continente.getCodigo());
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al actualizar el continente: " + continente.getCodigo(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eliminar(String codigo, Connection conn) throws MapaMundiException {
|
||||
String sql = "DELETE FROM T_CONTINENTE WHERE codigo = ?";
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, codigo);
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al eliminar el continente: " + codigo, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection obtenerConexion() throws SQLException {
|
||||
String url = GestorConfiguracion.getUrlConexion();
|
||||
String user = GestorConfiguracion.getProperty("bbdd.usuario");
|
||||
String pass = GestorConfiguracion.getProperty("bbdd.clave");
|
||||
return DriverManager.getConnection(url, user, pass);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.palomafp.aadd.aez.dao.impl;
|
||||
|
||||
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.aez.config.GestorConfiguracion;
|
||||
import org.palomafp.aadd.aez.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.aez.exception.MapaMundiException;
|
||||
import org.palomafp.aadd.aez.model.Pais;
|
||||
|
||||
public class PaisDaoJDBC implements IPaisDAO {
|
||||
|
||||
private Connection obtenerConexion() throws SQLException {
|
||||
String url = GestorConfiguracion.getUrlConexion();
|
||||
String user = GestorConfiguracion.getProperty("bbdd.usuario");
|
||||
String pass = GestorConfiguracion.getProperty("bbdd.clave");
|
||||
return DriverManager.getConnection(url, user, pass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerTodos() throws MapaMundiException {
|
||||
List<Pais> lista = new ArrayList<>();
|
||||
String sql = "SELECT identificador, nombre_pais, capital, cod_continente FROM T_PAIS";
|
||||
|
||||
try (Connection conn = obtenerConexion();
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
|
||||
while (rs.next()) {
|
||||
lista.add(new Pais(rs.getInt("identificador"), rs.getString("nombre_pais"), rs.getString("capital"),
|
||||
rs.getString("cod_continente")));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar todos los países", e);
|
||||
}
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPorContinente(String codContinente) throws MapaMundiException {
|
||||
List<Pais> lista = new ArrayList<>();
|
||||
String sql = "SELECT identificador, nombre_pais, capital, cod_continente FROM T_PAIS WHERE cod_continente = ?";
|
||||
|
||||
try (Connection conn = obtenerConexion(); PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setString(1, codContinente);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
lista.add(new Pais(rs.getInt("identificador"), rs.getString("nombre_pais"), rs.getString("capital"),
|
||||
rs.getString("cod_continente")));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar los países del continente: " + codContinente, e);
|
||||
}
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eliminarPorContinente(String codContinente, Connection conn) throws MapaMundiException {
|
||||
String sql = "DELETE FROM T_PAIS WHERE cod_continente = ?";
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, codContinente);
|
||||
return ps.executeUpdate() >= 0;
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al eliminar los países del continente: " + codContinente, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.aadd.aez.exception;
|
||||
|
||||
public class MapaMundiException extends Exception {
|
||||
// Capturar las anomalías producidas durante el acceso a la base de datos
|
||||
|
||||
public MapaMundiException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
public MapaMundiException(String mensaje, Throwable causa) {
|
||||
super(mensaje, causa);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.palomafp.aadd.aez.model;
|
||||
|
||||
public class Continente {
|
||||
private String nombre;
|
||||
private String codigo;
|
||||
|
||||
public Continente(String nombre, String codigo) {
|
||||
this.nombre = nombre;
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public String getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [nombre=" + nombre + ", codigo=" + codigo + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.palomafp.aadd.aez.model;
|
||||
|
||||
public class Pais {
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
private String codContinente;
|
||||
|
||||
public Pais() {
|
||||
}
|
||||
|
||||
public Pais(int identificador, String nombre, String capital, String codContinente) {
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.codContinente = codContinente;
|
||||
}
|
||||
|
||||
public int getIdentificador() {
|
||||
return identificador;
|
||||
}
|
||||
|
||||
public void setIdentificador(int identificador) {
|
||||
this.identificador = identificador;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public String getCapital() {
|
||||
return capital;
|
||||
}
|
||||
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
public String getCodContinente() {
|
||||
return codContinente;
|
||||
}
|
||||
|
||||
public void setCodContinente(String codContinente) {
|
||||
this.codContinente = codContinente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital
|
||||
+ ", codContinente=" + codContinente + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.palomafp.aadd.aez.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.exception.MapaMundiException;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
|
||||
public interface IMapaMundiService {
|
||||
abstract List<Continente> listarContinentes() throws MapaMundiException;
|
||||
|
||||
abstract Continente buscarContinentePorCodigo(String codigo) throws MapaMundiException;
|
||||
|
||||
abstract void eliminarContinenteYsusPaises(String codContinente) throws MapaMundiException;
|
||||
|
||||
abstract boolean insertarContinente(Continente continente) throws MapaMundiException;
|
||||
|
||||
abstract boolean actualizarContinente(Continente continente) throws MapaMundiException;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.palomafp.aadd.aez.service.impl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.config.GestorConfiguracion;
|
||||
import org.palomafp.aadd.aez.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.aez.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.aez.dao.impl.ContinenteDaoJDBC;
|
||||
import org.palomafp.aadd.aez.dao.impl.PaisDaoJDBC;
|
||||
import org.palomafp.aadd.aez.exception.MapaMundiException;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
import org.palomafp.aadd.aez.service.IMapaMundiService;
|
||||
|
||||
public class MapaMundiServiceImpl implements IMapaMundiService {
|
||||
|
||||
private final IContinenteDAO continenteDAO;
|
||||
private final IPaisDAO paisDAO;
|
||||
|
||||
public MapaMundiServiceImpl() {
|
||||
this.continenteDAO = new ContinenteDaoJDBC();
|
||||
this.paisDAO = new PaisDaoJDBC();
|
||||
}
|
||||
|
||||
private Connection obtenerConexion() throws SQLException {
|
||||
String url = GestorConfiguracion.getUrlConexion();
|
||||
String user = GestorConfiguracion.getProperty("bbdd.usuario");
|
||||
String pass = GestorConfiguracion.getProperty("bbdd.clave");
|
||||
return DriverManager.getConnection(url, user, pass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> listarContinentes() throws MapaMundiException {
|
||||
return continenteDAO.obtenerTodos();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente buscarContinentePorCodigo(String codigo) throws MapaMundiException {
|
||||
return continenteDAO.obtenerPorCodigo(codigo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eliminarContinenteYsusPaises(String codContinente) throws MapaMundiException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = obtenerConexion();
|
||||
conn.setAutoCommit(false); // Desactivar autoCommit para iniciar la transacción
|
||||
|
||||
// 1. Eliminar países del continente
|
||||
paisDAO.eliminarPorContinente(codContinente, conn);
|
||||
|
||||
// 2. Eliminar el continente
|
||||
continenteDAO.eliminar(codContinente, conn);
|
||||
|
||||
conn.commit(); // Confirmar cambios si todo sale bien
|
||||
System.out.println("Transacción exitosa: Continente " + codContinente + " y sus países eliminados.");
|
||||
} catch (Exception e) {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.rollback(); // Deshacer cambios en caso de fallo
|
||||
System.err.println("Rollback ejecutado.");
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error durante rollback: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
throw new MapaMundiException("Fallo en la transacción al eliminar el continente " + codContinente, e);
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al cerrar conexión: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertarContinente(Continente continente) throws MapaMundiException {
|
||||
return continenteDAO.insertar(continente);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizarContinente(Continente continente) throws MapaMundiException {
|
||||
return continenteDAO.actualizar(continente);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user