Primero
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package aadd.anp;
|
||||
|
||||
import aadd.anp.dao.model.Continente;
|
||||
import aadd.anp.exception.MapaMundiException;
|
||||
import aadd.anp.service.ContinenteService;
|
||||
import java.util.List;
|
||||
|
||||
public class Principal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ContinenteService continenteService = new ContinenteService();
|
||||
|
||||
try {
|
||||
System.out.println("==================================================");
|
||||
System.out.println(" 1. CONSULTAR Y LISTAR TODOS LOS CONTINENTES");
|
||||
System.out.println("==================================================");
|
||||
List<Continente> lista = continenteService.listarContinentes();
|
||||
for (Continente c : lista) {
|
||||
System.out.println(" - Código: " + c.getNombreContinente() + " | Nombre: " + c.getNombreContinente());
|
||||
}
|
||||
|
||||
System.out.println("\n==================================================");
|
||||
System.out.println(" 2. CONSULTAR EL CONTINENTE CON CÓDIGO '04'");
|
||||
System.out.println("==================================================");
|
||||
Continente continente04 = continenteService.obtenerContinentePorCodigo("04");
|
||||
if (continente04 != null) {
|
||||
System.out.println(" ¡Encontrado! -> Código: " + continente04.getNombreContinente() + " | Nombre: " + continente04.getNombreContinente());
|
||||
} else {
|
||||
System.out.println("⚠️ No existe el continente con código '04'.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("\n==================================================");
|
||||
System.out.println(" 3. BORRAR CONTINENTE Y SUS PAÍSES (En Transacción)");
|
||||
System.out.println("==================================================");
|
||||
continenteService.borrarContinente("04");
|
||||
System.out.println(" Éxito: El continente '04' y sus países asociados han sido borrados de forma atómica.");
|
||||
|
||||
System.out.println("\n==================================================");
|
||||
System.out.println(" 4. INSERTAR NUEVAMENTE EL CONTINENTE (del punto 2)");
|
||||
System.out.println("==================================================");
|
||||
boolean insertado = continenteService.insertarContinente(continente04);
|
||||
if (insertado) {
|
||||
System.out.println(" Éxito: El continente '04' (" + continente04.getNombreContinente() + ") ha sido reinsertado.");
|
||||
}
|
||||
|
||||
System.out.println("\n==================================================");
|
||||
System.out.println(" 5. ACTUALIZAR NOMBRE DEL CONTINENTE A 'Paneuropa'");
|
||||
System.out.println("==================================================");
|
||||
boolean actualizado = continenteService.actualizarNombreContinente("04", "Paneuropa");
|
||||
if (actualizado) {
|
||||
System.out.println(" Éxito: El continente '04' ahora se llama 'Paneuropa'.");
|
||||
}
|
||||
|
||||
System.out.println("\n Estado final del continente '04':");
|
||||
Continente finalContinente = continenteService.obtenerContinentePorCodigo("04");
|
||||
System.out.println(" - Código: " + finalContinente.getNombreContinente() + " | Nombre: " + finalContinente.getNombreContinente());
|
||||
|
||||
} catch (MapaMundiException e) {
|
||||
System.err.println("\n❌ ERROR DE APLICACIÓN: " + e.getMessage());
|
||||
if (e.getCause() != null) {
|
||||
System.err.println("Causa original: " + e.getCause().getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package aadd.anp.conexion;
|
||||
import java.sql.*;
|
||||
|
||||
public class Conexion {
|
||||
private static final String URL = "jdbc:mysql://10.100.11.150/Mapa_Mundi";
|
||||
private static final String USUARIO = "root";
|
||||
private static final String PASSWORD = "1234";
|
||||
|
||||
/*
|
||||
* 10.100.11.150
|
||||
* localhost:3306
|
||||
*/
|
||||
|
||||
public static Connection obtenerConexion() throws SQLException {
|
||||
return DriverManager.getConnection(URL, USUARIO, PASSWORD);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package aadd.anp.dao.impl;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import aadd.anp.dao.model.*;
|
||||
import aadd.anp.conexion.*;
|
||||
import aadd.anp.exception.MapaMundiException;
|
||||
|
||||
public class ContinenteDao {
|
||||
|
||||
public List<Continente> obtenerTodos() throws MapaMundiException {
|
||||
List<Continente> listaContinentes = new ArrayList<>();
|
||||
// Modificado cod_continente a codigo, y nombre_continente según corresponda
|
||||
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE";
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
PreparedStatement pstmt = con.prepareStatement(sql);
|
||||
ResultSet rs = pstmt.executeQuery()) {
|
||||
|
||||
while (rs.next()) {
|
||||
Continente c = new Continente(
|
||||
rs.getString("codigo"),
|
||||
rs.getString("nombre_continente")
|
||||
);
|
||||
listaContinentes.add(c);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al consultar los continentes", e);
|
||||
}
|
||||
return listaContinentes;
|
||||
}
|
||||
|
||||
public Continente obtenerPorCodigo(String codContinente) throws MapaMundiException {
|
||||
Continente continente = null;
|
||||
// Modificado cod_continente a codigo
|
||||
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE WHERE codigo = ?";
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||
|
||||
pstmt.setString(1, codContinente);
|
||||
|
||||
try (ResultSet rs = pstmt.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 con código " + codContinente, e);
|
||||
}
|
||||
return continente;
|
||||
}
|
||||
|
||||
public boolean borrar(String codContinente) throws MapaMundiException {
|
||||
// Modificado cod_continente a codigo
|
||||
String sql = "DELETE FROM T_CONTINENTE WHERE codigo = ?";
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||
|
||||
pstmt.setString(1, codContinente);
|
||||
int filas = pstmt.executeUpdate();
|
||||
return filas > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al borrar el continente", e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean insertar(Continente continente) throws MapaMundiException {
|
||||
// Modificado cod_continente a codigo en la sentencia SQL
|
||||
String sql = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)";
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||
|
||||
// CORREGIDO: El primer parámetro debe ser el código y el segundo el nombre
|
||||
pstmt.setString(1, continente.getCodigo()); // O el método que tenga tu modelo para obtener el código
|
||||
pstmt.setString(2, continente.getNombreContinente());
|
||||
|
||||
int filas = pstmt.executeUpdate();
|
||||
return filas > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al insertar el continente", e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean actualizarNombre(String codContinente, String nuevoNombre) throws MapaMundiException {
|
||||
// CORREGIDO: Se cambia nombre_nombre por nombre_continente y cod_continente por codigo
|
||||
String sql = "UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?";
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||
|
||||
pstmt.setString(1, nuevoNombre);
|
||||
pstmt.setString(2, codContinente);
|
||||
|
||||
int filas = pstmt.executeUpdate();
|
||||
return filas > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new MapaMundiException("Error al actualizar el nombre del continente", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package aadd.anp.dao.impl;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import aadd.anp.dao.model.*;
|
||||
import aadd.anp.conexion.*;
|
||||
|
||||
|
||||
public class PaisDao {
|
||||
|
||||
public List<Pais> obtenerTodos() {
|
||||
List<Pais> listaPaises = new ArrayList<>();
|
||||
String sql = "SELECT cod_continente, identificador, nombre_pais, capital FROM T_PAIS";
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
Statement stmt = con.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
|
||||
while (rs.next()) {
|
||||
Pais p = new Pais(
|
||||
rs.getString("cod_continente"),
|
||||
rs.getInt("identificador"),
|
||||
rs.getString("nombre_pais"),
|
||||
rs.getString("capital")
|
||||
);
|
||||
listaPaises.add(p);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al consultar países: " + e.getMessage());
|
||||
}
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
|
||||
public Pais obtenerPorId(int identificador) {
|
||||
Pais pais = null;
|
||||
String sql = "SELECT cod_continente, identificador, nombre_pais, capital FROM T_PAIS WHERE identificador = " + identificador;
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
Statement stmt = con.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
|
||||
if (rs.next()) {
|
||||
pais = new Pais(
|
||||
rs.getString("cod_continente"),
|
||||
rs.getInt("identificador"),
|
||||
rs.getString("nombre_pais"),
|
||||
rs.getString("capital")
|
||||
);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al consultar el país con ID " + identificador + ": " + e.getMessage());
|
||||
}
|
||||
return pais;
|
||||
}
|
||||
|
||||
|
||||
public boolean borrar(int identificador) {
|
||||
String sql = "DELETE FROM T_PAIS WHERE identificador = " + identificador;
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
Statement stmt = con.createStatement()) {
|
||||
|
||||
int filas = stmt.executeUpdate(sql);
|
||||
return filas > 0;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al borrar el país: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean insertar(Pais pais) {
|
||||
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES ('"
|
||||
+ pais.getCodContinente() + "', "
|
||||
+ pais.getIdentificador() + ", '"
|
||||
+ pais.getNombrePais() + "', '"
|
||||
+ pais.getCapital() + "')";
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
Statement stmt = con.createStatement()) {
|
||||
|
||||
int filas = stmt.executeUpdate(sql);
|
||||
return filas > 0;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al insertar el país: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean actualizarCapital(int identificador, String nuevaCapital) {
|
||||
String sql = "UPDATE T_PAIS SET capital = '" + nuevaCapital + "' WHERE identificador = " + identificador;
|
||||
|
||||
try (Connection con = Conexion.obtenerConexion();
|
||||
Statement stmt = con.createStatement()) {
|
||||
|
||||
int filas = stmt.executeUpdate(sql);
|
||||
return filas > 0;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al actualizar la capital: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package aadd.anp.dao.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombreContinente;
|
||||
|
||||
public Continente(String codigo, String nombre_continente) {
|
||||
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 nombre_continente) {
|
||||
this.nombreContinente = nombreContinente;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package aadd.anp.dao.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private String codContinente;
|
||||
private int identificador;
|
||||
private String nombrePais;
|
||||
private String capital;
|
||||
|
||||
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 [ID=" + identificador + ", Continente=" + codContinente + ", Nombre=" + nombrePais + ", Capital=" + capital + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package aadd.anp.exception;
|
||||
|
||||
public class MapaMundiException extends Exception {
|
||||
|
||||
public MapaMundiException(String mensaje) {
|
||||
super(mensaje);
|
||||
}
|
||||
|
||||
public MapaMundiException(String mensaje, Throwable cause) {
|
||||
super(mensaje, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package aadd.anp.service;
|
||||
|
||||
import java.util.List;
|
||||
import aadd.anp.dao.impl.ContinenteDao; //
|
||||
import aadd.anp.dao.model.Continente;
|
||||
import aadd.anp.exception.MapaMundiException;
|
||||
|
||||
public class ContinenteService {
|
||||
|
||||
private ContinenteDao continenteDao;
|
||||
|
||||
public ContinenteService() {
|
||||
this.continenteDao = new ContinenteDao();
|
||||
}
|
||||
|
||||
public List<Continente> listarContinentes() throws MapaMundiException {
|
||||
return continenteDao.obtenerTodos();
|
||||
}
|
||||
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) throws MapaMundiException {
|
||||
if (codContinente == null || codContinente.trim().isEmpty()) {
|
||||
throw new MapaMundiException("El código de continente no puede estar vacío.");
|
||||
}
|
||||
Continente continente = continenteDao.obtenerPorCodigo(codContinente);
|
||||
if (continente == null) {
|
||||
throw new MapaMundiException("No se encontró ningún continente con el código: " + codContinente);
|
||||
}
|
||||
return continente;
|
||||
}
|
||||
|
||||
public boolean insertarContinente(Continente continente) throws MapaMundiException {
|
||||
if (continente == null) {
|
||||
throw new MapaMundiException("El objeto continente no puede ser nulo.");
|
||||
}
|
||||
if (continente.getNombreContinente() == null || continente.getNombreContinente().trim().isEmpty()) {
|
||||
throw new MapaMundiException("El código del continente es obligatorio.");
|
||||
}
|
||||
if (continente.getNombreContinente() == null || continente.getNombreContinente().trim().isEmpty()) {
|
||||
throw new MapaMundiException("El nombre del continente es obligatorio.");
|
||||
}
|
||||
|
||||
return continenteDao.insertar(continente);
|
||||
}
|
||||
|
||||
public boolean borrarContinente(String codContinente) throws MapaMundiException {
|
||||
if (codContinente == null || codContinente.trim().isEmpty()) {
|
||||
throw new MapaMundiException("Código de continente no válido.");
|
||||
}
|
||||
boolean borrado = continenteDao.borrar(codContinente);
|
||||
if (!borrado) {
|
||||
throw new MapaMundiException("No se pudo borrar el continente con código " + codContinente);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean actualizarNombreContinente(String codContinente, String nuevoNombre) throws MapaMundiException {
|
||||
if (codContinente == null || codContinente.trim().isEmpty()) {
|
||||
throw new MapaMundiException("Código de continente no válido.");
|
||||
}
|
||||
if (nuevoNombre == null || nuevoNombre.trim().isEmpty()) {
|
||||
throw new MapaMundiException("El nuevo nombre del continente no puede estar vacío.");
|
||||
}
|
||||
return continenteDao.actualizarNombre(codContinente, nuevoNombre);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package aadd.anp.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import aadd.anp.dao.impl.PaisDao;
|
||||
import aadd.anp.dao.model.Pais;
|
||||
import aadd.anp.exception.MapaMundiException;
|
||||
|
||||
public class PaisService {
|
||||
|
||||
|
||||
private PaisDao paisDao;
|
||||
|
||||
public PaisService() {
|
||||
this.paisDao = new PaisDao();
|
||||
}
|
||||
|
||||
public List<Pais> listarPaises() throws MapaMundiException {
|
||||
return paisDao.obtenerTodos();
|
||||
}
|
||||
|
||||
public Pais obtenerPaisPorId(int identificador) throws MapaMundiException {
|
||||
if (identificador <= 0) {
|
||||
throw new MapaMundiException("El identificador del país debe ser mayor que 0.");
|
||||
}
|
||||
Pais pais = paisDao.obtenerPorId(identificador);
|
||||
if (pais == null) {
|
||||
throw new MapaMundiException("No se encontró ningún país con el ID: " + identificador);
|
||||
}
|
||||
return pais;
|
||||
}
|
||||
|
||||
public boolean insertarPais(Pais pais) throws MapaMundiException {
|
||||
if (pais == null) {
|
||||
throw new MapaMundiException("El objeto país no puede ser nulo.");
|
||||
}
|
||||
if (pais.getNombrePais() == null || pais.getNombrePais().trim().isEmpty()) {
|
||||
throw new MapaMundiException("El nombre del país es obligatorio.");
|
||||
}
|
||||
if (pais.getCodContinente() == null || pais.getCodContinente().trim().isEmpty()) {
|
||||
throw new MapaMundiException("El código de continente es obligatorio.");
|
||||
}
|
||||
|
||||
return paisDao.insertar(pais);
|
||||
}
|
||||
|
||||
public boolean borrarPais(int identificador) throws MapaMundiException {
|
||||
if (identificador <= 0) {
|
||||
throw new MapaMundiException("ID de país no válido.");
|
||||
}
|
||||
boolean borrado = paisDao.borrar(identificador);
|
||||
if (!borrado) {
|
||||
throw new MapaMundiException("No se pudo borrar el país con ID " + identificador + " (quizás no exista).");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean actualizarCapital(int identificador, String nuevaCapital) throws MapaMundiException {
|
||||
if (identificador <= 0) {
|
||||
throw new MapaMundiException("ID de país no válido.");
|
||||
}
|
||||
if (nuevaCapital == null || nuevaCapital.trim().isEmpty()) {
|
||||
throw new MapaMundiException("La nueva capital no puede estar vacía.");
|
||||
}
|
||||
return paisDao.actualizarCapital(identificador, nuevaCapital);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user