Primero
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
|||||||
|
<?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-17">
|
||||||
|
<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="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?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>
|
||||||
|
</projectDescription>
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<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>
|
||||||
|
<!-- Conector de MySQL JDBC -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<version>8.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src</sourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- Cambia esto al número de la versión de Java que tengas instalada (ej: 17, 21, etc.) -->
|
||||||
|
<release>17</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -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<Pais> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Build-Jdk-Spec: 21
|
||||||
|
Created-By: Maven Integration for Eclipse
|
||||||
|
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<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>
|
||||||
|
<!-- Conector de MySQL JDBC -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<version>8.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src</sourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- Cambia esto al número de la versión de Java que tengas instalada (ej: 17, 21, etc.) -->
|
||||||
|
<release>17</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user