Primer commit conectores 02
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Compilación Java
|
||||||
|
bin/
|
||||||
|
target/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Sistema operativo
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
||||||
|
sgdb.conexion.url=jdbc:mysql://10.100.11.133:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=root
|
||||||
|
bbdd.clave=1234
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<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>
|
||||||
|
<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>26</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.palomafp.aadd.jx;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.jx.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.jx.dao.impl.PaisDaoImpl;
|
||||||
|
import org.palomafp.aadd.jx.model.Pais;
|
||||||
|
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
private static final int ID_PAIS = 152;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IPaisDAO paisDAO = new PaisDaoImpl();
|
||||||
|
|
||||||
|
List<Pais> listaPaises = paisDAO.obtenerListaPaises();
|
||||||
|
for (Pais pais : listaPaises) {
|
||||||
|
System.out.println(pais);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pais paisConsultado = paisDAO.obtenerPaisPorIdentificador(ID_PAIS);
|
||||||
|
if (paisConsultado == null) {
|
||||||
|
System.out.println("No se ha encontrado el país con identificador " + ID_PAIS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println(paisConsultado);
|
||||||
|
|
||||||
|
paisDAO.eliminarPais(ID_PAIS);
|
||||||
|
|
||||||
|
paisDAO.insertarPais(paisConsultado);
|
||||||
|
|
||||||
|
paisDAO.actualizarCapitalPais(ID_PAIS, "Capital city");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.palomafp.aadd.jx;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class GestorPropiedad {
|
||||||
|
|
||||||
|
private static Properties propiedades = new Properties();
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
Path ruta = Paths.get("config", "application.properties");
|
||||||
|
try (BufferedReader entrada = Files.newBufferedReader(ruta, StandardCharsets.ISO_8859_1)) {
|
||||||
|
propiedades.load(entrada);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al cargar el archivo de propiedades: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String get(String clave) {
|
||||||
|
return propiedades.getProperty(clave);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package org.palomafp.aadd.jx.dao;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.palomafp.aadd.jx.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.jx.model.Pais;
|
||||||
|
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
List<Pais> obtenerListaPaises();
|
||||||
|
Pais obtenerPaisPorIdentificador(int identificador);
|
||||||
|
void eliminarPais(int identificador);
|
||||||
|
void insertarPais(Pais nuevoPais);
|
||||||
|
void actualizarCapitalPais(int identificador, String nuevaCapital);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.palomafp.aadd.jx.dao.impl;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.jx.dao.IContinenteDAO;
|
||||||
|
|
||||||
|
public class ContinenteDaoImpl implements IContinenteDAO {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
package org.palomafp.aadd.jx.dao.impl;
|
||||||
|
|
||||||
|
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 org.palomafp.aadd.jx.GestorPropiedad;
|
||||||
|
import org.palomafp.aadd.jx.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.jx.model.Continente;
|
||||||
|
import org.palomafp.aadd.jx.model.Pais;
|
||||||
|
|
||||||
|
|
||||||
|
public class PaisDaoImpl implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String SELECT_PAISES = "SELECT p.identificador, p.nombre_pais, p.capital, "
|
||||||
|
+ "c.codigo, c.nombre_continente "
|
||||||
|
+ "FROM T_PAIS p INNER JOIN T_CONTINENTE c ON p.cod_continente = c.codigo";
|
||||||
|
|
||||||
|
public List<Pais> obtenerListaPaises() {
|
||||||
|
List<Pais> listaPaises = new ArrayList<Pais>();
|
||||||
|
|
||||||
|
try (Connection conexion = obtenerConexion();
|
||||||
|
Statement sentencia = conexion.createStatement();
|
||||||
|
ResultSet resultados = sentencia.executeQuery(SELECT_PAISES);) {
|
||||||
|
while (resultados.next()) {
|
||||||
|
listaPaises.add(crearPais(resultados));
|
||||||
|
}
|
||||||
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais obtenerPaisPorIdentificador(int identificador) {
|
||||||
|
Pais paisEncontrado = null;
|
||||||
|
|
||||||
|
String sentenciaSQL = String.format(SELECT_PAISES + " WHERE p.identificador = %d", identificador);
|
||||||
|
|
||||||
|
try (Connection conexion = obtenerConexion();
|
||||||
|
Statement sentencia = conexion.createStatement();
|
||||||
|
ResultSet resultados = sentencia.executeQuery(sentenciaSQL);) {
|
||||||
|
if (resultados.next()) {
|
||||||
|
paisEncontrado = crearPais(resultados);
|
||||||
|
}
|
||||||
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return paisEncontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eliminarPais(int identificador) {
|
||||||
|
String sentenciaSQL = String.format("DELETE FROM T_PAIS WHERE identificador = %d", identificador);
|
||||||
|
|
||||||
|
try (Connection conexion = obtenerConexion(); Statement sentencia = conexion.createStatement();) {
|
||||||
|
sentencia.execute(sentenciaSQL);
|
||||||
|
System.out.println(sentenciaSQL);
|
||||||
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertarPais(Pais nuevoPais) {
|
||||||
|
String sentenciaSQL = String.format(
|
||||||
|
"INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES ('%s', %d, '%s', '%s')",
|
||||||
|
nuevoPais.getContinente().getCodigo(), nuevoPais.getIdentificador(),
|
||||||
|
escapar(nuevoPais.getNombre()), escapar(nuevoPais.getCapital()));
|
||||||
|
|
||||||
|
try (Connection conexion = obtenerConexion(); Statement sentencia = conexion.createStatement();) {
|
||||||
|
sentencia.execute(sentenciaSQL);
|
||||||
|
System.out.println(sentenciaSQL);
|
||||||
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizarCapitalPais(int identificador, String nuevaCapital) {
|
||||||
|
String sentenciaSQL = String.format("UPDATE T_PAIS SET capital = '%s' WHERE identificador = %d",
|
||||||
|
escapar(nuevaCapital), identificador);
|
||||||
|
|
||||||
|
try (Connection conexion = obtenerConexion(); Statement sentencia = conexion.createStatement();) {
|
||||||
|
sentencia.execute(sentenciaSQL);
|
||||||
|
System.out.println(sentenciaSQL);
|
||||||
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection obtenerConexion() throws ClassNotFoundException, SQLException {
|
||||||
|
Class.forName(GestorPropiedad.get("sgdb.clase.driver"));
|
||||||
|
|
||||||
|
String url = GestorPropiedad.get("sgdb.conexion.url") + GestorPropiedad.get("bbdd.nombre");
|
||||||
|
|
||||||
|
String usuario = GestorPropiedad.get("bbdd.usuario");
|
||||||
|
String clave = GestorPropiedad.get("bbdd.clave");
|
||||||
|
|
||||||
|
return DriverManager.getConnection(url, usuario, clave);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pais crearPais(ResultSet resultados) throws SQLException {
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(resultados.getString("codigo"));
|
||||||
|
continente.setNombre(resultados.getString("nombre_continente"));
|
||||||
|
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(resultados.getInt("identificador"));
|
||||||
|
pais.setNombre(resultados.getString("nombre_pais"));
|
||||||
|
pais.setCapital(resultados.getString("capital"));
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String escapar(String texto) {
|
||||||
|
return texto.replace("'", "''");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package org.palomafp.aadd.jx.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 [Código=" + codigo + ", Nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package org.palomafp.aadd.jx.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String nombreCapital;
|
||||||
|
private 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 nombreCapital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCapital(String capital) {
|
||||||
|
this.nombreCapital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return nombre + " (" + identificador + ") con capital en " + nombreCapital + " pertenece al continente "
|
||||||
|
+ continente.getNombre() + " (" + continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user