commit 6135c1ae178e86c11e18ddae055189b066150d64 Author: Avelino Date: Sun Nov 1 19:20:06 2026 +0100 Primero diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..9c7983f --- /dev/null +++ b/.classpath @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..4249269 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + Conectores_02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ee93af1 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +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.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c83bdec --- /dev/null +++ b/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + Conectores_02 + Conectores_02 + 0.0.1-SNAPSHOT + + + + + com.mysql + mysql-connector-j + 8.3.0 + + + + + src + + + maven-compiler-plugin + 3.13.0 + + + 17 + + + + + \ No newline at end of file diff --git a/src/aadd/anp/Principal.java b/src/aadd/anp/Principal.java new file mode 100644 index 0000000..8eea387 --- /dev/null +++ b/src/aadd/anp/Principal.java @@ -0,0 +1,52 @@ +package aadd.anp; +import aadd.anp.dao.model.Pais; +import aadd.anp.dao.impl.PaisDao; +import java.util.List; + +public class Principal { + public static void main(String[] args) { + + + PaisDao paisDao = new PaisDao(); + + System.out.println("=== 1. LISTA DE TODOS LOS PAÍSES ==="); + List listaPaises = paisDao.obtenerTodos(); + for (Pais p : listaPaises) { + System.out.println(p); + } + + System.out.println("\n=== 2. CONSULTA DEL PAÍS CON IDENTIFICADOR 152 ==="); + Pais pais152 = paisDao.obtenerPorId(152); + if (pais152 != null) { + System.out.println(pais152); + } else { + System.out.println("No se encontró el país con identificador 152."); + } + + System.out.println("\n=== 3. BORRANDO EL PAÍS CON IDENTIFICADOR 152 ==="); + if (pais152 != null) { + boolean borrado = paisDao.borrar(pais152.getIdentificador()); + if (borrado) { + System.out.println("-> País con ID 152 borrado correctamente."); + } + } + + System.out.println("\n=== 4. INSERTANDO NUEVAMENTE EL PAÍS 152 A PARTIR DEL OBJETO ==="); + if (pais152 != null) { + boolean insertado = paisDao.insertar(pais152); + if (insertado) { + System.out.println("-> País 152 insertado nuevamente con éxito."); + } + } + + System.out.println("\n=== 5. ACTUALIZANDO LA CAPITAL DEL PAÍS 152 A 'Capital city' ==="); + if (pais152 != null) { + boolean actualizado = paisDao.actualizarCapital(pais152.getIdentificador(), "Capital city"); + if (actualizado) { + System.out.println("-> Capital actualizada correctamente."); + Pais paisActualizado = paisDao.obtenerPorId(152); + System.out.println("Estado final del país: " + paisActualizado); + } + } + } +} \ No newline at end of file diff --git a/src/aadd/anp/conexion/Conexion.java b/src/aadd/anp/conexion/Conexion.java new file mode 100644 index 0000000..88c6c80 --- /dev/null +++ b/src/aadd/anp/conexion/Conexion.java @@ -0,0 +1,12 @@ +package aadd.anp.conexion; +import java.sql.*; + +public class Conexion { + private static final String URL = "jdbc:mysql://localhost:3306/mapa_mundi"; + private static final String USUARIO = "root"; + private static final String PASSWORD = "1234"; + + public static Connection obtenerConexion() throws SQLException { + return DriverManager.getConnection(URL, USUARIO, PASSWORD); + } +} \ No newline at end of file diff --git a/src/aadd/anp/dao/impl/PaisDao.java b/src/aadd/anp/dao/impl/PaisDao.java new file mode 100644 index 0000000..7ee5db0 --- /dev/null +++ b/src/aadd/anp/dao/impl/PaisDao.java @@ -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 obtenerTodos() { + List 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; + } + } +} diff --git a/src/aadd/anp/dao/model/Pais.java b/src/aadd/anp/dao/model/Pais.java new file mode 100644 index 0000000..5c193ba --- /dev/null +++ b/src/aadd/anp/dao/model/Pais.java @@ -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 + "]"; + } +} diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..9bdf3b1 --- /dev/null +++ b/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Build-Jdk-Spec: 21 +Created-By: Maven Integration for Eclipse + diff --git a/target/classes/META-INF/maven/Conectores_02/Conectores_02/pom.properties b/target/classes/META-INF/maven/Conectores_02/Conectores_02/pom.properties new file mode 100644 index 0000000..dbbffc8 --- /dev/null +++ b/target/classes/META-INF/maven/Conectores_02/Conectores_02/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Sun Nov 01 19:19:48 CET 2026 +artifactId=Conectores_02 +groupId=Conectores_02 +m2e.projectLocation=C\:\\Users\\PC GAMING\\eclipse-workspace\\Conectores_02 +m2e.projectName=Conectores_02 +version=0.0.1-SNAPSHOT diff --git a/target/classes/META-INF/maven/Conectores_02/Conectores_02/pom.xml b/target/classes/META-INF/maven/Conectores_02/Conectores_02/pom.xml new file mode 100644 index 0000000..c83bdec --- /dev/null +++ b/target/classes/META-INF/maven/Conectores_02/Conectores_02/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + Conectores_02 + Conectores_02 + 0.0.1-SNAPSHOT + + + + + com.mysql + mysql-connector-j + 8.3.0 + + + + + src + + + maven-compiler-plugin + 3.13.0 + + + 17 + + + + + \ No newline at end of file diff --git a/target/classes/aadd/anp/Principal.class b/target/classes/aadd/anp/Principal.class new file mode 100644 index 0000000..10f5ebc Binary files /dev/null and b/target/classes/aadd/anp/Principal.class differ diff --git a/target/classes/aadd/anp/conexion/Conexion.class b/target/classes/aadd/anp/conexion/Conexion.class new file mode 100644 index 0000000..e73b61b Binary files /dev/null and b/target/classes/aadd/anp/conexion/Conexion.class differ diff --git a/target/classes/aadd/anp/dao/impl/PaisDao.class b/target/classes/aadd/anp/dao/impl/PaisDao.class new file mode 100644 index 0000000..e8d0813 Binary files /dev/null and b/target/classes/aadd/anp/dao/impl/PaisDao.class differ diff --git a/target/classes/aadd/anp/dao/model/Pais.class b/target/classes/aadd/anp/dao/model/Pais.class new file mode 100644 index 0000000..4c67e60 Binary files /dev/null and b/target/classes/aadd/anp/dao/model/Pais.class differ