This commit is contained in:
avelino
2026-11-03 18:48:17 +01:00
commit 6c5a817923
26 changed files with 627 additions and 0 deletions
+109
View File
@@ -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);
}
}
}
+102
View File
@@ -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;
}
}
}
+27
View File
@@ -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;
}
}
+33
View File
@@ -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 + "]";
}
}