Primer commit

This commit is contained in:
SASI
2026-11-01 11:26:14 +01:00
commit 49e2c428aa
35 changed files with 801 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
<?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="output" path="target/classes"/>
</classpath>
+5
View File
@@ -0,0 +1,5 @@
bin/
.settings/
.classpath
.project
target/
+23
View File
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>aadd_conect03</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>
</projectDescription>
+12
View File
@@ -0,0 +1,12 @@
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.release=enabled
org.eclipse.jdt.core.compiler.source=25
+4
View File
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
+4
View File
@@ -0,0 +1,4 @@
sgdb.conexion.url=jdbc:mysql://192.168.1.134:3306/
bbdd.nombre=Mapa_Mundi
bbdd.usuario=root
bbdd.clave=root
+30
View File
@@ -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>aadd_conect03</groupId>
<artifactId>aadd_conect03</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,56 @@
package org.palomafp.aadd.ss;
import java.util.List;
import org.palomafp.aadd.ss.excepciones.MapaMundiException;
import org.palomafp.aadd.ss.model.Continente;
import org.palomafp.aadd.ss.service.ContinenteService;
public class AppPrincipal {
public static void main(String[] args) {
ContinenteService cs = new ContinenteService();
try {
System.out.println("--- 1. CONSULTA DE CONTINENTES ---");
List<Continente> lista = cs.obtenerListaContinentes();
for(Continente c : lista) {
System.out.println(c);
}
System.out.println("\n--- 2. CONSULTAR CONTINENTE '04' ---");
Continente c4 = cs.obtenerContinente("04");
System.out.println("Continente obtenido: " + c4);
if(c4 != null) {
System.out.println("\n--- 3. BORRAR CONTINENTE '04' (TRANSACCIÓN) ---");
cs.borrarContinenteyPaises("04");
System.out.println("Cotinente 4 y sus paises se han borrado con exito");
System.out.println("\n--- 4. REINSERTAR CONTINENTE DESDE RAM ---");
cs.insertarContinente(c4);
System.out.println("Continente 4 se ha reinsertado");
System.out.println("\n--- 5. ACTUALIZAR NOMBRE A 'Paneuropa' ---");
c4.setNombreContinente("Paneuropa");
cs.actualizarContinente(c4);
System.out.println("Nombre actualizado a Paneuropa");
System.out.println("\n--- COMPROBACIÓN FINAL ---");
Continente resultado = cs.obtenerContinente("04");
System.out.println(resultado);
}
}catch(MapaMundiException e) {
System.out.println("Errror capturado: " + e.getMessage());
}
}
}
@@ -0,0 +1,15 @@
package org.palomafp.aadd.ss.dao;
import org.palomafp.aadd.ss.excepciones.MapaMundiException;
import java.util.List;
import org.palomafp.aadd.ss.model.Continente;
public interface IContinenteDAO {
List<Continente> obtenerListaContinentes() throws MapaMundiException;
Continente obtenerContinente(String codigo) throws MapaMundiException;
int borrarContinente(String codigo) throws MapaMundiException;
int insertarContinente(Continente continente) throws MapaMundiException;
int actuaizarContinente(Continente continente) throws MapaMundiException;
}
@@ -0,0 +1,17 @@
package org.palomafp.aadd.ss.dao;
import java.util.List;
import org.palomafp.aadd.ss.excepciones.MapaMundiException;
import org.palomafp.aadd.ss.model.Pais;
public interface IPaisDAO {
List<Pais> obtenerListaPaises() throws MapaMundiException;
Pais obtenerPaisPorIdentificador(int idPais) throws MapaMundiException;
int borrarPais(int idPais) throws MapaMundiException;
int insertarPais(Pais pais) throws MapaMundiException;
int actualizarPais(Pais pais) throws MapaMundiException;
int borrarPorContinente(String codContinente) throws MapaMundiException;
}
@@ -0,0 +1,126 @@
package org.palomafp.aadd.ss.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.palomafp.aadd.ss.excepciones.MapaMundiException;
import java.util.ArrayList;
import java.util.List;
import org.palomafp.aadd.ss.dao.IContinenteDAO;
import org.palomafp.aadd.ss.model.Continente;
public class ContinenteDaoJDBC implements IContinenteDAO{
private final Connection conexion;
public ContinenteDaoJDBC(Connection conexion) {
this.conexion = conexion;
}
@Override
public List<Continente> obtenerListaContinentes() throws MapaMundiException {
List<Continente> listaContinentes = new ArrayList<>();
String sql = "SELECT * FROM T_CONTINENTE";
try(PreparedStatement sentencia = conexion.prepareStatement(sql);
ResultSet resultado = sentencia.executeQuery()){
while(resultado.next()) {
listaContinentes.add(new Continente(resultado.getString("codigo"), resultado.getString("nombre_continente")));
}
} catch (SQLException e) {
throw new MapaMundiException("Erro al obtener los continentes" + e);
}
return listaContinentes;
}
@Override
public Continente obtenerContinente(String codigo) throws MapaMundiException {
Continente c = null;
String sql = "SELECT * FROM T_CONTINENTE WHERE codigo = ?";
try(PreparedStatement sentencia = conexion.prepareStatement(sql)){
sentencia.setString(1, codigo);
try(ResultSet resultado = sentencia.executeQuery()){
if(resultado.next()) {
c = new Continente(resultado.getString("codigo"), resultado.getString("nombre_continente"));
}
} catch (SQLException e) {
throw new MapaMundiException("Erro al obtener los continentes" + e);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
return c;
}
@Override
public int borrarContinente(String codigo) throws MapaMundiException {
String sql = "DELETE FROM T_CONTINENTE WHERE codigo = ?";
try(PreparedStatement sentencia = conexion.prepareStatement(sql)){
sentencia.setString(1, codigo);
return sentencia.executeUpdate();
} catch (SQLException e) {
throw new MapaMundiException("Erro al obtener los continente" + e);
}
}
@Override
public int insertarContinente(Continente continente) throws MapaMundiException {
String sql = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)";
try(PreparedStatement sentencia = conexion.prepareStatement(sql)){
sentencia.setString(1, continente.getCodContinente());
sentencia.setString(2, continente.getNombreContinente());
return sentencia.executeUpdate();
} catch (SQLException e) {
throw new MapaMundiException("Erro al obtener los continentes" + e);
}
}
@Override
public int actuaizarContinente(Continente continente) throws MapaMundiException {
String sql = "UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?";
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
ps.setString(1, continente.getNombreContinente());
ps.setString(2, continente.getCodContinente());
return ps.executeUpdate();
} catch (SQLException e) {
throw new MapaMundiException("Erro al obtener los continentes" + e);
}
}
}
@@ -0,0 +1,151 @@
package org.palomafp.aadd.ss.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.ss.dao.IPaisDAO;
import org.palomafp.aadd.ss.excepciones.MapaMundiException;
import org.palomafp.aadd.ss.model.Pais;
public class PaisDaoJDBC implements IPaisDAO{
private Connection conexion;
public PaisDaoJDBC(Connection conexion) {
this.conexion = conexion;
}
@Override
public List<Pais> obtenerListaPaises() throws MapaMundiException {
List<Pais> lista = new ArrayList<>();
String sql = "SELECT * FROM T_PAIS";
try (PreparedStatement ps = conexion.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
lista.add(new Pais(
rs.getString("cod_continente"),
rs.getInt("identificador"),
rs.getString("nombre_pais"),
rs.getString("capital")
));
}
} catch (SQLException e) {
throw new MapaMundiException("Error al obtener los paises" + e);
}
return lista;
}
@Override
public Pais obtenerPaisPorIdentificador(int idPais) throws MapaMundiException {
Pais pais = null;
String sql = "SELECT cod_continente, identificador, nombre_pais, capital FROM T_PAIS WHERE identificador = ?";
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
ps.setInt(1, idPais);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
pais = new Pais(
rs.getString("cod_continente"),
rs.getInt("identificador"),
rs.getString("nombre_pais"),
rs.getString("capital")
);
}
} catch (SQLException e) {
throw new MapaMundiException("Error al obenter pais con el id: " + idPais + e);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
return pais;
}
@Override
public int borrarPais(int idPais) throws MapaMundiException {
String sql = "DELETE FROM T_PAIS WHERE identificador = ?";
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
ps.setInt(1, idPais);
return ps.executeUpdate();
} catch (SQLException e) {
throw new MapaMundiException("Error al borrar el pais con el id: " + idPais + 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 = conexion.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 pais " + 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 = conexion.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 pais " + e);
}
}
@Override
public int borrarPorContinente(String codContinente) throws MapaMundiException {
String sql = "DELETE FROM T_PAIS WHERE cod_continente = ?";
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
ps.setString(1, codContinente);
return ps.executeUpdate();
} catch (SQLException e) {
throw new MapaMundiException("Error al borrar paices por continente" + e);
}
}
}
@@ -0,0 +1,13 @@
package org.palomafp.aadd.ss.excepciones;
public class MapaMundiException extends Exception{
public MapaMundiException(String mensaje) {
super(mensaje);
}
public MapaMundiException(String mensaje, Throwable causa) {
super(mensaje, causa);
}
}
@@ -0,0 +1,46 @@
package org.palomafp.aadd.ss.gestores;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class GestorConfiguracion {
private static final String RUTA_CONFIG = "application.properties";
private static final Properties PROPIEDADES = new Properties();
static {
try(InputStream inputStream = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_CONFIG)){
if ( inputStream == null) {
throw new IOException("No se ha podido encontrar el archivo de configuracion");
}
PROPIEDADES.load(inputStream);
} catch (IOException e) {
throw new RuntimeException("No ha podido cargar el archivo" + e);
}
}
public static String getConexionURL() {
return PROPIEDADES.getProperty("sgdb.conexion.url");
}
public static String getNombreBBDD() {
return PROPIEDADES.getProperty("bbdd.nombre");
}
public static String getUsuarioBBDD() {
return PROPIEDADES.getProperty("bbdd.usuario");
}
public static String getClaveBBDD() {
return PROPIEDADES.getProperty("bbdd.clave");
}
}
@@ -0,0 +1,21 @@
package org.palomafp.aadd.ss.gestores;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class GestroConexionBBDD {
private GestroConexionBBDD() {
}
public static Connection obtenerConexion() throws SQLException {
String url = GestorConfiguracion.getConexionURL() + GestorConfiguracion.getNombreBBDD();
String usuario = GestorConfiguracion.getUsuarioBBDD();
String clave = GestorConfiguracion.getClaveBBDD();
return DriverManager.getConnection(url, usuario, clave);
}
}
@@ -0,0 +1,40 @@
package org.palomafp.aadd.ss.model;
public class Continente {
private String codContinente;
private String nombreContinente;
public Continente() {}
public Continente(String codContinente, String nombreContinente) {
this.codContinente = codContinente;
this.nombreContinente = nombreContinente;
}
public String getCodContinente() {
return codContinente;
}
public void setCodContinente(String codContinente) {
this.codContinente = codContinente;
}
public String getNombreContinente() {
return nombreContinente;
}
public void setNombreContinente(String nombreContinente) {
this.nombreContinente = nombreContinente;
}
@Override
public String toString() {
return "Continente [codContinente=" + codContinente + ", nombreContinente=" + nombreContinente + "]";
}
}
+48
View File
@@ -0,0 +1,48 @@
package org.palomafp.aadd.ss.model;
public class Pais {
private String codContinente;
private int identificador;
private String nombrePais;
private String capital;
public Pais() {
}
public Pais(String codContinente, int identificador, String nombrePais, String capital) {
this.codContinente = codContinente;
this.identificador = identificador;
this.nombrePais = nombrePais;
this.capital = capital;
}
public String getCodContinente() {
return codContinente;
}
public void setCodContinente(String codContinente) {
this.codContinente = codContinente;
}
public int getIdentificador() {
return identificador;
}
public void setIdentificador(int identificador) {
this.identificador = identificador;
}
public String getNombrePais() {
return nombrePais;
}
public void setNombrePais(String nombrePais) {
this.nombrePais = nombrePais;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
@Override
public String toString() {
return "Pais [codContinente=" + codContinente + ", identificador=" + identificador + ", nombrePais="
+ nombrePais + ", capital=" + capital + "]";
}
}
@@ -0,0 +1,118 @@
package org.palomafp.aadd.ss.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.palomafp.aadd.ss.dao.IContinenteDAO;
import org.palomafp.aadd.ss.dao.IPaisDAO;
import org.palomafp.aadd.ss.dao.impl.ContinenteDaoJDBC;
import org.palomafp.aadd.ss.dao.impl.PaisDaoJDBC;
import org.palomafp.aadd.ss.excepciones.MapaMundiException;
import org.palomafp.aadd.ss.gestores.GestroConexionBBDD;
import org.palomafp.aadd.ss.model.Continente;
public class ContinenteService {
public List<Continente> obtenerListaContinentes() throws MapaMundiException {
try(Connection conexion = GestroConexionBBDD.obtenerConexion()){
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
return continenteDao.obtenerListaContinentes();
} catch (SQLException e) {
throw new MapaMundiException("Error de conexión al obtener los continentes", e);
}
}
public Continente obtenerContinente(String codigo) throws MapaMundiException{
try(Connection conexion = GestroConexionBBDD.obtenerConexion()){
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
return continenteDao.obtenerContinente(codigo);
} catch (SQLException e) {
throw new MapaMundiException("Error de conexión al obtener el continente " + codigo, e);
}
}
public int insertarContinente(Continente continente) throws MapaMundiException {
try (Connection conexion = GestroConexionBBDD.obtenerConexion()) {
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
return continenteDao.insertarContinente(continente);
} catch (SQLException e) {
throw new MapaMundiException("Error de conexión al insertar el continente " + e);
}
}
public int actualizarContinente(Continente c) throws MapaMundiException{
try(Connection conexion = GestroConexionBBDD.obtenerConexion()){
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
return continenteDao.actuaizarContinente(c);
} catch (SQLException e) {
throw new MapaMundiException("Error al insertar el continente " + c.getCodContinente(), e);
}
}
public void borrarContinenteyPaises(String codigo) throws MapaMundiException {
try(Connection conexion = GestroConexionBBDD.obtenerConexion()){
try {
conexion.setAutoCommit(false);
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
paisDao.borrarPorContinente(codigo);
continenteDao.borrarContinente(codigo);
conexion.commit();
}catch (MapaMundiException e) {
conexion.rollback();
throw e;
}
}catch(SQLException e) {
throw new MapaMundiException("Error al borrar el cotinente " + e);
}
}
/**
public void borrarConPaises(String codigo) throws MapaMundiException {
try (Connection conexion = GestorConexionBBDD.getConexion()) {
try {
conexion.setAutoCommit(false);
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
paisDao.borrarPorContinente(codigo);
// 2. Borrar el continente
continenteDao.borrar(codigo);
// 3. Confirmar los dos borrados a la vez
conexion.commit();
} catch (MapaMundiException e) {
conexion.rollback(); // Si algo falla deshace todo
throw e; // Se lanza la misma excepción que se atrapó hacia arriba (al main)
}
} catch (SQLException e) {
throw new MapaMundiException("Error en el borrado del continente " + codigo, e);
}
}
**/
}
@@ -0,0 +1,5 @@
package org.palomafp.aadd.ss.service;
public class PaisService {
}
+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
#Sat Oct 31 22:02:11 CET 2026
artifactId=aadd_conect03
groupId=aadd_conect03
m2e.projectLocation=C\:\\Users\\sajed\\Desktop\\2DAM\\AADD\\eclipse-workspace\\aadd_conect03
m2e.projectName=aadd_conect03
version=0.0.1-SNAPSHOT
@@ -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>aadd_conect03</groupId>
<artifactId>aadd_conect03</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>
+4
View File
@@ -0,0 +1,4 @@
sgdb.conexion.url=jdbc:mysql://192.168.1.134:3306/
bbdd.nombre=Mapa_Mundi
bbdd.usuario=root
bbdd.clave=root
Binary file not shown.