Actividad 2.2 - CRUD sobre Pais / Mapamundi
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,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>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>25</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,6 @@
|
||||
# info de mi bbdd
|
||||
sgdb.clase.driver=com.mysql.cj.jdbc.Driver
|
||||
sgdb.conexion.url=jdbc:mysql://192.168.1.143:3307/
|
||||
bbdd.nombre=Mapa_Mundi
|
||||
bbdd.usuario=root
|
||||
bbdd.pass=rootpass
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.palomafp.aadd.rmr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.config.ConexionBD;
|
||||
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.rmr.dao.impl.PaisJDBC;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public class AppPrincipal {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
Connection conexion = null;
|
||||
try {
|
||||
conexion = ConexionBD.obtenerConexion();
|
||||
System.out.println("Conectado a la BBDD :)");
|
||||
|
||||
IPaisDAO paisDAO = new PaisJDBC (conexion);
|
||||
|
||||
Continente europa = new Continente("04", "Europa");
|
||||
paisDAO.insertarPais(new Pais(152, "España", "Madrid", europa));
|
||||
|
||||
List<Pais> listaDePaises = paisDAO.obtenerPaises();
|
||||
System.out.println("PARTE 1");
|
||||
System.out.println("PAÍSES - Lista: ");
|
||||
for (Pais pais : listaDePaises) {
|
||||
System.out.println(pais);
|
||||
}
|
||||
|
||||
// parte de consulta de país
|
||||
System.out.println("\nPARTE 2");
|
||||
System.out.println("PAÍS 152 - Consulta: ");
|
||||
Pais paisAConsultar = paisDAO.obtenerPaisPorId(152);
|
||||
System.out.println(paisAConsultar);
|
||||
|
||||
// parte de borrar el país
|
||||
System.out.println("\nPARTE 3");
|
||||
System.out.println("PAÍS 152 - Borrado: ");
|
||||
paisDAO.borrarPais(152);
|
||||
System.out.println(paisDAO.obtenerPaisPorId(152)); // compruebo por si acaso
|
||||
|
||||
// parte de insertar el país. preguntar mañana
|
||||
System.out.println("\nPARTE 4");
|
||||
System.out.println("PAÍS 152 - Insertado: ");
|
||||
paisDAO.insertarPais(paisAConsultar);
|
||||
// preguntar: no puedo hacer paisDAO.insertarPais(paisDAO.obtenerPaisPorId(152));?
|
||||
System.out.println(paisDAO.obtenerPaisPorId(152));
|
||||
|
||||
// parte de actualizar la capital
|
||||
System.out.println("\nPARTE 5:");
|
||||
System.out.println("PAÍS 152 - Capital actualizada: ");
|
||||
paisDAO.actualizarCapital(152, "Capital city");
|
||||
System.out.println(paisDAO.obtenerPaisPorId(152));
|
||||
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
System.err.println("Error: ");
|
||||
} finally {
|
||||
if (conexion != null) {
|
||||
conexion.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.palomafp.aadd.rmr.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ConexionBD {
|
||||
|
||||
private static final String FICHERO = "application.properties";
|
||||
|
||||
private static Properties cargarPropiedades() throws IOException {
|
||||
Properties propiedades = new Properties();
|
||||
InputStream entrada = null;
|
||||
try {
|
||||
entrada = ConexionBD.class.getClassLoader().getResourceAsStream("application.properties");
|
||||
propiedades.load(entrada);
|
||||
} catch (IOException e) {
|
||||
e.getMessage();
|
||||
} finally {
|
||||
if (entrada != null) {
|
||||
entrada.close();
|
||||
}
|
||||
}
|
||||
return propiedades;
|
||||
}
|
||||
|
||||
public static Connection obtenerConexion() throws IOException, ClassNotFoundException, SQLException {
|
||||
Properties propiedades = cargarPropiedades();
|
||||
Class.forName(propiedades.getProperty("sgdb.clase.driver"));
|
||||
|
||||
// estos nombres tienen que coincidir con el de application.properties
|
||||
String url = propiedades.getProperty("sgdb.conexion.url") + propiedades.getProperty("bbdd.nombre");
|
||||
String usuario = propiedades.getProperty("bbdd.usuario");
|
||||
String pass = propiedades.getProperty("bbdd.pass");
|
||||
|
||||
return DriverManager.getConnection(url, usuario, pass);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.palomafp.aadd.rmr.dao;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.palomafp.aadd.rmr.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
List<Pais> obtenerPaises();
|
||||
Pais obtenerPaisPorId(int identificador);
|
||||
void borrarPais(int identificador);
|
||||
void insertarPais(Pais pais);
|
||||
void actualizarCapital(int identificador, String capitalNueva);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.palomafp.aadd.rmr.dao.impl;
|
||||
|
||||
public class ContinenteJDBC {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package org.palomafp.aadd.rmr.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.rmr.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.rmr.model.Continente;
|
||||
import org.palomafp.aadd.rmr.model.Pais;
|
||||
|
||||
public class PaisJDBC implements IPaisDAO {
|
||||
|
||||
private Connection conexion;
|
||||
|
||||
public PaisJDBC(Connection conexion) {
|
||||
this.conexion = conexion;
|
||||
}
|
||||
|
||||
public List<Pais> obtenerPaises() {
|
||||
List<Pais> listaDePaises = new ArrayList<>();
|
||||
try {
|
||||
Statement sentencia = conexion.createStatement();
|
||||
ResultSet resultadoConsulta = sentencia.executeQuery(
|
||||
"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");
|
||||
|
||||
while (resultadoConsulta.next()) {
|
||||
int identificador = resultadoConsulta.getInt("identificador");
|
||||
String nombre = resultadoConsulta.getString("nombre_pais");
|
||||
String capital = resultadoConsulta.getString("capital");
|
||||
String codigoContinente = resultadoConsulta.getString("codigo");
|
||||
String nombreContinente = resultadoConsulta.getString("nombre_continente");
|
||||
|
||||
Continente continente = new Continente(codigoContinente, nombreContinente);
|
||||
Pais pais = new Pais(identificador, nombre, capital, continente);
|
||||
listaDePaises.add(pais);
|
||||
|
||||
}
|
||||
resultadoConsulta.close();
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
}
|
||||
return listaDePaises;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorId(int identificador) { // no puedo poner que lanza una excepción si no lo he declarado en la interfaz
|
||||
Pais pais = null;
|
||||
Statement sentencia = null;
|
||||
ResultSet resultado = null;
|
||||
|
||||
try {
|
||||
|
||||
sentencia = conexion.createStatement();
|
||||
resultado = sentencia.executeQuery("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 WHERE p.identificador = " + identificador);
|
||||
|
||||
if (resultado.next()) {
|
||||
String nombre = resultado.getString("nombre_pais");
|
||||
String capital = resultado.getString("capital");
|
||||
String codigoContinente = resultado.getString("codigo");
|
||||
String nombreContinente = resultado.getString("nombre_continente");
|
||||
|
||||
// mi clase pais guarda también un objeto continente. antes de crear el país
|
||||
// hay que crear su continente con los dos datos que la consulta trae de t_continente (code + nombre)
|
||||
Continente continente = new Continente (codigoContinente, nombreContinente);
|
||||
pais = new Pais (identificador, nombre, capital, continente); // este id es el que le paso por parámetro
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al consultar el país: " + e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (resultado != null) {
|
||||
resultado.close();
|
||||
}
|
||||
if (sentencia != null) {
|
||||
sentencia.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al cerrar: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void borrarPais(int identificador) {
|
||||
|
||||
try {
|
||||
|
||||
Statement sentencia = conexion.createStatement();
|
||||
sentencia.executeUpdate("DELETE FROM T_PAIS WHERE identificador = " + identificador);
|
||||
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al borrar el país: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertarPais(Pais pais) {
|
||||
|
||||
try {
|
||||
Statement sentencia = conexion.createStatement();
|
||||
|
||||
String consulta = "INSERT INTO T_PAIS (identificador, nombre_pais, capital, cod_continente) VALUES (" + pais.getIdentificador() + ", '" + pais.getNombre() + "', '" + pais.getCapital() + "', '" + pais.getContinente().getCodigo() + "')";
|
||||
sentencia.executeUpdate(consulta);
|
||||
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al insertar el país: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actualizarCapital(int identificador, String capitalNueva) {
|
||||
|
||||
try {
|
||||
|
||||
Statement sentencia = conexion.createStatement();
|
||||
sentencia.executeUpdate("UPDATE T_PAIS SET capital = '" + capitalNueva + "' WHERE identificador = " + identificador);
|
||||
sentencia.close();
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error al actualizar la capital: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.palomafp.aadd.rmr.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
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,59 @@
|
||||
package org.palomafp.aadd.rmr.model;
|
||||
|
||||
public class Pais {
|
||||
|
||||
private int identificador;
|
||||
private String nombre;
|
||||
private String capital;
|
||||
private Continente continente;
|
||||
|
||||
public Pais (int identificador, String nombre, String capital, Continente continente) {
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.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 capital;
|
||||
}
|
||||
|
||||
public void setCapital(String capital) {
|
||||
this.capital = capital;
|
||||
}
|
||||
|
||||
|
||||
public Continente getContinente() {
|
||||
return continente;
|
||||
}
|
||||
|
||||
public void setContinente(Continente continente) {
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pais - Identificador: " + identificador + ", Nombre: " + nombre + ", Capital: " + capital + ", Continente: "
|
||||
+ continente;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user