Primer commit
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
# Compilados de Java
|
||||
*.class
|
||||
bin/
|
||||
|
||||
# Compilados de Maven
|
||||
target/
|
||||
|
||||
# Configuración de Eclipse
|
||||
.project
|
||||
.classpath
|
||||
.settings/
|
||||
@@ -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>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>21</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,5 @@
|
||||
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
||||
sgdb.conexion.url=jdbc:mysql://localhost:3306/
|
||||
bbdd.nombre=Mapa_Mundi
|
||||
bbdd.usuario=root
|
||||
bbdd.clave=root1234
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.palomafp.aadd.smmb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.smmb.dao.impl.PaisDaoMySql;
|
||||
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
// Aquí irian todas las interfaces
|
||||
private final IPaisDAO paisDao;
|
||||
|
||||
// Recibimos la configuración ya cargada y la asignamos
|
||||
public GestorMapaMundi(GestorConfiguracion configuracion) {
|
||||
this.paisDao = new PaisDaoMySql(configuracion);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
|
||||
GestorConfiguracion configuracion = new GestorConfiguracion("application.properties");
|
||||
GestorMapaMundi gmm = new GestorMapaMundi(configuracion);
|
||||
|
||||
gmm.ejecutarPasos();
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error de configuración: " + e.getMessage());
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error de base de datos: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ejecutarPasos() throws SQLException {
|
||||
|
||||
System.out.println("1. Lista de países:");
|
||||
List<Pais> paises = paisDao.obtenerPaises();
|
||||
|
||||
for (Pais p : paises) {
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
System.out.println("2. País con identificador 152:");
|
||||
Pais espana = paisDao.obtenerPaisPorID(152);
|
||||
|
||||
if (espana == null) {
|
||||
System.out.println("No existe el país 152");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println(espana);
|
||||
|
||||
System.out.println("3. Borramos el país 152");
|
||||
paisDao.borrarPais(152);
|
||||
|
||||
System.out.println("4. Insertamos de nuevo el país");
|
||||
paisDao.insertarPais(espana);
|
||||
|
||||
System.out.println("5. Cambiamos la capital a Capital city");
|
||||
espana.setNombreCapital("Capital city");
|
||||
paisDao.actualizarPais(espana);
|
||||
|
||||
Pais actualizado = paisDao.obtenerPaisPorID(152);
|
||||
System.out.println("Resultado final: " + actualizado);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.palomafp.aadd.smmb.dao;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
Continente obtenerContinentePorCodigo(String idContinente) throws SQLException;
|
||||
List<Continente> obtenerListaContinentes() throws SQLException;
|
||||
void insertarContinente(Continente continente) throws SQLException;
|
||||
void borrarContinente(String codigoContinente) throws SQLException;
|
||||
void actualizarContinente(Continente continente) throws SQLException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.palomafp.aadd.smmb.dao;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
public List<Pais> obtenerPaises() throws SQLException;
|
||||
public Pais obtenerPaisPorID(int idPais) throws SQLException;
|
||||
void insertarPais(Pais pais) throws SQLException;
|
||||
void borrarPais(int idPais) throws SQLException;
|
||||
void actualizarPais(Pais pais) throws SQLException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package org.palomafp.aadd.smmb.dao.impl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.smmb.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||
import org.palomafp.aadd.smmb.model.Continente;
|
||||
import org.palomafp.aadd.smmb.model.Pais;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class PaisDaoMySql implements IPaisDAO{
|
||||
|
||||
private final String url;
|
||||
private final String bbdd;
|
||||
private final String usuario;
|
||||
private final String clave;
|
||||
|
||||
public PaisDaoMySql(GestorConfiguracion gestor) {
|
||||
this.url = gestor.getUrl();
|
||||
this.bbdd = gestor.getBaseDatos();
|
||||
this.usuario = gestor.getUsuario();
|
||||
this.clave = gestor.getClave();
|
||||
}
|
||||
|
||||
private Connection obtenerConexion() throws SQLException {
|
||||
|
||||
// No hace falta usar el Driver porque esto lo organiza solo (?)
|
||||
Connection conexion = DriverManager.getConnection(url + bbdd, usuario, clave);
|
||||
|
||||
return conexion;
|
||||
}
|
||||
|
||||
private static final String SELECT_PAISES = "SELECT p.identificador, p.nombre_pais, p.capital, c.codigo, c.nombre_continente "
|
||||
+ "FROM T_PAIS p JOIN T_CONTINENTE c ON p.cod_continente = c.codigo";
|
||||
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPaises() throws SQLException {
|
||||
|
||||
List<Pais> lista = new ArrayList<>();
|
||||
|
||||
// Conexión, mensajero y tabla de resultados
|
||||
try (Connection conexion = obtenerConexion();
|
||||
Statement st = conexion.createStatement();
|
||||
ResultSet rs = st.executeQuery(SELECT_PAISES)) {
|
||||
|
||||
while (rs.next()) {
|
||||
Pais pais = construirPais(rs);
|
||||
lista.add(pais);
|
||||
}
|
||||
}
|
||||
|
||||
return lista;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int idPais) throws SQLException {
|
||||
|
||||
String sql = SELECT_PAISES + " WHERE p.identificador = " + idPais;
|
||||
|
||||
try (Connection conexion = obtenerConexion();
|
||||
Statement st = conexion.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
|
||||
// Vemos si la encuentra o no para devolver el pais o null
|
||||
if (rs.next()) {
|
||||
|
||||
Pais pais = construirPais(rs);
|
||||
return pais;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertarPais(Pais pais) throws SQLException {
|
||||
|
||||
String codContinente = pais.getContinente().getCodigo();
|
||||
int identificador = pais.getIdentificador();
|
||||
String nombre = pais.getNombre();
|
||||
String capital = pais.getNombreCapital();
|
||||
|
||||
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES ('"
|
||||
+ codContinente + "', " + identificador + ", '" + nombre + "', '" + capital + "')";
|
||||
|
||||
|
||||
try (Connection conexion = obtenerConexion();
|
||||
Statement st = conexion.createStatement();) {
|
||||
|
||||
st.executeUpdate(sql);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarPais(int idPais) throws SQLException {
|
||||
|
||||
String sql = "DELETE FROM T_PAIS WHERE identificador = " + idPais;
|
||||
|
||||
try (Connection conexion = obtenerConexion();
|
||||
Statement st = conexion.createStatement();) {
|
||||
|
||||
st.executeUpdate(sql);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarPais(Pais pais) throws SQLException {
|
||||
|
||||
String codContinente = pais.getContinente().getCodigo();
|
||||
int identificador = pais.getIdentificador();
|
||||
String nombre = pais.getNombre();
|
||||
String capital = pais.getNombreCapital();
|
||||
|
||||
String sql = "UPDATE T_PAIS SET cod_continente = '" + codContinente + "', nombre_pais = '" + nombre
|
||||
+ "', capital = '" + capital + "' WHERE identificador = " + identificador;
|
||||
|
||||
try (Connection conexion = obtenerConexion();
|
||||
Statement st = conexion.createStatement()) {
|
||||
|
||||
st.executeUpdate(sql);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private Pais construirPais(ResultSet rs) throws SQLException {
|
||||
|
||||
Continente continente = new Continente();
|
||||
continente.setCodigo(rs.getString("codigo"));
|
||||
continente.setNombre(rs.getString("nombre_continente"));
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(rs.getInt("identificador"));
|
||||
pais.setNombre(rs.getString("nombre_pais"));
|
||||
pais.setNombreCapital(rs.getString("capital"));
|
||||
pais.setContinente(continente);
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.palomafp.aadd.smmb.gestores;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
|
||||
private final Properties propiedades = new Properties();
|
||||
|
||||
public GestorConfiguracion(String rutaFicheroConfig) throws IOException {
|
||||
|
||||
try (InputStream is = GestorConfiguracion.class.getClassLoader().getResourceAsStream(rutaFicheroConfig)) {
|
||||
|
||||
if (is == null) {
|
||||
throw new IOException("No se ha encontrado el fichero de configuración: " + rutaFicheroConfig);
|
||||
}
|
||||
|
||||
propiedades.load(is);
|
||||
}
|
||||
}
|
||||
|
||||
public String getDriver() {
|
||||
return propiedades.getProperty("sgdb.clase.driver");
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return propiedades.getProperty("sgdb.conexion.url");
|
||||
}
|
||||
|
||||
public String getBaseDatos() {
|
||||
return propiedades.getProperty("bbdd.nombre");
|
||||
}
|
||||
|
||||
public String getUsuario() {
|
||||
return propiedades.getProperty("bbdd.usuario");
|
||||
}
|
||||
|
||||
public String getClave() {
|
||||
return propiedades.getProperty("bbdd.clave");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.palomafp.aadd.smmb.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Continente implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente() {
|
||||
}
|
||||
|
||||
|
||||
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 this.nombre + " (" + this.codigo + ")";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.palomafp.aadd.smmb.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Pais implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String nombreCapital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 getNombreCapital() {
|
||||
return nombreCapital;
|
||||
}
|
||||
public void setNombreCapital(String capital) {
|
||||
this.nombreCapital = capital;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.nombre + " (" + this.identificador + ") con capital en " + this.nombreCapital +
|
||||
" pertenece al continente " + this.continente;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user