Actividad 2.3 CRUD Pais - Continente con DAO + Service + Excepción (MySQL)
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
|
||||||
sgdb.conexion.url=jdbc:mysql://192.168.1.38/
|
sgdb.conexion.url=jdbc:mysql://192.168.1.38/
|
||||||
bbdd.nombre=Mapa_Mundi
|
bbdd.nombre=Mapa_Mundi
|
||||||
bbdd.usuario=root
|
bbdd.usuario=root
|
||||||
|
|||||||
@@ -1,45 +1,46 @@
|
|||||||
package org.palomafp.aadd.gdo;
|
package org.palomafp.aadd.gdo;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import org.palomafp.aadd.gdo.excepciones.MapaMundiException;
|
||||||
import java.sql.SQLException;
|
import org.palomafp.aadd.gdo.model.Continente;
|
||||||
import java.util.List;
|
import org.palomafp.aadd.gdo.service.ContinenteService;
|
||||||
|
|
||||||
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
|
||||||
import org.palomafp.aadd.gdo.dao.impl.PaisJDBC;
|
|
||||||
import org.palomafp.aadd.gdo.model.Pais;
|
|
||||||
|
|
||||||
|
|
||||||
public class AppPrincipal {
|
public class AppPrincipal {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
ContinenteService servicio = new ContinenteService();
|
||||||
|
|
||||||
try (Connection conexion = ConexionBD.getConexion()) {
|
try {
|
||||||
|
System.out.println("=== 1. Lista de continentes ===");
|
||||||
IPaisDAO dao = new PaisJDBC(conexion);
|
for (Continente c : servicio.consultarContinentes()) {
|
||||||
|
System.out.println(c);
|
||||||
System.out.println("== 1. Lista de países ==");
|
|
||||||
List<Pais> paises = dao.consultarTodos();
|
|
||||||
for (Pais p : paises) {
|
|
||||||
System.out.println(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("\n== 2. País 152 ==");
|
System.out.println("\n=== 2. Continente 04 ===");
|
||||||
Pais pais = dao.consultarPorId(152);
|
Continente europa = servicio.consultarContinente("04");
|
||||||
System.out.println(pais);
|
if (europa == null) {
|
||||||
|
System.out.println("No existe el continente 04");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println(europa);
|
||||||
|
|
||||||
System.out.println("\n== 3. Borrado ==");
|
System.out.println("\n=== 3. Borrado del continente 04 y sus países ===");
|
||||||
dao.borrar(152);
|
servicio.borrarContinenteConPaises(europa.getCodigo());
|
||||||
System.out.println("Existe tras borrar: " + (dao.consultarPorId(152) != null));
|
System.out.println("Borrado correcto");
|
||||||
|
|
||||||
System.out.println("\n== 4. Inserción ==");
|
System.out.println("\n=== 4. Inserción del continente ===");
|
||||||
dao.insertar(pais);
|
servicio.insertarContinente(europa);
|
||||||
System.out.println(dao.consultarPorId(152));
|
System.out.println("Insertado: " + europa);
|
||||||
|
|
||||||
System.out.println("\n== 5. Actualización ==");
|
System.out.println("\n=== 5. Actualización del nombre ===");
|
||||||
dao.actualizarCapital(152, "Capital city");
|
Continente actualizado = new Continente(europa.getCodigo(), "Paneuropa");
|
||||||
System.out.println(dao.consultarPorId(152));
|
servicio.actualizarContinente(actualizado);
|
||||||
|
System.out.println("Actualizado: " + servicio.consultarContinente("04"));
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (MapaMundiException e) {
|
||||||
System.err.println("Error de conexión: " + e.getMessage());
|
System.err.println("Error de aplicación: " + e.getMessage());
|
||||||
|
if (e.getCause() != null) {
|
||||||
|
System.err.println("Causa: " + e.getCause().getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ public class ConexionBD {
|
|||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
try (FileInputStream fis = new FileInputStream(RUTA_CONFIG)) {
|
try (FileInputStream fis = new FileInputStream(RUTA_CONFIG)) {
|
||||||
props.load(fis);
|
props.load(fis);
|
||||||
Class.forName(props.getProperty("sgdb.clase.driver"));
|
} catch (IOException e) {
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
|
||||||
throw new SQLException("Error cargando la configuración: " + e.getMessage(), e);
|
throw new SQLException("Error cargando la configuración: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
String url = props.getProperty("sgdb.conexion.url") + props.getProperty("bbdd.nombre");
|
String url = props.getProperty("sgdb.conexion.url") + props.getProperty("bbdd.nombre");
|
||||||
return DriverManager.getConnection(url,
|
return DriverManager.getConnection(url,
|
||||||
props.getProperty("bbdd.usuario"),
|
props.getProperty("bbdd.usuario"),
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
package org.palomafp.aadd.gdo.dao;
|
package org.palomafp.aadd.gdo.dao;
|
||||||
|
|
||||||
public interface IContinenteDAO {
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.gdo.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
List<Continente> consultarTodos() throws MapaMundiException;
|
||||||
|
Continente consultarPorCodigo(String codigo) throws MapaMundiException;
|
||||||
|
void insertar(Continente continente) throws MapaMundiException;
|
||||||
|
void actualizar(Continente continente) throws MapaMundiException;
|
||||||
|
void borrar(String codigo) throws MapaMundiException;
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,14 @@ package org.palomafp.aadd.gdo.dao;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.excepciones.MapaMundiException;
|
||||||
import org.palomafp.aadd.gdo.model.Pais;
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
public interface IPaisDAO {
|
public interface IPaisDAO {
|
||||||
List<Pais> consultarTodos();
|
List<Pais> consultarTodos() throws MapaMundiException;
|
||||||
Pais consultarPorId(int id);
|
Pais consultarPorId(int id) throws MapaMundiException;
|
||||||
void insertar(Pais pais);
|
void insertar(Pais pais) throws MapaMundiException;
|
||||||
void borrar(int id);
|
void borrar(int id) throws MapaMundiException;
|
||||||
void actualizarCapital(int id, String capitalNueva);
|
void borrarPorContinente(String codContinente) throws MapaMundiException;
|
||||||
|
void actualizarCapital(int id, String capitalNueva) throws MapaMundiException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,97 @@
|
|||||||
package org.palomafp.aadd.gdo.dao.impl;
|
package org.palomafp.aadd.gdo.dao.impl;
|
||||||
|
|
||||||
public class ContinenteJDBC {
|
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.gdo.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.gdo.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.gdo.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteJDBC implements IContinenteDAO {
|
||||||
|
|
||||||
|
private Connection conexion;
|
||||||
|
|
||||||
|
public ContinenteJDBC(Connection conexion) {
|
||||||
|
this.conexion = conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> consultarTodos() throws MapaMundiException {
|
||||||
|
List<Continente> continentes = new ArrayList<>();
|
||||||
|
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql);
|
||||||
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
continentes.add(new Continente(rs.getString("codigo"),
|
||||||
|
rs.getString("nombre_continente")));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al consultar los continentes", e);
|
||||||
|
}
|
||||||
|
return continentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente consultarPorCodigo(String codigo) throws MapaMundiException {
|
||||||
|
Continente continente = null;
|
||||||
|
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, codigo);
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
continente = new Continente(rs.getString("codigo"),
|
||||||
|
rs.getString("nombre_continente"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al consultar el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertar(Continente continente) throws MapaMundiException {
|
||||||
|
String sql = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, continente.getCodigo());
|
||||||
|
ps.setString(2, continente.getNombre());
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizar(Continente continente) throws MapaMundiException {
|
||||||
|
String sql = "UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, continente.getNombre());
|
||||||
|
ps.setString(2, continente.getCodigo());
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrar(String codigo) throws MapaMundiException {
|
||||||
|
String sql = "DELETE FROM T_CONTINENTE WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, codigo);
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,19 +1,23 @@
|
|||||||
package org.palomafp.aadd.gdo.dao.impl;
|
package org.palomafp.aadd.gdo.dao.impl;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.gdo.excepciones.MapaMundiException;
|
||||||
import org.palomafp.aadd.gdo.model.Continente;
|
import org.palomafp.aadd.gdo.model.Continente;
|
||||||
import org.palomafp.aadd.gdo.model.Pais;
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
|
|
||||||
public class PaisJDBC implements IPaisDAO {
|
public class PaisJDBC implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String SELECT_BASE =
|
||||||
|
"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";
|
||||||
|
|
||||||
private Connection conexion;
|
private Connection conexion;
|
||||||
|
|
||||||
public PaisJDBC(Connection conexion) {
|
public PaisJDBC(Connection conexion) {
|
||||||
@@ -21,90 +25,100 @@ public class PaisJDBC implements IPaisDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Pais> consultarTodos() {
|
public List<Pais> consultarTodos() throws MapaMundiException {
|
||||||
List<Pais> paises = new ArrayList<>();
|
List<Pais> paises = new ArrayList<>();
|
||||||
String sql = "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";
|
|
||||||
|
|
||||||
try (Statement st = conexion.createStatement();
|
try (PreparedStatement ps = conexion.prepareStatement(SELECT_BASE);
|
||||||
ResultSet rs = st.executeQuery(sql)) {
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
int identificador = rs.getInt("identificador");
|
Continente continente = new Continente(rs.getString("codigo"),
|
||||||
String nombre = rs.getString("nombre_pais");
|
rs.getString("nombre_continente"));
|
||||||
String capital = rs.getString("capital");
|
paises.add(new Pais(rs.getInt("identificador"),
|
||||||
String codigoContinente = rs.getString("codigo");
|
rs.getString("nombre_pais"),
|
||||||
String nombreContinente = rs.getString("nombre_continente");
|
rs.getString("capital"),
|
||||||
|
continente));
|
||||||
Continente continente = new Continente(codigoContinente, nombreContinente);
|
|
||||||
paises.add(new Pais(identificador, nombre, capital, continente));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println("Error al consultar los países: " + e.getMessage());
|
throw new MapaMundiException("Error al consultar los países", e);
|
||||||
}
|
}
|
||||||
return paises;
|
return paises;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pais consultarPorId(int id) {
|
public Pais consultarPorId(int id) throws MapaMundiException {
|
||||||
Pais pais = null;
|
Pais pais = null;
|
||||||
String sql = "SELECT p.identificador, p.nombre_pais, p.capital, c.codigo, c.nombre_continente "
|
String sql = SELECT_BASE + " WHERE p.identificador = ?";
|
||||||
+ "FROM T_PAIS p JOIN T_CONTINENTE c ON p.cod_continente = c.codigo "
|
|
||||||
+ "WHERE p.identificador = " + id;
|
|
||||||
|
|
||||||
try (Statement st = conexion.createStatement();
|
|
||||||
ResultSet rs = st.executeQuery(sql)) {
|
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setInt(1, id);
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
String nombre = rs.getString("nombre_pais");
|
Continente continente = new Continente(rs.getString("codigo"),
|
||||||
String capital = rs.getString("capital");
|
rs.getString("nombre_continente"));
|
||||||
String codigoContinente = rs.getString("codigo");
|
pais = new Pais(id,
|
||||||
String nombreContinente = rs.getString("nombre_continente");
|
rs.getString("nombre_pais"),
|
||||||
|
rs.getString("capital"),
|
||||||
Continente continente = new Continente(codigoContinente, nombreContinente);
|
continente);
|
||||||
pais = new Pais(id, nombre, capital, continente);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println("Error al consultar el país: " + e.getMessage());
|
throw new MapaMundiException("Error al consultar el país " + id, e);
|
||||||
}
|
}
|
||||||
return pais;
|
return pais;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insertar(Pais pais) {
|
public void insertar(Pais pais) throws MapaMundiException {
|
||||||
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES ('"
|
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) "
|
||||||
+ pais.getContinente().getCodigo() + "', " + pais.getIdentificador() + ", '"
|
+ "VALUES (?, ?, ?, ?)";
|
||||||
+ pais.getNombre() + "', '" + pais.getCapital() + "')";
|
|
||||||
|
|
||||||
try (Statement st = conexion.createStatement()) {
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
st.executeUpdate(sql);
|
ps.setString(1, pais.getContinente().getCodigo());
|
||||||
|
ps.setInt(2, pais.getIdentificador());
|
||||||
|
ps.setString(3, pais.getNombre());
|
||||||
|
ps.setString(4, pais.getCapital());
|
||||||
|
ps.executeUpdate();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println("Error al insertar el país: " + e.getMessage());
|
throw new MapaMundiException("Error al insertar el país " + pais.getIdentificador(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void borrar(int id) {
|
public void borrar(int id) throws MapaMundiException {
|
||||||
String sql = "DELETE FROM T_PAIS WHERE identificador = " + id;
|
String sql = "DELETE FROM T_PAIS WHERE identificador = ?";
|
||||||
|
|
||||||
try (Statement st = conexion.createStatement()) {
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
st.executeUpdate(sql);
|
ps.setInt(1, id);
|
||||||
|
ps.executeUpdate();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println("Error al borrar el país: " + e.getMessage());
|
throw new MapaMundiException("Error al borrar el país " + id, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actualizarCapital(int id, String capitalNueva) {
|
public void borrarPorContinente(String codContinente) throws MapaMundiException {
|
||||||
String sql = "UPDATE T_PAIS SET capital = '" + capitalNueva
|
String sql = "DELETE FROM T_PAIS WHERE cod_continente = ?";
|
||||||
+ "' WHERE identificador = " + id;
|
|
||||||
|
|
||||||
try (Statement st = conexion.createStatement()) {
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
st.executeUpdate(sql);
|
ps.setString(1, codContinente);
|
||||||
|
ps.executeUpdate();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.err.println("Error al actualizar la capital: " + e.getMessage());
|
throw new MapaMundiException("Error al borrar los países del continente " + codContinente, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarCapital(int id, String capitalNueva) throws MapaMundiException {
|
||||||
|
String sql = "UPDATE T_PAIS SET capital = ? WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, capitalNueva);
|
||||||
|
ps.setInt(2, id);
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar la capital del país " + id, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.gdo.excepciones;
|
||||||
|
|
||||||
|
public class MapaMundiException extends Exception {
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package org.palomafp.aadd.gdo.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.ConexionBD;
|
||||||
|
import org.palomafp.aadd.gdo.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.gdo.dao.impl.ContinenteJDBC;
|
||||||
|
import org.palomafp.aadd.gdo.dao.impl.PaisJDBC;
|
||||||
|
import org.palomafp.aadd.gdo.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.gdo.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteService {
|
||||||
|
|
||||||
|
public List<Continente> consultarContinentes() throws MapaMundiException {
|
||||||
|
List<Continente> continentes = null;
|
||||||
|
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||||
|
continentes = continenteDAO.consultarTodos();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al consultar los continentes", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return continentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente consultarContinente(String codigo) throws MapaMundiException {
|
||||||
|
Continente continente = null;
|
||||||
|
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||||
|
continente = continenteDAO.consultarPorCodigo(codigo);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al consultar el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||||
|
continenteDAO.insertar(continente);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarContinente(Continente continente) throws MapaMundiException {
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||||
|
continenteDAO.actualizar(continente);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrarContinenteConPaises(String codigo) throws MapaMundiException {
|
||||||
|
Connection conexion = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
conexion = ConexionBD.getConexion();
|
||||||
|
conexion.setAutoCommit(false);
|
||||||
|
|
||||||
|
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteJDBC(conexion);
|
||||||
|
|
||||||
|
paisDAO.borrarPorContinente(codigo);
|
||||||
|
continenteDAO.borrar(codigo);
|
||||||
|
|
||||||
|
conexion.commit();
|
||||||
|
|
||||||
|
} catch (MapaMundiException e) {
|
||||||
|
deshacerTransaccion(conexion);
|
||||||
|
throw e;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
deshacerTransaccion(conexion);
|
||||||
|
throw new MapaMundiException("Error al borrar el continente " + codigo, e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (conexion != null) {
|
||||||
|
conexion.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error al cerrar la conexión: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deshacerTransaccion(Connection conexion) {
|
||||||
|
try {
|
||||||
|
if (conexion != null) {
|
||||||
|
conexion.rollback();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error al deshacer la transacción: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package org.palomafp.aadd.gdo.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.ConexionBD;
|
||||||
|
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.gdo.dao.impl.PaisJDBC;
|
||||||
|
import org.palomafp.aadd.gdo.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
|
public class PaisService {
|
||||||
|
|
||||||
|
public List<Pais> consultarPaises() throws MapaMundiException {
|
||||||
|
List<Pais> paises = null;
|
||||||
|
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||||
|
paises = paisDAO.consultarTodos();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al consultar los países", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais consultarPais(int id) throws MapaMundiException {
|
||||||
|
Pais pais = null;
|
||||||
|
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||||
|
pais = paisDAO.consultarPorId(id);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al consultar el país " + id, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarPais(Pais pais) throws MapaMundiException {
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||||
|
paisDAO.insertar(pais);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar el país " + pais.getIdentificador(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrarPais(int id) throws MapaMundiException {
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||||
|
paisDAO.borrar(id);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar el país " + id, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarCapital(int id, String capitalNueva) throws MapaMundiException {
|
||||||
|
try (Connection conexion = ConexionBD.getConexion()) {
|
||||||
|
IPaisDAO paisDAO = new PaisJDBC(conexion);
|
||||||
|
paisDAO.actualizarCapital(id, capitalNueva);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar la capital del país " + id, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
/org/
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#Generated by Maven Integration for Eclipse
|
#Generated by Maven Integration for Eclipse
|
||||||
#Sat Nov 07 15:46:59 CET 2026
|
#Sat Nov 07 18:58:26 CET 2026
|
||||||
artifactId=aadd_conect02
|
artifactId=aadd_conect02
|
||||||
groupId=aadd_conect02
|
groupId=aadd_conect02
|
||||||
m2e.projectLocation=C\:\\Users\\gonza\\eclipse-workspace\\aadd_conect02
|
m2e.projectLocation=C\:\\Users\\gonza\\eclipse-workspace\\aadd_conect03
|
||||||
m2e.projectName=aadd_conect02
|
m2e.projectName=aadd_conect03
|
||||||
version=0.0.1-SNAPSHOT
|
version=0.0.1-SNAPSHOT
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
|
||||||
sgdb.conexion.url=jdbc:mysql://192.168.1.38/
|
sgdb.conexion.url=jdbc:mysql://192.168.1.38/
|
||||||
bbdd.nombre=Mapa_Mundi
|
bbdd.nombre=Mapa_Mundi
|
||||||
bbdd.usuario=root
|
bbdd.usuario=root
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user