Actividad 2.2
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-25">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="config"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="ignore_optional_problems" value="true"/>
|
||||||
|
<attribute name="m2e-apt" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="ignore_optional_problems" value="true"/>
|
||||||
|
<attribute name="m2e-apt" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
/bin/
|
||||||
|
/target/
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Conectores_02</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1793361753717</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.apt.aptEnabled=false
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=25
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=25
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.processAnnotations=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=25
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
# Información relativa a la base de datos
|
||||||
|
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
||||||
|
sgdb.conexion.url=jdbc:mysql://10.100.11.135:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=Alejandro
|
||||||
|
bbdd.clave=Combarro1
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>Conectores_02</groupId>
|
||||||
|
<artifactId>Conectores_02</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<dependencies>
|
||||||
|
<!-- Source:
|
||||||
|
https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<version>26.10.0</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src</sourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
<configuration>
|
||||||
|
<release>25</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -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