primer commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
bin/
|
||||||
|
.settings/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<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>fichero_02_02</groupId>
|
||||||
|
<artifactId>fichero_02_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>18</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package org.palomafp.aadd.aqv;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.aqv.dao.PaisDAO;
|
||||||
|
import org.palomafp.aadd.aqv.dao.impl.PaisDAOImpl;
|
||||||
|
import org.palomafp.aadd.aqv.model.Pais;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AppPricipal {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
PaisDAO paisDAO = new PaisDAOImpl();
|
||||||
|
|
||||||
|
System.out.println("--- 1. Lista de países ---");
|
||||||
|
List<Pais> paises = paisDAO.obtenerTodos();
|
||||||
|
for (Pais p : paises) {
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n--- 2. Consultar país con ID 152 ---");
|
||||||
|
Pais pais152 = paisDAO.obtenerPorId(152);
|
||||||
|
if (pais152 != null) {
|
||||||
|
System.out.println(pais152);
|
||||||
|
} else {
|
||||||
|
System.out.println("No se encontró el país 152.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n--- 3. Borrar el país 152 ---");
|
||||||
|
if (paisDAO.eliminar(152)) {
|
||||||
|
System.out.println("País eliminado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error al eliminar el país.");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n--- 4. Insertar el país consultado ---");
|
||||||
|
if (paisDAO.insertar(pais152)) {
|
||||||
|
System.out.println("País reinsertado correctamente.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error al insertar el país.");
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n--- 5. Actualizar la capital del país ---");
|
||||||
|
pais152.setCapital("Capital city");
|
||||||
|
if (paisDAO.actualizar(pais152)) {
|
||||||
|
System.out.println("Capital actualizada correctamente.");
|
||||||
|
System.out.println("Comprobación: " + paisDAO.obtenerPorId(152));
|
||||||
|
} else {
|
||||||
|
System.out.println("Error al actualizar la capital.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package org.palomafp.aadd.aqv.config;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class ConexionDB {
|
||||||
|
|
||||||
|
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
|
||||||
|
private static final String URL = "jdbc:mysql://192.168.1.36:3306/Mapa_Mundi";
|
||||||
|
private static final String USER = "root";
|
||||||
|
private static final String PASS = "mysql_123";
|
||||||
|
|
||||||
|
public static Connection getConexion() {
|
||||||
|
Connection conexion = null;
|
||||||
|
try {
|
||||||
|
Class.forName(DRIVER);
|
||||||
|
conexion = DriverManager.getConnection(URL, USER, PASS);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.out.println("Error al cargar el driver de MySQL: " + e.getMessage());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error al conectar a la base de datos: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return conexion;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.palomafp.aadd.aqv.dao;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.aqv.model.Pais;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface PaisDAO {
|
||||||
|
List<Pais> obtenerTodos();
|
||||||
|
|
||||||
|
Pais obtenerPorId(int id);
|
||||||
|
|
||||||
|
boolean eliminar(int id);
|
||||||
|
|
||||||
|
boolean insertar(Pais pais);
|
||||||
|
|
||||||
|
boolean actualizar(Pais pais);
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package org.palomafp.aadd.aqv.dao.impl;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.aqv.config.ConexionDB;
|
||||||
|
import org.palomafp.aadd.aqv.dao.PaisDAO;
|
||||||
|
import org.palomafp.aadd.aqv.model.Pais;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PaisDAOImpl implements PaisDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerTodos() {
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
String sql = "SELECT * FROM T_PAIS";
|
||||||
|
try (Connection con = ConexionDB.getConexion();
|
||||||
|
Statement st = con.createStatement();
|
||||||
|
ResultSet rs = st.executeQuery(sql)) {
|
||||||
|
while (rs.next()) {
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setCodContinente(rs.getString("cod_continente"));
|
||||||
|
pais.setIdentificador(rs.getInt("identificador"));
|
||||||
|
pais.setNombrePais(rs.getString("nombre_pais"));
|
||||||
|
pais.setCapital(rs.getString("capital"));
|
||||||
|
paises.add(pais);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error al obtener países: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPorId(int id) {
|
||||||
|
Pais pais = null;
|
||||||
|
String sql = "SELECT * FROM T_PAIS WHERE identificador = " + id;
|
||||||
|
try (Connection con = ConexionDB.getConexion();
|
||||||
|
Statement st = con.createStatement();
|
||||||
|
ResultSet rs = st.executeQuery(sql)) {
|
||||||
|
if (rs.next()) {
|
||||||
|
pais = new Pais();
|
||||||
|
pais.setCodContinente(rs.getString("cod_continente"));
|
||||||
|
pais.setIdentificador(rs.getInt("identificador"));
|
||||||
|
pais.setNombrePais(rs.getString("nombre_pais"));
|
||||||
|
pais.setCapital(rs.getString("capital"));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error al obtener país: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean eliminar(int id) {
|
||||||
|
String sql = "DELETE FROM T_PAIS WHERE identificador = " + id;
|
||||||
|
try (Connection con = ConexionDB.getConexion();
|
||||||
|
Statement st = con.createStatement()) {
|
||||||
|
return st.executeUpdate(sql) > 0;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error al eliminar país: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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 = ConexionDB.getConexion();
|
||||||
|
Statement st = con.createStatement()) {
|
||||||
|
return st.executeUpdate(sql) > 0;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error al insertar país: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean actualizar(Pais pais) {
|
||||||
|
String sql = "UPDATE T_PAIS SET capital = '" + pais.getCapital()
|
||||||
|
+ "' WHERE identificador = " + pais.getIdentificador();
|
||||||
|
try (Connection con = ConexionDB.getConexion();
|
||||||
|
Statement st = con.createStatement()) {
|
||||||
|
return st.executeUpdate(sql) > 0;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error al actualizar país: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.palomafp.aadd.aqv.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombreContinente;
|
||||||
|
|
||||||
|
public Continente() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente(String codigo, String nombreContinente) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombreContinente = nombreContinente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombreContinente() {
|
||||||
|
return nombreContinente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombreContinente(String nombreContinente) {
|
||||||
|
this.nombreContinente = nombreContinente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombreContinente=" + nombreContinente + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.palomafp.aadd.aqv.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private String codContinente;
|
||||||
|
private int identificador;
|
||||||
|
private String nombrePais;
|
||||||
|
private String capital;
|
||||||
|
|
||||||
|
public Pais() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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 [codContinente=" + codContinente + ", identificador=" + identificador + ", nombrePais="
|
||||||
|
+ nombrePais + ", capital=" + capital + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Java-Version: 18
|
||||||
|
Build-Jdk-Spec: 25
|
||||||
|
Created-By: Maven Integration for Eclipse
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#Generated by Maven Integration for Eclipse
|
||||||
|
#Fri Oct 30 16:09:07 CET 2026
|
||||||
|
artifactId=fichero_02_02
|
||||||
|
groupId=fichero_02_02
|
||||||
|
m2e.projectLocation=C\:\\Users\\alvar\\eclipse-workspace\\fichero_02_02
|
||||||
|
m2e.projectName=fichero_02_02
|
||||||
|
version=0.0.1-SNAPSHOT
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<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>fichero_02_02</groupId>
|
||||||
|
<artifactId>fichero_02_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>18</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user