Actividad 2.2
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package org.palomafp.aadd.aez;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.aez.dao.impl.PaisDaoJDBC;
|
||||
import org.palomafp.aadd.aez.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
IPaisDAO paisDAO = new PaisDaoJDBC();
|
||||
|
||||
// 1. Consultar la lista de países e imprimir por consola
|
||||
System.out.println("1. CONSULTA DE TODOS LOS PAÍSES");
|
||||
List<Pais> paises = paisDAO.consultarTodos();
|
||||
paises.forEach(System.out::println);
|
||||
|
||||
// 2. Consultar el país con identificador 152 e imprimir por consola
|
||||
System.out.println(" 2. CONSULTA PAÍS CON ID 152");
|
||||
Pais pais152 = paisDAO.consultarPorId(152);
|
||||
System.out.println("País consultado: " + pais152);
|
||||
|
||||
if (pais152 != null) {
|
||||
// 3. Borrar el país consultado
|
||||
System.out.println("3. BORRADO DEL PAÍS ID 152");
|
||||
boolean borrado = paisDAO.borrar(152);
|
||||
System.out.println("¿Borrado correcto?: " + borrado);
|
||||
System.out.println("Comprobación tras borrado: " + paisDAO.consultarPorId(152));
|
||||
|
||||
// 4. Insertar el país a partir de la consulta del punto 2
|
||||
System.out.println(" 4. REINSERTAR PAÍS CONSULTADO EN PUNTO 2");
|
||||
boolean insertado = paisDAO.insertar(pais152);
|
||||
System.out.println("¿Inserción correcta?: " + insertado);
|
||||
System.out.println("Comprobación tras inserción: " + paisDAO.consultarPorId(152));
|
||||
|
||||
// 5. Actualizar la capital del país a "Capital city"
|
||||
System.out.println("5. ACTUALIZAR CAPITAL A 'Capital city'");
|
||||
boolean actualizado = paisDAO.actualizarCapital(152, "Capital city");
|
||||
System.out.println("¿Actualización correcta?: " + actualizado);
|
||||
System.out.println("Estado final en BD: " + paisDAO.consultarPorId(152));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.palomafp.aadd.aez.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.aez.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
List<Pais> consultarTodos();
|
||||
|
||||
Pais consultarPorId(int id);
|
||||
|
||||
boolean borrar(int id);
|
||||
|
||||
boolean insertar(Pais pais);
|
||||
|
||||
boolean actualizarCapital(int id, String nuevaCapital);
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package org.palomafp.aadd.aez.dao.impl;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.palomafp.aadd.aez.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.aez.model.Continente;
|
||||
import org.palomafp.aadd.aez.model.Pais;
|
||||
|
||||
public class PaisDaoJDBC implements IPaisDAO {
|
||||
|
||||
private Connection obtenerConexion() throws SQLException {
|
||||
Properties prop = new Properties();
|
||||
try (InputStream is = new FileInputStream("config/application.properties")) {
|
||||
prop.load(is);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error al cargar la configuración: " + e.getMessage());
|
||||
}
|
||||
|
||||
String driver = prop.getProperty("sgdb.clase.driver");
|
||||
String urlBase = prop.getProperty("sgdb.conexion.url");
|
||||
String bbdd = prop.getProperty("bbdd.nombre");
|
||||
String usuario = prop.getProperty("bbdd.usuario");
|
||||
String clave = prop.getProperty("bbdd.clave");
|
||||
|
||||
try {
|
||||
Class.forName(driver);
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.err.println("Driver JDBC no encontrado: " + e.getMessage());
|
||||
}
|
||||
|
||||
String urlCompleta = urlBase + bbdd + "?useSSL=false&allowPublicKeyRetrieval=true";
|
||||
return DriverManager.getConnection(urlCompleta, usuario, clave);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> consultarTodos() {
|
||||
List<Pais> lista = new ArrayList<>();
|
||||
String sql = "SELECT p.identificador, p.nombre_pais, p.capital, p.cod_continente, c.nombre_continente "
|
||||
+ "FROM T_PAIS p LEFT JOIN T_CONTINENTE c ON p.cod_continente = c.codigo";
|
||||
|
||||
try (Connection conn = obtenerConexion();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
|
||||
while (rs.next()) {
|
||||
Continente cont = new Continente(rs.getString("cod_continente"), rs.getString("nombre_continente"));
|
||||
Pais pais = new Pais(rs.getInt("identificador"), rs.getString("nombre_pais"), rs.getString("capital"),
|
||||
cont);
|
||||
lista.add(pais);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al consultar la lista de países: " + e.getMessage());
|
||||
}
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais consultarPorId(int id) {
|
||||
Pais pais = null;
|
||||
String sql = "SELECT p.identificador, p.nombre_pais, p.capital, p.cod_continente, c.nombre_continente "
|
||||
+ "FROM T_PAIS p LEFT JOIN T_CONTINENTE c ON p.cod_continente = c.codigo " + "WHERE p.identificador = "
|
||||
+ id;
|
||||
|
||||
try (Connection conn = obtenerConexion();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
|
||||
if (rs.next()) {
|
||||
Continente cont = new Continente(rs.getString("cod_continente"), rs.getString("nombre_continente"));
|
||||
pais = new Pais(rs.getInt("identificador"), rs.getString("nombre_pais"), rs.getString("capital"), cont);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al consultar el país " + id + ": " + e.getMessage());
|
||||
}
|
||||
return pais;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean borrar(int id) {
|
||||
String sql = "DELETE FROM T_PAIS WHERE identificador = " + id;
|
||||
|
||||
try (Connection conn = obtenerConexion(); Statement stmt = conn.createStatement()) {
|
||||
|
||||
int filasAfectadas = stmt.executeUpdate(sql);
|
||||
return filasAfectadas > 0;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al borrar el país " + id + ": " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertar(Pais pais) {
|
||||
if (pais == null || pais.getContinente() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO T_PAIS (identificador, cod_continente, nombre_pais, capital) VALUES ("
|
||||
+ pais.getIdentificador() + ", '" + pais.getContinente().getCodigo() + "', '" + pais.getNombrePais()
|
||||
+ "', '" + pais.getCapital() + "')";
|
||||
|
||||
try (Connection conn = obtenerConexion(); Statement stmt = conn.createStatement()) {
|
||||
|
||||
int filasAfectadas = stmt.executeUpdate(sql);
|
||||
return filasAfectadas > 0;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al insertar el país: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizarCapital(int id, String nuevaCapital) {
|
||||
String sql = "UPDATE T_PAIS SET capital = '" + nuevaCapital + "' WHERE identificador = " + id;
|
||||
|
||||
try (Connection conn = obtenerConexion(); Statement stmt = conn.createStatement()) {
|
||||
|
||||
int filasAfectadas = stmt.executeUpdate(sql);
|
||||
return filasAfectadas > 0;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al actualizar la capital del país " + id + ": " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.palomafp.aadd.aez.model;
|
||||
|
||||
public class Continente {
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
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 [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.palomafp.aadd.aez.model;
|
||||
|
||||
public class Pais {
|
||||
private int identificador;
|
||||
private String nombrePais;
|
||||
private String capital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais() {
|
||||
}
|
||||
|
||||
public Pais(int identificador, String nombrePais, String capital, Continente continente) {
|
||||
this.identificador = identificador;
|
||||
this.nombrePais = nombrePais;
|
||||
this.capital = capital;
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais [identificador=" + identificador + ", nombre=" + nombrePais + ", capital=" + capital
|
||||
+ ", continente=" + continente + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user