Primer commit
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Compilación Java
|
||||||
|
bin/
|
||||||
|
target/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Sistema operativo
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# Elegir nombres de entradas que sean SIGNIFICATIVOS
|
||||||
|
# y que no se repitan con otras entradas de la misma aplicación
|
||||||
|
|
||||||
|
# Información relativa a la base de datos
|
||||||
|
sgdb.conexion.url=jdbc:mysql://192.168.1.36:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=root
|
||||||
|
bbdd.clave=mysql_123
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<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_01</groupId>
|
||||||
|
<artifactId>Conectores_01</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>24</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package org.palomafp.aadd.inm;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.inm.excepcion.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
import org.palomafp.aadd.inm.model.Pais;
|
||||||
|
import org.palomafp.aadd.inm.service.ContinenteService;
|
||||||
|
import org.palomafp.aadd.inm.service.PaisService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* AppPrincipal: Clase que realiza la gestión de las principales operaciones asociadas a países y continentes.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 28 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class AppPrincipal {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
AppPrincipal app = new AppPrincipal();
|
||||||
|
|
||||||
|
try {
|
||||||
|
app.realizarTratamientoPaises();;
|
||||||
|
|
||||||
|
app.realizarTratamientoContinentes();
|
||||||
|
|
||||||
|
} catch (MapaMundiException e) {
|
||||||
|
System.out.println("Error al realizar el tratamiento de Mapamundi: " + e.getMessage());
|
||||||
|
System.err.println(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void realizarTratamientoPaises() throws MapaMundiException {
|
||||||
|
PaisService paisService = new PaisService();
|
||||||
|
|
||||||
|
// 1. Imprimir lista Paises
|
||||||
|
List<Pais> listaPaises = paisService.obtenerListaPaises();
|
||||||
|
for (Pais pais : listaPaises) {
|
||||||
|
System.out.println(pais);
|
||||||
|
}
|
||||||
|
System.out.println("----------Fin---obtenerListaPaises-------------");
|
||||||
|
|
||||||
|
// 2. Imprimir país 152
|
||||||
|
int idPais = 152;
|
||||||
|
Pais pais = paisService.obtenerPaisPorID(idPais);
|
||||||
|
System.out.println(pais);
|
||||||
|
System.out.println("--------Fin---obtenerPaisPorIdentificador---------------");
|
||||||
|
|
||||||
|
// Se realizan las operaciones si existe el país.
|
||||||
|
if (pais != null) {
|
||||||
|
// 3. Borrar Paises 152
|
||||||
|
paisService.borrarPais(idPais);
|
||||||
|
System.out.println("--------Fin---borrarPais---------------");
|
||||||
|
|
||||||
|
// 4. Insertar Paises 152
|
||||||
|
paisService.insertarPais(pais);
|
||||||
|
System.out.println("---------Fin---insertarPais--------------");
|
||||||
|
|
||||||
|
// 5. Actualizar País - Capital City
|
||||||
|
pais.setCapital("Capital City");
|
||||||
|
paisService.actualizarPais(pais);
|
||||||
|
System.out.println("---------Fin---actualizarPais--------------");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void realizarTratamientoContinentes() throws MapaMundiException {
|
||||||
|
ContinenteService continenteService = new ContinenteService();
|
||||||
|
|
||||||
|
// 1. Imprimir lista Continentes
|
||||||
|
List<Continente> listaContinentes = continenteService.obtenerListaContinentes();
|
||||||
|
for (Continente continente : listaContinentes) {
|
||||||
|
System.out.println(continente);
|
||||||
|
}
|
||||||
|
System.out.println("----------Fin---obtenerListaContinentes-------------");
|
||||||
|
|
||||||
|
// 2. Imprimir contiente "04"
|
||||||
|
String codigoContinente = "04";
|
||||||
|
Continente continente = continenteService.obtenerContinentePorCodigo(codigoContinente);
|
||||||
|
System.out.println(continente);
|
||||||
|
System.out.println("--------Fin---obtenerContinentePorCodigo---------------");
|
||||||
|
|
||||||
|
// Se realizan las operaciones si existe el continente.
|
||||||
|
if (continente != null) {
|
||||||
|
// 3. Borrar Contiente "04"
|
||||||
|
continenteService.borrarContinente(codigoContinente);
|
||||||
|
System.out.println("--------Fin---borrarContinente---------------");
|
||||||
|
|
||||||
|
// 4. Insertar Contiente "04"
|
||||||
|
continenteService.insertarContinente(continente);
|
||||||
|
System.out.println("---------Fin---insertarContinente--------------");
|
||||||
|
|
||||||
|
// 5. Actualizar Continente - Paneuropeo
|
||||||
|
continente.setNombre("Paneuropeo");
|
||||||
|
continenteService.actualizarContinente(continente);;
|
||||||
|
System.out.println("---------Fin---actualizarContinente--------------");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package org.palomafp.aadd.inm.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.inm.excepcion.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* IPaisDAO: Interfaz que contiene los métodos para obtener información de continentes desde un origen de datos.
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 26 sept 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
List<Continente> obtenerListaContinentes() throws MapaMundiException;
|
||||||
|
Continente obtenerContinentePorCodigo(String codigoContinente) throws MapaMundiException;
|
||||||
|
void insertarContinente(Continente continente) throws MapaMundiException;
|
||||||
|
void actualizarContinente(Continente continente) throws MapaMundiException;
|
||||||
|
void borrarContinente(String codigoContinente) throws MapaMundiException;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.palomafp.aadd.inm.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.inm.excepcion.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.inm.model.Pais;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* IPaisDAO: Interfaz que contiene los métodos para obtener información de países desde un origen de datos.
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 26 sept 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
List<Pais> obtenerListaPaises() throws MapaMundiException;
|
||||||
|
Pais obtenerPaisPorIdentificador(int idPais) throws MapaMundiException;
|
||||||
|
void insertarPais(Pais pais) throws MapaMundiException;
|
||||||
|
void actualizarPais(Pais pais) throws MapaMundiException;
|
||||||
|
void borrarPais(int idPais) throws MapaMundiException;
|
||||||
|
void borrarPaisesPorContinente(String codContinente) throws MapaMundiException;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package org.palomafp.aadd.inm.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.inm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.inm.excepcion.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* ContinenteDaoJDBC: Clase que realiza la implementación JDBC del interfaz
|
||||||
|
* IContinenteDAO
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 29 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<Continente>();
|
||||||
|
|
||||||
|
String sentenciaSQL = """
|
||||||
|
SELECT *
|
||||||
|
FROM T_CONTINENTE
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentencia = conexion.prepareStatement(sentenciaSQL);
|
||||||
|
ResultSet resultados = sentencia.executeQuery();) {
|
||||||
|
while (resultados.next()) {
|
||||||
|
String codigoContinente = resultados.getString("codigo");
|
||||||
|
String nombreContinente = resultados.getString("nombre_continente");
|
||||||
|
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContinente);
|
||||||
|
|
||||||
|
listaContinentes.add(continente);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener la lista de continentes.", e);
|
||||||
|
}
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCodigo(String codigoContinente) throws MapaMundiException {
|
||||||
|
Continente continente = null;
|
||||||
|
|
||||||
|
String sentenciaSQL = """
|
||||||
|
SELECT *
|
||||||
|
FROM T_CONTINENTE
|
||||||
|
WHERE codigo = ?
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setString(1, codigoContinente);
|
||||||
|
ResultSet resultados = sentenciaPreparada.executeQuery();
|
||||||
|
if (resultados.next()) {
|
||||||
|
String nombreContinente = resultados.getString("nombre_continente");
|
||||||
|
|
||||||
|
continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener un continente por código.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
String sentenciaSQL = """
|
||||||
|
INSERT INTO T_CONTINENTE (codigo, nombre_continente)
|
||||||
|
VALUES (?, ?)
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setString(1, continente.getCodigo());
|
||||||
|
sentenciaPreparada.setString(2, continente.getNombre());
|
||||||
|
sentenciaPreparada.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar un continente.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
String sentenciaSQL = """
|
||||||
|
UPDATE T_CONTINENTE
|
||||||
|
SET nombre_continente = ?
|
||||||
|
WHERE codigo= ?
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setString(1, continente.getNombre());
|
||||||
|
sentenciaPreparada.setString(2, continente.getCodigo());
|
||||||
|
sentenciaPreparada.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar un continente.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrarContinente(String codigoContinente) throws MapaMundiException {
|
||||||
|
String sentenciaSQL = """
|
||||||
|
DELETE FROM T_CONTINENTE
|
||||||
|
WHERE codigo = ?
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setString(1, codigoContinente);
|
||||||
|
sentenciaPreparada.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar un continente.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
package org.palomafp.aadd.inm.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.inm.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.inm.excepcion.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
import org.palomafp.aadd.inm.model.Pais;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* PaisDaoJDBC: Clase que realiza la implementación JDBC del interfaz IPaisDAO
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 29 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class PaisDaoJDBC implements IPaisDAO {
|
||||||
|
private final Connection conexion;
|
||||||
|
|
||||||
|
public PaisDaoJDBC(Connection conexion) {
|
||||||
|
this.conexion = conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerListaPaises() throws MapaMundiException{
|
||||||
|
List<Pais> listaPaises = new ArrayList<Pais>();
|
||||||
|
|
||||||
|
String sentenciaSQL = """
|
||||||
|
SELECT *
|
||||||
|
FROM T_PAIS p JOIN T_CONTINENTE c
|
||||||
|
ON p.cod_continente=c.codigo
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentencia = conexion.prepareStatement(sentenciaSQL);
|
||||||
|
ResultSet resultados = sentencia.executeQuery();) {
|
||||||
|
while (resultados.next()) {
|
||||||
|
int identificador = resultados.getInt("p.identificador");
|
||||||
|
String nombrePais = resultados.getString("p.nombre_pais");
|
||||||
|
String capital = resultados.getString("p.capital");
|
||||||
|
String codigoContinente = resultados.getString("c.codigo");
|
||||||
|
String nombreContinente = resultados.getString("c.nombre_continente");
|
||||||
|
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(identificador);
|
||||||
|
pais.setNombre(nombrePais);
|
||||||
|
pais.setCapital(capital);
|
||||||
|
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContinente);
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
listaPaises.add(pais);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener la lista de países.",e);
|
||||||
|
}
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorIdentificador(int idPais) throws MapaMundiException{
|
||||||
|
Pais pais = null;
|
||||||
|
|
||||||
|
String sentenciaSQL = """
|
||||||
|
SELECT *
|
||||||
|
FROM T_PAIS p JOIN T_CONTINENTE c
|
||||||
|
ON p.cod_continente=c.codigo
|
||||||
|
WHERE p.identificador = ?
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setInt(1, idPais);
|
||||||
|
ResultSet resultados = sentenciaPreparada.executeQuery();
|
||||||
|
if (resultados.next()) {
|
||||||
|
int identificador = resultados.getInt("p.identificador");
|
||||||
|
String nombrePais = resultados.getString("p.nombre_pais");
|
||||||
|
String capital = resultados.getString("p.capital");
|
||||||
|
String codigoContinente = resultados.getString("c.codigo");
|
||||||
|
String nombreContinente = resultados.getString("c.nombre_continente");
|
||||||
|
|
||||||
|
pais = new Pais();
|
||||||
|
pais.setIdentificador(identificador);
|
||||||
|
pais.setNombre(nombrePais);
|
||||||
|
pais.setCapital(capital);
|
||||||
|
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(codigoContinente);
|
||||||
|
continente.setNombre(nombreContinente);
|
||||||
|
pais.setContinente(continente);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener un país por identificador.",e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertarPais(Pais pais) throws MapaMundiException{
|
||||||
|
String sentenciaSQL = """
|
||||||
|
INSERT INTO T_PAIS (identificador, nombre_pais, capital, cod_continente)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setInt(1, pais.getIdentificador());
|
||||||
|
sentenciaPreparada.setString(2, pais.getNombre());
|
||||||
|
sentenciaPreparada.setString(3, pais.getCapital());
|
||||||
|
sentenciaPreparada.setString(4, pais.getContinente().getCodigo());
|
||||||
|
sentenciaPreparada.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar un país.",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarPais(Pais pais) throws MapaMundiException{
|
||||||
|
String sentenciaSQL = """
|
||||||
|
UPDATE T_PAIS
|
||||||
|
SET nombre_pais = ?,
|
||||||
|
capital = ?,
|
||||||
|
cod_continente= ?
|
||||||
|
WHERE identificador= ?
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setString(1, pais.getNombre());
|
||||||
|
sentenciaPreparada.setString(2, pais.getCapital());
|
||||||
|
sentenciaPreparada.setString(3, pais.getContinente().getCodigo());
|
||||||
|
sentenciaPreparada.setInt(4, pais.getIdentificador());
|
||||||
|
sentenciaPreparada.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar un país.",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrarPais(int idPais) throws MapaMundiException{
|
||||||
|
String sentenciaSQL = """
|
||||||
|
DELETE FROM T_PAIS
|
||||||
|
WHERE identificador = ?
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setInt(1, idPais);
|
||||||
|
sentenciaPreparada.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar un país.",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrarPaisesPorContinente(String codContinente) throws MapaMundiException {
|
||||||
|
String sentenciaSQL = """
|
||||||
|
DELETE FROM T_PAIS
|
||||||
|
WHERE cod_continente = ?
|
||||||
|
""";
|
||||||
|
// System.out.println(sentenciaSQL);
|
||||||
|
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||||
|
sentenciaPreparada.setString(1, codContinente);
|
||||||
|
sentenciaPreparada.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar los países de un continente.",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.palomafp.aadd.inm.excepcion;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* MapaMundoException: Clase abstracta que representa la excepción base del proyecto MapaMundi
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 15 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MapaMundiException extends Exception {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.palomafp.aadd.inm.gestores;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* GestorConfiguracion: Clase que carga la configuración de la aplicación desde
|
||||||
|
* un fichero de propiedades
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 8 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GestorConexionesBBDD {
|
||||||
|
|
||||||
|
// Constructor privado: de esta manera conseguimos que esta clase no se puede
|
||||||
|
// instanciar.
|
||||||
|
private GestorConexionesBBDD() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection obtenerConexion() throws SQLException {
|
||||||
|
Connection conexion = null;
|
||||||
|
|
||||||
|
String urlConexionCompleta = GestorConfiguracion.getConexionURL() + GestorConfiguracion.getNombreBBDD();
|
||||||
|
String usuarioBBDD = GestorConfiguracion.getUsuarioBBDD();
|
||||||
|
String claveBBDD = GestorConfiguracion.getClaveBBDD();
|
||||||
|
conexion = DriverManager.getConnection(urlConexionCompleta, usuarioBBDD, claveBBDD);
|
||||||
|
|
||||||
|
return conexion;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package org.palomafp.aadd.inm.gestores;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* GestorConfiguracion: Clase que carga la configuración de la aplicación desde
|
||||||
|
* un fichero de propiedades
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 8 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GestorConfiguracion {
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||||
|
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||||
|
|
||||||
|
// Constructor privado: de esta manera conseguimos que esta clase no se puede
|
||||||
|
// instanciar.
|
||||||
|
private GestorConfiguracion() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Este bloque se ejecuta una sola vez, cuando se carga la clase
|
||||||
|
// GestorConfiguracion
|
||||||
|
static {
|
||||||
|
try (InputStream inputStream = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION);)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (inputStream == null) {
|
||||||
|
throw new IOException(
|
||||||
|
"No se encuentra "+ RUTA_FICHERO_CONFIGURACION + " en el classpath");
|
||||||
|
}
|
||||||
|
PROPIEDADES_CONFIGURACION.load(inputStream);
|
||||||
|
// System.out.println(PROPIEDADES_CONFIGURACION);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("No se ha podido cargar el fichero de configuración", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getClaseDriver() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("sgdb.clase.driver");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getConexionURL() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("sgdb.conexion.url");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getNombreBBDD() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("bbdd.nombre");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUsuarioBBDD() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("bbdd.usuario");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getClaveBBDD() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("bbdd.clave");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package org.palomafp.aadd.inm.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Continente: Clase que se asocia a la información de un continente.
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 24 sept 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.palomafp.aadd.inm.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Pais Clase que se asocia a la información de un país.
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 24 sept 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String capital;
|
||||||
|
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
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 Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + ", continente="
|
||||||
|
+ continente + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package org.palomafp.aadd.inm.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.inm.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.inm.dao.impl.ContinenteDaoJDBC;
|
||||||
|
import org.palomafp.aadd.inm.dao.impl.PaisDaoJDBC;
|
||||||
|
import org.palomafp.aadd.inm.excepcion.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.inm.gestores.GestorConexionesBBDD;
|
||||||
|
import org.palomafp.aadd.inm.model.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* ContinenteService: Clase que se encarga de las operaciones de servicio de un Continente.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 29 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ContinenteService {
|
||||||
|
|
||||||
|
public List<Continente> obtenerListaContinentes() throws MapaMundiException {
|
||||||
|
List<Continente> listaContinentes = null;
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoJDBC(conexion);
|
||||||
|
listaContinentes = continenteDAO.obtenerListaContinentes();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener la conexión", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente obtenerContinentePorCodigo(String codigoContinente) throws MapaMundiException {
|
||||||
|
Continente continente = null;
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoJDBC(conexion);
|
||||||
|
continente = continenteDAO.obtenerContinentePorCodigo(codigoContinente);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener la conexión", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoJDBC(conexion);
|
||||||
|
continenteDAO.insertarContinente(continente);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoJDBC(conexion);
|
||||||
|
continenteDAO.actualizarContinente(continente);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener la conexión", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrarContinente(String codigoContinente)
|
||||||
|
throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion =
|
||||||
|
GestorConexionesBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
IContinenteDAO continenteDAO =
|
||||||
|
new ContinenteDaoJDBC(conexion);
|
||||||
|
|
||||||
|
IPaisDAO paisDAO =
|
||||||
|
new PaisDaoJDBC(conexion);
|
||||||
|
|
||||||
|
// Se prepara la transacción de varias operaciones.
|
||||||
|
conexion.setAutoCommit(false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. Borrar los países del continente.
|
||||||
|
paisDAO.borrarPaisesPorContinente(codigoContinente);
|
||||||
|
|
||||||
|
// 2. Borrar el continente.
|
||||||
|
continenteDAO.borrarContinente(codigoContinente);
|
||||||
|
|
||||||
|
// 3. Confirmar la transacción.
|
||||||
|
conexion.commit();
|
||||||
|
|
||||||
|
} catch (MapaMundiException | RuntimeException e) {
|
||||||
|
|
||||||
|
// Si algo falla, deshacer las operaciones.
|
||||||
|
try {
|
||||||
|
conexion.rollback();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
e.addSuppressed(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException(
|
||||||
|
"Error al borrar el continente.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package org.palomafp.aadd.inm.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.inm.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.inm.dao.impl.PaisDaoJDBC;
|
||||||
|
import org.palomafp.aadd.inm.excepcion.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.inm.gestores.GestorConexionesBBDD;
|
||||||
|
import org.palomafp.aadd.inm.model.Pais;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* PaisService: Clase que se encarga de las operaciones de servicio de un Pais (Casos de uso de países).
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 29 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class PaisService {
|
||||||
|
|
||||||
|
public List<Pais> obtenerListaPaises() throws MapaMundiException {
|
||||||
|
List<Pais> listaPaises = null;
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IPaisDAO iPaisDAO = new PaisDaoJDBC(conexion);
|
||||||
|
listaPaises = iPaisDAO.obtenerListaPaises();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener la lista de países.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais obtenerPaisPorID(int idPais) throws MapaMundiException {
|
||||||
|
Pais pais = null;
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IPaisDAO iPaisDAO = new PaisDaoJDBC(conexion);
|
||||||
|
pais = iPaisDAO.obtenerPaisPorIdentificador(idPais);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener un paíse por identificador.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IPaisDAO iPaisDAO = new PaisDaoJDBC(conexion);
|
||||||
|
iPaisDAO.insertarPais(pais);
|
||||||
|
;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al crear un nuevo país.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarPais(Pais pais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion();) {
|
||||||
|
IPaisDAO iPaisDAO = new PaisDaoJDBC(conexion);
|
||||||
|
iPaisDAO.actualizarPais(pais);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar los datos de un país.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrarPais(int idPais) throws MapaMundiException {
|
||||||
|
|
||||||
|
try (Connection conexion = GestorConexionesBBDD.obtenerConexion()) {
|
||||||
|
|
||||||
|
IPaisDAO iPaisDAO = new PaisDaoJDBC(conexion);
|
||||||
|
iPaisDAO.borrarPais(idPais);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar un país.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user