Actividad 2.3 - Manipulación de datos Continente DAO
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,5 @@
|
||||
# info de mi bbdd
|
||||
sgdb.conexion.url=jdbc:mysql://10.100.11.134:3307/
|
||||
bbdd.nombre=Mapa_Mundi
|
||||
bbdd.usuario=root
|
||||
bbdd.pass=rootpass
|
||||
@@ -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>Conectores_02</groupId>
|
||||
<artifactId>Conectores_02</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.rmr;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.excepciones.MapaMundiException;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
import org.palomafp.aadd.rmr.service.ContinenteService;
|
||||
|
||||
public class AppPrincipal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ContinenteService continenteService = new ContinenteService ();
|
||||
|
||||
try {
|
||||
|
||||
System.out.println("PARTE 1");
|
||||
System.out.println("CONTINENTES - Lista: ");
|
||||
List<Continente> listaDeContinentes = continenteService.obtenerContoinentes();
|
||||
|
||||
for (Continente continente : listaDeContinentes) {
|
||||
System.out.println(continente);
|
||||
}
|
||||
|
||||
System.out.println("\nPARTE 2");
|
||||
System.out.println("CONTINENTE 4 - COnsulta: ");
|
||||
Continente continenteAConsultar = continenteService.obtenerContinentePorCodigo("04");
|
||||
System.out.println(continenteAConsultar);
|
||||
|
||||
if (continenteAConsultar != null) {
|
||||
|
||||
System.out.println("\nPARTE 3");
|
||||
System.out.println("CONTINENTE 04 - Borrado: ");
|
||||
continenteService.borrarContinente("04");
|
||||
System.out.println(continenteService.obtenerContinentePorCodigo("04"));
|
||||
|
||||
System.out.println("\nPARTE 4");
|
||||
System.out.println("CONTINENTE 04 - Insertado: ");
|
||||
continenteService.insertarContinente(continenteAConsultar);
|
||||
System.out.println(continenteService.obtenerContinentePorCodigo("04"));
|
||||
|
||||
System.out.println("\nPARTE 5");
|
||||
System.out.println("CONTINENTE 04 - Nombre actualizado: ");
|
||||
continenteAConsultar.setNombre("Paneuropa");
|
||||
continenteService.actualizarContinente(continenteAConsultar);
|
||||
System.out.println(continenteService.obtenerContinentePorCodigo("04"));
|
||||
}
|
||||
|
||||
} catch (MapaMundiException e) {
|
||||
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.palomafp.aadd.rmr.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ConexionBD {
|
||||
|
||||
private static final String FICHERO = "application.properties";
|
||||
|
||||
private static Properties cargarPropiedades() throws IOException {
|
||||
Properties propiedades = new Properties();
|
||||
InputStream entrada = null;
|
||||
try {
|
||||
entrada = ConexionBD.class.getClassLoader().getResourceAsStream("application.properties");
|
||||
propiedades.load(entrada);
|
||||
} catch (IOException e) {
|
||||
e.getMessage();
|
||||
} finally {
|
||||
if (entrada != null) {
|
||||
entrada.close();
|
||||
}
|
||||
}
|
||||
return propiedades;
|
||||
}
|
||||
|
||||
public static Connection obtenerConexion() throws IOException, SQLException {
|
||||
Properties propiedades = cargarPropiedades();
|
||||
|
||||
// estos nombres tienen que coincidir con el de application.properties
|
||||
String url = propiedades.getProperty("sgdb.conexion.url") + propiedades.getProperty("bbdd.nombre");
|
||||
String usuario = propiedades.getProperty("bbdd.usuario");
|
||||
String pass = propiedades.getProperty("bbdd.pass");
|
||||
|
||||
return DriverManager.getConnection(url, usuario, pass);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.palomafp.aadd.rmr.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.excepciones.MapaMundiException;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
List<Continente> obtenerContinentes() throws MapaMundiException;
|
||||
Continente obtenerContinentePorCodigo(String codigo) throws MapaMundiException;
|
||||
void borrarContinente(String codigo) throws MapaMundiException;
|
||||
void insertarContinente(Continente continente) throws MapaMundiException;
|
||||
void actualizarContinente(Continente continente) throws MapaMundiException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.palomafp.aadd.rmr.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.excepciones.MapaMundiException;
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
List<Pais> obtenerPaises() throws MapaMundiException;
|
||||
Pais obtenerPaisPorId(int identificador) throws MapaMundiException;
|
||||
void borrarPais(int identificador) throws MapaMundiException;
|
||||
void insertarPais(Pais pais) throws MapaMundiException;
|
||||
void actualizarPais(Pais pais) throws MapaMundiException;
|
||||
void borrarPaisesDeUnContinente (String codigoContinente) throws MapaMundiException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package org.palomafp.aadd.rmr.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.rmr.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.rmr.excepciones.MapaMundiException;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
|
||||
public class ContinenteJDBC implements IContinenteDAO {
|
||||
|
||||
private Connection conexion;
|
||||
|
||||
public ContinenteJDBC(Connection conexion) {
|
||||
this.conexion = conexion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerContinentes() throws MapaMundiException {
|
||||
|
||||
List<Continente> listaDeContinentes = new ArrayList<>();
|
||||
try {
|
||||
PreparedStatement sentencia = conexion
|
||||
.prepareStatement("SELECT codigo, nombre_continente FROM T_CONTINENTE");
|
||||
ResultSet resultado = sentencia.executeQuery();
|
||||
|
||||
while (resultado.next()) {
|
||||
String codigo = resultado.getString("codigo");
|
||||
String nombre = resultado.getString("nombre_continente");
|
||||
|
||||
Continente continente = new Continente(codigo, nombre);
|
||||
listaDeContinentes.add(continente);
|
||||
}
|
||||
|
||||
resultado.close();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar lso continentes: " + e);
|
||||
}
|
||||
return listaDeContinentes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codigo) throws MapaMundiException {
|
||||
Continente continente = null;
|
||||
PreparedStatement sentencia = null;
|
||||
ResultSet resultado = null;
|
||||
|
||||
try {
|
||||
|
||||
sentencia = conexion
|
||||
.prepareStatement("SELECT codigo, nombre_continente FROM T_CONTINENTE WHERE codigo = ?");
|
||||
sentencia.setString(1, codigo);
|
||||
resultado = sentencia.executeQuery();
|
||||
|
||||
if (resultado.next()) {
|
||||
String nombre = resultado.getString("nombre_continente");
|
||||
continente = new Continente(codigo, nombre);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar el continente: " + codigo + "por: " + e);
|
||||
} finally {
|
||||
try {
|
||||
if (resultado != null) {
|
||||
resultado.close();
|
||||
}
|
||||
|
||||
if (sentencia != null) {
|
||||
sentencia.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al cerrar por: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
return continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarContinente(String codigo) throws MapaMundiException {
|
||||
|
||||
try {
|
||||
|
||||
PreparedStatement sentencia = conexion.prepareStatement("DELETE FROM T_CONTINENTE WHERE codigo = ?");
|
||||
sentencia.setString(1, codigo);
|
||||
sentencia.executeUpdate();
|
||||
sentencia.close();
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al borrar el continente: " + codigo + " por: " + e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertarContinente(Continente continente) throws MapaMundiException {
|
||||
|
||||
try {
|
||||
|
||||
PreparedStatement sentencia = conexion
|
||||
.prepareStatement("INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)");
|
||||
sentencia.setString(1, continente.getCodigo());
|
||||
sentencia.setString(2, continente.getNombre());
|
||||
sentencia.executeUpdate();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al insertar el continente: " + continente.getCodigo() + " por: " + e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarContinente(Continente continente) throws MapaMundiException {
|
||||
|
||||
try {
|
||||
|
||||
PreparedStatement sentencia = conexion.prepareStatement("UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?");
|
||||
sentencia.setString(1, continente.getNombre());
|
||||
sentencia.setString(2, continente.getCodigo());
|
||||
sentencia.executeUpdate();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
throw new MapaMundiException("Error al actualizar el continente: " + continente.getCodigo());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package org.palomafp.aadd.rmr.dao.impl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.rmr.excepciones.MapaMundiException;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public class PaisJDBC implements IPaisDAO {
|
||||
|
||||
private Connection conexion;
|
||||
|
||||
public PaisJDBC(Connection conexion) {
|
||||
this.conexion = conexion;
|
||||
}
|
||||
|
||||
public List<Pais> obtenerPaises() throws MapaMundiException {
|
||||
List<Pais> listaDePaises = new ArrayList<>();
|
||||
try {
|
||||
PreparedStatement sentencia = conexion.prepareStatement("SELECT p.identificador, p.nombre_pais, p.capital, c.codigo, c.nombre_continente FROM T_PAIS p JOIN T_CONTINENTE c ON p.cod_continente = c.codigo");
|
||||
ResultSet resultadoConsulta = sentencia.executeQuery();
|
||||
|
||||
while (resultadoConsulta.next()) {
|
||||
int identificador = resultadoConsulta.getInt("identificador");
|
||||
String nombre = resultadoConsulta.getString("nombre_pais");
|
||||
String capital = resultadoConsulta.getString("capital");
|
||||
String codigoContinente = resultadoConsulta.getString("codigo");
|
||||
String nombreContinente = resultadoConsulta.getString("nombre_continente");
|
||||
|
||||
Continente continente = new Continente(codigoContinente, nombreContinente);
|
||||
Pais pais = new Pais(identificador, nombre, capital, continente);
|
||||
listaDePaises.add(pais);
|
||||
|
||||
}
|
||||
resultadoConsulta.close();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar los países", e);
|
||||
}
|
||||
return listaDePaises;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorId(int identificador) throws MapaMundiException{ // no puedo poner que lanza una excepción si no lo he declarado en la interfaz
|
||||
Pais pais = null;
|
||||
PreparedStatement sentencia = null;
|
||||
ResultSet resultado = null;
|
||||
|
||||
try {
|
||||
|
||||
sentencia = conexion.prepareStatement("SELECT p.identificador, p.nombre_pais, p.capital, c.codigo, c.nombre_continente FROM T_PAIS p JOIN T_CONTINENTE c ON p.cod_continente = c.codigo WHERE p.identificador = ?");
|
||||
sentencia.setInt(1, identificador);
|
||||
resultado = sentencia.executeQuery();
|
||||
|
||||
if (resultado.next()) {
|
||||
String nombre = resultado.getString("nombre_pais");
|
||||
String capital = resultado.getString("capital");
|
||||
String codigoContinente = resultado.getString("codigo");
|
||||
String nombreContinente = resultado.getString("nombre_continente");
|
||||
|
||||
// mi clase pais guarda también un objeto continente. antes de crear el país
|
||||
// hay que crear su continente con los dos datos que la consulta trae de t_continente (code + nombre)
|
||||
Continente continente = new Continente (codigoContinente, nombreContinente);
|
||||
pais = new Pais (identificador, nombre, capital, continente); // este id es el que le paso por parámetro
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar el país " + identificador, e);
|
||||
} finally {
|
||||
try {
|
||||
if (resultado != null) {
|
||||
resultado.close();
|
||||
}
|
||||
if (sentencia != null) {
|
||||
sentencia.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al cerrar: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarPais(int identificador) throws MapaMundiException {
|
||||
|
||||
try {
|
||||
|
||||
PreparedStatement sentencia = conexion.prepareStatement("DELETE FROM T_PAIS WHERE identificador = ?");
|
||||
sentencia.setInt(1, identificador);
|
||||
sentencia.executeUpdate();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al borrar el país " + identificador, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertarPais(Pais pais) throws MapaMundiException {
|
||||
|
||||
try {
|
||||
PreparedStatement sentencia = conexion.prepareStatement("INSERT INTO T_PAIS (identificador, nombre_pais, capital, cod_continente) VALUES (?, ?, ?, ?)");
|
||||
|
||||
sentencia.setInt(1, pais.getIdentificador());
|
||||
sentencia.setString(2, pais.getNombre());
|
||||
sentencia.setString(3, pais.getCapital());
|
||||
sentencia.setString(4, pais.getContinente().getCodigo());
|
||||
sentencia.executeUpdate();
|
||||
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al insertar el país " + pais.getIdentificador(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void actualizarPais (Pais pais) throws MapaMundiException {
|
||||
|
||||
try {
|
||||
|
||||
PreparedStatement sentencia = conexion.prepareStatement("UPDATE T_PAIS SET capital = ? WHERE identificador = ?");
|
||||
sentencia.setString(1, pais.getCapital());
|
||||
sentencia.setInt(2, pais.getIdentificador());
|
||||
sentencia.executeUpdate();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al actualizar el país " + pais.getIdentificador(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void borrarPaisesDeUnContinente (String codigoContinente) throws MapaMundiException {
|
||||
|
||||
try {
|
||||
|
||||
PreparedStatement sentencia = conexion.prepareStatement("DELETE FROM T_PAIS WHERE cod_continente = ?");
|
||||
sentencia.setString(1, codigoContinente);
|
||||
sentencia.executeUpdate();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException ("Error al borrar los países del contiennte. " + codigoContinente + " por: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.aadd.rmr.excepciones;
|
||||
|
||||
public class MapaMundiException extends Exception { // hereda de Exception y es checked (obliga a quien llame a un método que la lance a tratarla con try catch)
|
||||
|
||||
public MapaMundiException (String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
public MapaMundiException (String mensaje, Throwable causa) {
|
||||
super (mensaje, causa);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.palomafp.aadd.rmr.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente(String codigo, String nombre) {
|
||||
this.codigo = codigo;
|
||||
this.nombre = 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 - Código: " + codigo + ", Nombre: " + nombre;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.palomafp.aadd.rmr.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais (int identificador, String nombre, String capital, Continente continente) {
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.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,114 @@
|
||||
package org.palomafp.aadd.rmr.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.config.ConexionBD;
|
||||
import org.palomafp.aadd.rmr.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.rmr.dao.impl.ContinenteJDBC;
|
||||
import org.palomafp.aadd.rmr.dao.impl.PaisJDBC;
|
||||
import org.palomafp.aadd.rmr.excepciones.MapaMundiException;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
|
||||
public class ContinenteService {
|
||||
|
||||
public List<Continente> obtenerContoinentes() throws MapaMundiException {
|
||||
List<Continente> listaDeContinentes = null;
|
||||
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||
listaDeContinentes = continenteDAO.obtenerContinentes();
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException ("Error al obtener lso continentes: " + e);
|
||||
}
|
||||
|
||||
return listaDeContinentes;
|
||||
}
|
||||
|
||||
public Continente obtenerContinentePorCodigo (String codigo) throws MapaMundiException {
|
||||
Continente continente = null;
|
||||
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||
continente = continenteDAO.obtenerContinentePorCodigo(codigo);
|
||||
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException ("Error al obtener el continente: " + codigo + " por: " + e);
|
||||
}
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void insertarContinente (Continente continente) throws MapaMundiException {
|
||||
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||
continenteDAO.insertarContinente(continente);
|
||||
|
||||
} catch (IOException | SQLException e) {
|
||||
System.out.println("Error al insertar el contintente: " + continente.getCodigo() + " por: " + e);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void actualizarContinente(Continente continente) throws MapaMundiException {
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||
continenteDAO.actualizarContinente(continente);
|
||||
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException ("Error al actualizar el continente: " + continente.getCodigo() + " por: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public void borrarContinente (String codigo) throws MapaMundiException {
|
||||
|
||||
Connection conexion = null;
|
||||
|
||||
try {
|
||||
|
||||
conexion = ConexionBD.obtenerConexion();
|
||||
conexion.setAutoCommit(false);
|
||||
|
||||
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||
IContinenteDAO continenteDAO = new ContinenteJDBC (conexion);
|
||||
|
||||
paisDAO.borrarPaisesDeUnContinente(codigo);
|
||||
continenteDAO.borrarContinente(codigo);
|
||||
|
||||
conexion.commit();
|
||||
|
||||
} catch (IOException | SQLException | MapaMundiException e) {
|
||||
try {
|
||||
if (conexion != null) {
|
||||
conexion.rollback();
|
||||
}
|
||||
} catch (SQLException s) {
|
||||
System.out.println("Error al deshacer la transacción: " + s.getMessage());
|
||||
}
|
||||
|
||||
throw new MapaMundiException ("Error al borrar el continente: " + codigo + " por: " + e);
|
||||
|
||||
} finally {
|
||||
try {
|
||||
if (conexion != null) {
|
||||
conexion.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al cerrar la conexión: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.palomafp.aadd.rmr.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.config.ConexionBD;
|
||||
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.rmr.dao.impl.PaisJDBC;
|
||||
import org.palomafp.aadd.rmr.excepciones.MapaMundiException;
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public class PaisService {
|
||||
|
||||
public List<Pais> obtenerPaises() throws MapaMundiException {
|
||||
List<Pais> listaPaises = null;
|
||||
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||
listaPaises = paisDAO.obtenerPaises();
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException("Error al obtener los países por: " + e);
|
||||
}
|
||||
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
public Pais obtenerPaisPorId(int identificador) throws MapaMundiException {
|
||||
Pais pais = null;
|
||||
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||
pais = paisDAO.obtenerPaisPorId(identificador);
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException("Error al obtener el país " + identificador + " por: " + e);
|
||||
}
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
public void insertarPais(Pais pais) throws MapaMundiException {
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||
paisDAO.insertarPais(pais);
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException("Error al insertar el país " + pais.getIdentificador() + " por: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public void actualizarPais(Pais pais) throws MapaMundiException {
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||
paisDAO.actualizarPais(pais);
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException("Error al actualizar el país " + pais.getIdentificador() + " por: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public void borrarPais(int identificador) throws MapaMundiException {
|
||||
try (Connection conexion = ConexionBD.obtenerConexion()) {
|
||||
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||
paisDAO.borrarPais(identificador);
|
||||
} catch (IOException | SQLException e) {
|
||||
throw new MapaMundiException("Error al borrar el país " + identificador + " por: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user