Primer commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
bin/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
target/
|
||||
@@ -0,0 +1,6 @@
|
||||
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
||||
sgdb.conexion.url=jdbc:mysql://192.168.1.134:3306/
|
||||
bbdd.nombre=Mapa_Mundi
|
||||
bbdd.usuario=root
|
||||
bbdd.clave=root
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<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>aadd_conect02</groupId>
|
||||
<artifactId>aadd_conect02</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>25</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.palomafp.aadd.ss;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.ss.dao.impl.PaisDaoJDBC;
|
||||
import org.palomafp.aadd.ss.gestores.GestorConexionesBBDD;
|
||||
import org.palomafp.aadd.ss.model.Pais;
|
||||
|
||||
public class AppPrincipal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try(Connection con = GestorConexionesBBDD.obtenerConexion()){
|
||||
System.out.println("¡Conexión establecida con éxito mediante el gestor!\n");
|
||||
|
||||
IPaisDAO dao = new PaisDaoJDBC(con);
|
||||
|
||||
|
||||
System.out.println("--- 1. CONSULTA DE TODOS LOS PAÍSES ---");
|
||||
List<Pais> paices = dao.obtenerListaPaises();
|
||||
for(Pais p : paices) {
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
|
||||
System.out.println("\n--- 2. CONSULTAR PAÍS CON ID 152 ---");
|
||||
Pais pais152 = dao.obtenerPaisPorIdentificador(152);
|
||||
if(pais152 != null) {
|
||||
System.out.println(pais152);
|
||||
}else {
|
||||
System.out.println("No ha encontradao el pais 152");
|
||||
}
|
||||
|
||||
System.out.println("\n--- 3. BORRAR PAÍS ID 152 ---");
|
||||
int filasAfectadas = dao.borrarPais(152);
|
||||
System.out.println("Filas afectaas al borrar: " + filasAfectadas);
|
||||
|
||||
System.out.println("\n--- 4. REINSERTAR PAÍS DESDE LA VARIABLE DEL PASO 2 ---");
|
||||
if (pais152 != null) {
|
||||
int filasInsertadas = dao.insertarPais(pais152);
|
||||
System.out.println("Filas insertadas: " + filasInsertadas);
|
||||
}
|
||||
|
||||
System.out.println("\n--- 5. ACTUALIZAR CAPITAL A 'Capital city' ---");
|
||||
if (pais152 != null) {
|
||||
pais152.setCapital("Capital city");
|
||||
int filasActualizadas = dao.actualizarPais(pais152);
|
||||
System.out.println("Filas actualizadas: " + filasActualizadas);
|
||||
}
|
||||
|
||||
System.out.println("\n--- COMPROBACIÓN FINAL EN LA BBDD ---");
|
||||
Pais paisFinal = dao.obtenerPaisPorIdentificador(152);
|
||||
System.out.println("Resultado final guardado en MySQL: " + paisFinal);
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error en la Base de Datos: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error general en la aplicación: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.palomafp.aadd.ss.dao;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
List<Pais> obtenerListaPaises();
|
||||
Pais obtenerPaisPorIdentificador(int idPais);
|
||||
int borrarPais(int idPais) throws SQLException;
|
||||
int insertarPais(Pais pais) throws SQLException;
|
||||
int actualizarPais(Pais pais) throws SQLException;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package org.palomafp.aadd.ss.dao.impl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.ss.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.ss.model.Pais;
|
||||
|
||||
public class PaisDaoJDBC implements IPaisDAO{
|
||||
|
||||
private Connection conexion;
|
||||
public PaisDaoJDBC(Connection conexion) {
|
||||
this.conexion = conexion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerListaPaises() {
|
||||
|
||||
List<Pais> paices = new ArrayList<>();
|
||||
String sql = "SELECT * FROM T_PAIS";
|
||||
|
||||
try(Statement sentencia = conexion.createStatement();
|
||||
ResultSet resultado = sentencia.executeQuery(sql)){
|
||||
while(resultado.next()) {
|
||||
Pais p = new Pais(
|
||||
resultado.getString("cod_continente"),
|
||||
resultado.getInt("identificador"),
|
||||
resultado.getString("nombre_pais"),
|
||||
resultado.getString("capital")
|
||||
);
|
||||
paices.add(p);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return paices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorIdentificador(int idPais) {
|
||||
String sql = "SELECT * FROM T_PAIS p JOIN T_CONTINENTE c ON p.cod_continente WHERE p.identificador = " + idPais;
|
||||
Pais pais = null;
|
||||
try(Statement sentencia = conexion.createStatement();
|
||||
ResultSet resultado = sentencia.executeQuery(sql)){
|
||||
|
||||
if(resultado.next()) {
|
||||
pais = new Pais(
|
||||
resultado.getString("cod_continente"),
|
||||
resultado.getInt("identificador"),
|
||||
resultado.getString("nombre_pais"),
|
||||
resultado.getString("capital")
|
||||
);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return pais;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int borrarPais(int idPais) throws SQLException {
|
||||
String sql = "DELETE FROM T_PAIS WHERE identificador = " + idPais;
|
||||
try(Statement sentencia = conexion.createStatement()){
|
||||
return sentencia.executeUpdate(sql);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertarPais(Pais pais) throws SQLException {
|
||||
|
||||
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES ('"
|
||||
+ pais.getCodContinente() + "', "
|
||||
+ pais.getIdentificador() + ", '"
|
||||
+ pais.getNombrePais() + "', '"
|
||||
+ pais.getCapital() + "')";
|
||||
|
||||
try(Statement sentencia = conexion.createStatement()){
|
||||
return sentencia.executeUpdate(sql);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int actualizarPais(Pais pais) throws SQLException {
|
||||
|
||||
String sql = "UPDATE T_PAIS SET cod_continente = '"
|
||||
+ pais.getCodContinente() + "', nombre_pais = '"
|
||||
+ pais.getNombrePais() + "', capital = '"
|
||||
+ pais.getCapital() + "' WHERE identificador = "
|
||||
+ pais.getIdentificador() + " ";
|
||||
|
||||
try(Statement sentencia = conexion.createStatement()){
|
||||
return sentencia.executeUpdate(sql);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.palomafp.aadd.ss.gestores;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class GestorConexionesBBDD {
|
||||
private GestorConexionesBBDD() {
|
||||
|
||||
}
|
||||
|
||||
public static Connection obtenerConexion() {
|
||||
Connection conexion = null;
|
||||
|
||||
try {
|
||||
//Carga dinámicamente el driver JDBC de MySQL en memoria.
|
||||
Class.forName(GestoreConfiguracion.getClaseDriver());
|
||||
String urlConexcionCompleta = GestoreConfiguracion.getConexionURL() + GestoreConfiguracion.getNombreBBDD();
|
||||
String usuarioBBDD = GestoreConfiguracion.getUsuarioBBDD();
|
||||
String claveBBDD = GestoreConfiguracion.getClaveBBDD();
|
||||
|
||||
conexion = DriverManager.getConnection(urlConexcionCompleta, usuarioBBDD, claveBBDD);
|
||||
|
||||
}catch(ClassNotFoundException | SQLException e){
|
||||
throw new RuntimeException("No ha podido obtener una conexion con el SGBD ", e);
|
||||
}
|
||||
return conexion;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.palomafp.aadd.ss.gestores;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class GestoreConfiguracion {
|
||||
private static final String RUTA_FICHERO = "application.properties";
|
||||
private static final Properties PROPIEDADES = new Properties();
|
||||
|
||||
private GestoreConfiguracion() {
|
||||
|
||||
}
|
||||
|
||||
static {
|
||||
try(InputStream inputStream = GestoreConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO)){
|
||||
|
||||
if (inputStream == null) {
|
||||
throw new IOException("No se encuentra " + RUTA_FICHERO);
|
||||
}
|
||||
PROPIEDADES.load(inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("No se ha podido cargar el fichero de configuracion ", e);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static String getClaseDriver() {
|
||||
return PROPIEDADES.getProperty("sgdb.clase.driver");
|
||||
}
|
||||
|
||||
public static String getConexionURL() {
|
||||
return PROPIEDADES.getProperty("sgdb.conexion.url");
|
||||
}
|
||||
|
||||
public static String getNombreBBDD() {
|
||||
return PROPIEDADES.getProperty("bbdd.nombre");
|
||||
}
|
||||
|
||||
public static String getUsuarioBBDD() {
|
||||
return PROPIEDADES.getProperty("bbdd.usuario");
|
||||
}
|
||||
|
||||
public static String getClaveBBDD() {
|
||||
return PROPIEDADES.getProperty("bbdd.clave");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.palomafp.aadd.ss.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 + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user