commit e52fa81784b263f3203e22bb34be26200fefbfdb Author: juanperoh11-lgtm. Date: Fri Oct 30 17:18:50 2026 +0100 Actvidad 2.3 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..0f17f76 --- /dev/null +++ b/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09e3bc9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/target/ diff --git a/.project b/.project new file mode 100644 index 0000000..ad7647e --- /dev/null +++ b/.project @@ -0,0 +1,34 @@ + + + Conectores_03 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + + + 1793377072059 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/.settings/org.eclipse.jdt.apt.core.prefs b/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..d59c201 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/Config/application.properties b/Config/application.properties new file mode 100644 index 0000000..4b36ecc --- /dev/null +++ b/Config/application.properties @@ -0,0 +1,6 @@ +# Informaci髇 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! \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9caabe4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + Conectores_03 + Conectores_03 + 0.0.1-SNAPSHOT + + + + com.mysql + mysql-connector-j + 26.10.0 + compile + + + + src + + + maven-compiler-plugin + 3.13.0 + + 25 + + + + + \ No newline at end of file diff --git a/src/org/palomafp/aadd/aez/Main.java b/src/org/palomafp/aadd/aez/Main.java new file mode 100644 index 0000000..1188bb4 --- /dev/null +++ b/src/org/palomafp/aadd/aez/Main.java @@ -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 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()); + } + } + } +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/aez/config/GestorConfiguracion.java b/src/org/palomafp/aadd/aez/config/GestorConfiguracion.java new file mode 100644 index 0000000..e6531f7 --- /dev/null +++ b/src/org/palomafp/aadd/aez/config/GestorConfiguracion.java @@ -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"); + } +} diff --git a/src/org/palomafp/aadd/aez/dao/IContinenteDAO.java b/src/org/palomafp/aadd/aez/dao/IContinenteDAO.java new file mode 100644 index 0000000..1c0c8ab --- /dev/null +++ b/src/org/palomafp/aadd/aez/dao/IContinenteDAO.java @@ -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 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; +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/aez/dao/IPaisDAO.java b/src/org/palomafp/aadd/aez/dao/IPaisDAO.java new file mode 100644 index 0000000..cacea29 --- /dev/null +++ b/src/org/palomafp/aadd/aez/dao/IPaisDAO.java @@ -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 obtenerTodos() throws MapaMundiException; + + List obtenerPorContinente(String codContinente) throws MapaMundiException; + + boolean eliminarPorContinente(String codContinente, java.sql.Connection conn) throws MapaMundiException; +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/aez/dao/impl/ContinenteDaoJDBC.java b/src/org/palomafp/aadd/aez/dao/impl/ContinenteDaoJDBC.java new file mode 100644 index 0000000..84e2a61 --- /dev/null +++ b/src/org/palomafp/aadd/aez/dao/impl/ContinenteDaoJDBC.java @@ -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 obtenerTodos() throws MapaMundiException { + List 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); + } + +} diff --git a/src/org/palomafp/aadd/aez/dao/impl/PaisDaoJDBC.java b/src/org/palomafp/aadd/aez/dao/impl/PaisDaoJDBC.java new file mode 100644 index 0000000..fcdfe26 --- /dev/null +++ b/src/org/palomafp/aadd/aez/dao/impl/PaisDaoJDBC.java @@ -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 obtenerTodos() throws MapaMundiException { + List 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 obtenerPorContinente(String codContinente) throws MapaMundiException { + List 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); + } + } +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/aez/exception/MapaMundiException.java b/src/org/palomafp/aadd/aez/exception/MapaMundiException.java new file mode 100644 index 0000000..b29b7c2 --- /dev/null +++ b/src/org/palomafp/aadd/aez/exception/MapaMundiException.java @@ -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); + } +} diff --git a/src/org/palomafp/aadd/aez/model/Continente.java b/src/org/palomafp/aadd/aez/model/Continente.java new file mode 100644 index 0000000..7fe39e0 --- /dev/null +++ b/src/org/palomafp/aadd/aez/model/Continente.java @@ -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 + "]"; + } + + } diff --git a/src/org/palomafp/aadd/aez/model/Pais.java b/src/org/palomafp/aadd/aez/model/Pais.java new file mode 100644 index 0000000..a04bd6c --- /dev/null +++ b/src/org/palomafp/aadd/aez/model/Pais.java @@ -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 + "]"; + } +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/aez/service/IMapaMundiService.java b/src/org/palomafp/aadd/aez/service/IMapaMundiService.java new file mode 100644 index 0000000..9ce9b04 --- /dev/null +++ b/src/org/palomafp/aadd/aez/service/IMapaMundiService.java @@ -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 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; +} diff --git a/src/org/palomafp/aadd/aez/service/impl/MapaMundiServiceImpl.java b/src/org/palomafp/aadd/aez/service/impl/MapaMundiServiceImpl.java new file mode 100644 index 0000000..97ad473 --- /dev/null +++ b/src/org/palomafp/aadd/aez/service/impl/MapaMundiServiceImpl.java @@ -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 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); + } +} \ No newline at end of file