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
@@ -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 {
}