Actividad 2.2 CRUD sobre Pais - MapaMundi
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package org.palomafp.aadd.gdo;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
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 static void main(String[] args) {
|
||||
|
||||
try (Connection conexion = ConexionBD.getConexion()) {
|
||||
|
||||
IPaisDAO dao = new PaisJDBC(conexion);
|
||||
|
||||
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 ==");
|
||||
Pais pais = dao.consultarPorId(152);
|
||||
System.out.println(pais);
|
||||
|
||||
System.out.println("\n== 3. Borrado ==");
|
||||
dao.borrar(152);
|
||||
System.out.println("Existe tras borrar: " + (dao.consultarPorId(152) != null));
|
||||
|
||||
System.out.println("\n== 4. Inserción ==");
|
||||
dao.insertar(pais);
|
||||
System.out.println(dao.consultarPorId(152));
|
||||
|
||||
System.out.println("\n== 5. Actualización ==");
|
||||
dao.actualizarCapital(152, "Capital city");
|
||||
System.out.println(dao.consultarPorId(152));
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error de conexión: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.palomafp.aadd.gdo;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ConexionBD {
|
||||
|
||||
private static final String RUTA_CONFIG = "config/application.properties";
|
||||
|
||||
public static Connection getConexion() throws SQLException {
|
||||
Properties props = new Properties();
|
||||
try (FileInputStream fis = new FileInputStream(RUTA_CONFIG)) {
|
||||
props.load(fis);
|
||||
Class.forName(props.getProperty("sgdb.clase.driver"));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
throw new SQLException("Error cargando la configuración: " + e.getMessage(), e);
|
||||
}
|
||||
String url = props.getProperty("sgdb.conexion.url") + props.getProperty("bbdd.nombre");
|
||||
return DriverManager.getConnection(url,
|
||||
props.getProperty("bbdd.usuario"),
|
||||
props.getProperty("bbdd.clave"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.palomafp.aadd.gdo.dao;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.aadd.gdo.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.gdo.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
List<Pais> consultarTodos();
|
||||
Pais consultarPorId(int id);
|
||||
void insertar(Pais pais);
|
||||
void borrar(int id);
|
||||
void actualizarCapital(int id, String capitalNueva);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.palomafp.aadd.gdo.dao.impl;
|
||||
|
||||
public class ContinenteJDBC {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package org.palomafp.aadd.gdo.dao.impl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.gdo.model.Continente;
|
||||
import org.palomafp.aadd.gdo.model.Pais;
|
||||
|
||||
|
||||
public class PaisJDBC implements IPaisDAO {
|
||||
|
||||
private Connection conexion;
|
||||
|
||||
public PaisJDBC(Connection conexion) {
|
||||
this.conexion = conexion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> consultarTodos() {
|
||||
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();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
|
||||
while (rs.next()) {
|
||||
int identificador = rs.getInt("identificador");
|
||||
String nombre = rs.getString("nombre_pais");
|
||||
String capital = rs.getString("capital");
|
||||
String codigoContinente = rs.getString("codigo");
|
||||
String nombreContinente = rs.getString("nombre_continente");
|
||||
|
||||
Continente continente = new Continente(codigoContinente, nombreContinente);
|
||||
paises.add(new Pais(identificador, nombre, capital, continente));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al consultar los países: " + e.getMessage());
|
||||
}
|
||||
return paises;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais consultarPorId(int id) {
|
||||
Pais pais = null;
|
||||
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 "
|
||||
+ "WHERE p.identificador = " + id;
|
||||
|
||||
try (Statement st = conexion.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
|
||||
if (rs.next()) {
|
||||
String nombre = rs.getString("nombre_pais");
|
||||
String capital = rs.getString("capital");
|
||||
String codigoContinente = rs.getString("codigo");
|
||||
String nombreContinente = rs.getString("nombre_continente");
|
||||
|
||||
Continente continente = new Continente(codigoContinente, nombreContinente);
|
||||
pais = new Pais(id, nombre, capital, continente);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al consultar el país: " + e.getMessage());
|
||||
}
|
||||
return pais;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertar(Pais pais) {
|
||||
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES ('"
|
||||
+ pais.getContinente().getCodigo() + "', " + pais.getIdentificador() + ", '"
|
||||
+ pais.getNombre() + "', '" + pais.getCapital() + "')";
|
||||
|
||||
try (Statement st = conexion.createStatement()) {
|
||||
st.executeUpdate(sql);
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al insertar el país: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrar(int id) {
|
||||
String sql = "DELETE FROM T_PAIS WHERE identificador = " + id;
|
||||
|
||||
try (Statement st = conexion.createStatement()) {
|
||||
st.executeUpdate(sql);
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al borrar el país: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarCapital(int id, String capitalNueva) {
|
||||
String sql = "UPDATE T_PAIS SET capital = '" + capitalNueva
|
||||
+ "' WHERE identificador = " + id;
|
||||
|
||||
try (Statement st = conexion.createStatement()) {
|
||||
st.executeUpdate(sql);
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error al actualizar la capital: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.palomafp.aadd.gdo.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.gdo.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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user