Primer commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package org.palomafp.aadd.aqv;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aqv.excepcion.MapaMundiException;
|
||||
import org.palomafp.aadd.aqv.model.Continente;
|
||||
import org.palomafp.aadd.aqv.model.Pais;
|
||||
import org.palomafp.aadd.aqv.service.ContinenteService;
|
||||
import org.palomafp.aadd.aqv.service.PaisService;
|
||||
|
||||
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();
|
||||
|
||||
List<Pais> listaPaises = paisService.obtenerListaPaises();
|
||||
for (Pais pais : listaPaises) {
|
||||
System.out.println(pais);
|
||||
}
|
||||
System.out.println("----------Fin---obtenerListaPaises-------------");
|
||||
|
||||
int idPais = 152;
|
||||
Pais pais = paisService.obtenerPaisPorID(idPais);
|
||||
System.out.println(pais);
|
||||
System.out.println("--------Fin---obtenerPaisPorIdentificador---------------");
|
||||
|
||||
if (pais != null) {
|
||||
paisService.borrarPais(idPais);
|
||||
System.out.println("--------Fin---borrarPais---------------");
|
||||
|
||||
paisService.insertarPais(pais);
|
||||
System.out.println("---------Fin---insertarPais--------------");
|
||||
|
||||
pais.setCapital("Capital City");
|
||||
paisService.actualizarPais(pais);
|
||||
System.out.println("---------Fin---actualizarPais--------------");
|
||||
}
|
||||
}
|
||||
|
||||
private void realizarTratamientoContinentes() throws MapaMundiException {
|
||||
ContinenteService continenteService = new ContinenteService();
|
||||
|
||||
List<Continente> listaContinentes = continenteService.obtenerListaContinentes();
|
||||
for (Continente continente : listaContinentes) {
|
||||
System.out.println(continente);
|
||||
}
|
||||
System.out.println("----------Fin---obtenerListaContinentes-------------");
|
||||
|
||||
String codigoContinente = "04";
|
||||
Continente continente = continenteService.obtenerContinentePorCodigo(codigoContinente);
|
||||
System.out.println(continente);
|
||||
System.out.println("--------Fin---obtenerContinentePorCodigo---------------");
|
||||
|
||||
if (continente != null) {
|
||||
continenteService.borrarContinente(codigoContinente);
|
||||
System.out.println("--------Fin---borrarContinente---------------");
|
||||
|
||||
continenteService.insertarContinente(continente);
|
||||
System.out.println("---------Fin---insertarContinente--------------");
|
||||
|
||||
continente.setNombreContinente("Paneuropeo");
|
||||
continenteService.actualizarContinente(continente);
|
||||
;
|
||||
System.out.println("---------Fin---actualizarContinente--------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.palomafp.aadd.aqv.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aqv.excepcion.MapaMundiException;
|
||||
import org.palomafp.aadd.aqv.model.Continente;
|
||||
|
||||
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,20 @@
|
||||
package org.palomafp.aadd.aqv.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aqv.excepcion.MapaMundiException;
|
||||
import org.palomafp.aadd.aqv.model.Pais;
|
||||
|
||||
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,125 @@
|
||||
package org.palomafp.aadd.aqv.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.aqv.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.aqv.excepcion.MapaMundiException;
|
||||
import org.palomafp.aadd.aqv.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<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.setNombreContinente(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.setNombreContinente(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.getNombreContinente());
|
||||
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.getNombreContinente());
|
||||
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,170 @@
|
||||
package org.palomafp.aadd.aqv.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.aqv.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.aqv.excepcion.MapaMundiException;
|
||||
import org.palomafp.aadd.aqv.model.Continente;
|
||||
import org.palomafp.aadd.aqv.model.Pais;
|
||||
|
||||
|
||||
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.setNombrePais(nombrePais);
|
||||
pais.setCapital(capital);
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codigoContinente);
|
||||
continente.setNombreContinente(nombreContinente);
|
||||
pais.setCodContinente(nombreContinente);
|
||||
|
||||
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.setNombrePais(nombrePais);
|
||||
pais.setCapital(capital);
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(codigoContinente);
|
||||
continente.setNombreContinente(nombreContinente);
|
||||
pais.setCodContinente(nombreContinente);
|
||||
}
|
||||
|
||||
} 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.getNombrePais());
|
||||
sentenciaPreparada.setString(3, pais.getCapital());
|
||||
sentenciaPreparada.setString(4, pais.getCodContinente());
|
||||
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.getNombrePais());
|
||||
sentenciaPreparada.setString(2, pais.getCapital());
|
||||
sentenciaPreparada.setString(4, pais.getCodContinente());
|
||||
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,14 @@
|
||||
package org.palomafp.aadd.aqv.excepcion;
|
||||
|
||||
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,24 @@
|
||||
package org.palomafp.aadd.aqv.gestores;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
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,47 @@
|
||||
package org.palomafp.aadd.aqv.gestores;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||
|
||||
private 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);
|
||||
} 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,37 @@
|
||||
package org.palomafp.aadd.aqv.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombreContinente;
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
public Continente(String codigo, String nombreContinente) {
|
||||
this.codigo = codigo;
|
||||
this.nombreContinente = nombreContinente;
|
||||
}
|
||||
|
||||
public String getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
public String getNombreContinente() {
|
||||
return nombreContinente;
|
||||
}
|
||||
|
||||
public void setNombreContinente(String nombreContinente) {
|
||||
this.nombreContinente = nombreContinente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombreContinente=" + nombreContinente + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.palomafp.aadd.aqv.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,99 @@
|
||||
package org.palomafp.aadd.aqv.service;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aqv.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.aqv.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.aqv.dao.impl.ContinenteDaoJDBC;
|
||||
import org.palomafp.aadd.aqv.dao.impl.PaisDaoJDBC;
|
||||
import org.palomafp.aadd.aqv.excepcion.MapaMundiException;
|
||||
import org.palomafp.aadd.aqv.gestores.GestorConexionesBBDD;
|
||||
import org.palomafp.aadd.aqv.model.Continente;
|
||||
|
||||
|
||||
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);
|
||||
|
||||
conexion.setAutoCommit(false);
|
||||
|
||||
try {
|
||||
paisDAO.borrarPaisesPorContinente(codigoContinente);
|
||||
|
||||
continenteDAO.borrarContinente(codigoContinente);
|
||||
|
||||
conexion.commit();
|
||||
|
||||
} catch (MapaMundiException | RuntimeException e) {
|
||||
|
||||
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,73 @@
|
||||
package org.palomafp.aadd.aqv.service;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aqv.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.aqv.dao.impl.PaisDaoJDBC;
|
||||
import org.palomafp.aadd.aqv.excepcion.MapaMundiException;
|
||||
import org.palomafp.aadd.aqv.gestores.GestorConexionesBBDD;
|
||||
import org.palomafp.aadd.aqv.model.Pais;
|
||||
|
||||
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