commit inicial
This commit is contained in:
commit
146d48dda6
41
.classpath
Normal file
41
.classpath
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-25">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
||||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target/
|
||||||
|
*.class
|
||||||
23
.project
Normal file
23
.project
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>aadd_act3_4</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
2
.settings/org.eclipse.core.resources.prefs
Normal file
2
.settings/org.eclipse.core.resources.prefs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding/<project>=UTF-8
|
||||||
13
.settings/org.eclipse.jdt.core.prefs
Normal file
13
.settings/org.eclipse.jdt.core.prefs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=24
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=24
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.release=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=24
|
||||||
4
.settings/org.eclipse.m2e.core.prefs
Normal file
4
.settings/org.eclipse.m2e.core.prefs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
||||||
27
pom.xml
Normal file
27
pom.xml
Normal file
@ -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>aadd_act2_3</groupId>
|
||||||
|
<artifactId>aadd_act2_3</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<dependencies>
|
||||||
|
<!-- Hibernate Core -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.orm</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
<version>7.1.11.Final</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- MySQL Connector/J -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<version>9.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bytebuddy</groupId>
|
||||||
|
<artifactId>byte-buddy</artifactId>
|
||||||
|
<version>1.15.11</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
103
src/main/java/es/palomafp/aadd/inm/GestorMapaMundi.java
Normal file
103
src/main/java/es/palomafp/aadd/inm/GestorMapaMundi.java
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package es.palomafp.aadd.inm;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import es.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||||
|
import es.palomafp.aadd.inm.dao.IPaisDAO;
|
||||||
|
import es.palomafp.aadd.inm.dao.hbm.ContinenteDaoHibernate;
|
||||||
|
import es.palomafp.aadd.inm.dao.hbm.PaisDaoHibernate;
|
||||||
|
import es.palomafp.aadd.inm.vo.Continente;
|
||||||
|
import es.palomafp.aadd.inm.vo.Pais;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* GestorMapaMundi: Clase que accede a información de la BBDD MapaMundi.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 26 sept 2025
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
GestorMapaMundi gestor = new GestorMapaMundi();
|
||||||
|
|
||||||
|
Scanner scanner = new Scanner(System.in); // Crear el scanner
|
||||||
|
|
||||||
|
System.out.println("Introduce una opción de procesamiento sobre Mapa Mundi.");
|
||||||
|
System.out.println("Opción 1: Consultar países del continente americao que empiezan por Sa");
|
||||||
|
System.out.println("Opción 2: Añadir nuevo continente.");
|
||||||
|
System.out.println("Opción 3: Actualizar el país con código 107. capital -> Capital City.");
|
||||||
|
System.out.println("Opción 4: Eliminar el continente con código 02.");
|
||||||
|
String opcion = scanner.nextLine(); // Leer una opción introducida.
|
||||||
|
|
||||||
|
switch (opcion) {
|
||||||
|
case "1":
|
||||||
|
System.out.println("Opción 1: Consultar países del continente americano que empiezan por Sa");
|
||||||
|
List<Pais> listaPaises = gestor.consultarPaisesAmericanosEmpiezanSa();
|
||||||
|
for (Pais pais : listaPaises) {
|
||||||
|
String infoPais = "%s (%d) pertenece al continente %s (%s)";
|
||||||
|
infoPais = String.format(infoPais, pais.getNombrePais(), pais.getIdentificador(),
|
||||||
|
pais.getContinente().getNombreContinente(), pais.getContinente().getCodigo());
|
||||||
|
|
||||||
|
System.out.println(infoPais);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "2":
|
||||||
|
System.out.println("Opción 2: Añadir nuevo continente.");
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo("06");
|
||||||
|
continente.setNombreContinente("Antártida");
|
||||||
|
gestor.anyadirContinente(continente);
|
||||||
|
break;
|
||||||
|
case "3":
|
||||||
|
System.out.println("Opción 3: Actualizar el país con código 107. capital -> Capital City.");
|
||||||
|
gestor.actualizarPaisCodigo107();
|
||||||
|
break;
|
||||||
|
case "4":
|
||||||
|
System.out.println("Opción 4: Eliminar el continente con código 02.");
|
||||||
|
gestor.eliminarContinente("02");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Pais> consultarPaisesAmericanosEmpiezanSa() {
|
||||||
|
List<Pais> listaPaises = null;
|
||||||
|
|
||||||
|
IPaisDAO paisDAO = new PaisDaoHibernate();
|
||||||
|
listaPaises = paisDAO.obtenerListaPaisesAmericanosEmpiezanSa();
|
||||||
|
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eliminarContinente(String codigo) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoHibernate();
|
||||||
|
continenteDAO.eliminarContinentePorCodigo(codigo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais actualizarPaisCodigo107() {
|
||||||
|
Pais pais = null;
|
||||||
|
int identificadorPais = 107;
|
||||||
|
|
||||||
|
IPaisDAO paisDAO = new PaisDaoHibernate();
|
||||||
|
pais = paisDAO.obtenerPaisPorID(identificadorPais);
|
||||||
|
pais.setCapital("Capital City");
|
||||||
|
paisDAO.actualizarPais(pais);
|
||||||
|
|
||||||
|
pais = paisDAO.obtenerPaisPorID(identificadorPais);
|
||||||
|
System.out.println(pais);
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void anyadirContinente(Continente continente) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoHibernate();
|
||||||
|
continenteDAO.anyadirContinente(continente);
|
||||||
|
System.out.println("Se ha creado el continente: " + continente);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
15
src/main/java/es/palomafp/aadd/inm/dao/IContinenteDAO.java
Normal file
15
src/main/java/es/palomafp/aadd/inm/dao/IContinenteDAO.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package es.palomafp.aadd.inm.dao;
|
||||||
|
|
||||||
|
import es.palomafp.aadd.inm.vo.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* IContinenteDAO: Interfaz que define las operaciones de acceso a datos para Continente.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 31 oct 2025
|
||||||
|
*/
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
void anyadirContinente(Continente continente);
|
||||||
|
void eliminarContinentePorCodigo(String codigo);
|
||||||
|
}
|
||||||
18
src/main/java/es/palomafp/aadd/inm/dao/IPaisDAO.java
Normal file
18
src/main/java/es/palomafp/aadd/inm/dao/IPaisDAO.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package es.palomafp.aadd.inm.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import es.palomafp.aadd.inm.vo.Pais;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* IPaisDAO: Interfaz que define las operaciones de acceso a datos para País.
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 31 oct 2025
|
||||||
|
*/
|
||||||
|
public interface IPaisDAO {
|
||||||
|
Pais obtenerPaisPorID(int identificador);
|
||||||
|
List<Pais> obtenerListaPaisesAmericanosEmpiezanSa();
|
||||||
|
void actualizarPais(Pais pais);
|
||||||
|
}
|
||||||
@ -0,0 +1,61 @@
|
|||||||
|
package es.palomafp.aadd.inm.dao.hbm;
|
||||||
|
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
import org.hibernate.query.MutationQuery;
|
||||||
|
|
||||||
|
import es.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||||
|
import es.palomafp.aadd.inm.gestor.GestorSesionesHibernate;
|
||||||
|
import es.palomafp.aadd.inm.vo.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* ContinenteDaoJDBC: Clase que implementa el interfaz IContinenteDAO
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 31 oct 2025
|
||||||
|
*/
|
||||||
|
public class ContinenteDaoHibernate implements IContinenteDAO {
|
||||||
|
private Transaction transaccion = null;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void anyadirContinente(Continente continente) {
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Connection, Statement
|
||||||
|
try (Session sesion = GestorSesionesHibernate.getSession();) {
|
||||||
|
transaccion = sesion.beginTransaction();
|
||||||
|
|
||||||
|
sesion.persist(continente);
|
||||||
|
|
||||||
|
transaccion.commit();
|
||||||
|
} catch (HibernateException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eliminarContinentePorCodigo(String codigo) {
|
||||||
|
|
||||||
|
try (Session sesion = GestorSesionesHibernate.getSession();) {
|
||||||
|
transaccion = sesion.beginTransaction();
|
||||||
|
|
||||||
|
String sentenciaBorradoPaisesHQL = "DELETE FROM Pais p WHERE p.continente.codigo = :codigo";
|
||||||
|
// Obsoleto
|
||||||
|
// Query senteciaBorradoPaises = sesion.createQuery(sentenciaBorradoPaisesHQL, Pais.class);
|
||||||
|
MutationQuery senteciaBorradoPaises = sesion.createMutationQuery(sentenciaBorradoPaisesHQL);
|
||||||
|
senteciaBorradoPaises.setParameter("codigo", codigo);
|
||||||
|
senteciaBorradoPaises.executeUpdate();
|
||||||
|
|
||||||
|
String sentenciaBorradoContinenteHQL = "DELETE FROM Continente WHERE codigo= :codigo";
|
||||||
|
MutationQuery sentenciaBorradoContinente = sesion.createMutationQuery(sentenciaBorradoContinenteHQL);
|
||||||
|
sentenciaBorradoContinente.setParameter("codigo", codigo);
|
||||||
|
sentenciaBorradoContinente.executeUpdate();
|
||||||
|
|
||||||
|
transaccion.commit();
|
||||||
|
} catch (HibernateException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
package es.palomafp.aadd.inm.dao.hbm;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
import org.hibernate.query.SelectionQuery;
|
||||||
|
|
||||||
|
import es.palomafp.aadd.inm.dao.IPaisDAO;
|
||||||
|
import es.palomafp.aadd.inm.gestor.GestorSesionesHibernate;
|
||||||
|
import es.palomafp.aadd.inm.vo.Pais;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* PaisDaoJDBC: Clase que implementa el interfaz IPaisDAO
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 31 oct 2025
|
||||||
|
*/
|
||||||
|
public class PaisDaoHibernate implements IPaisDAO {
|
||||||
|
private Transaction transaccion = null;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerListaPaisesAmericanosEmpiezanSa() {
|
||||||
|
List<Pais> listaPaises = null;
|
||||||
|
String sentenciaHQL = """
|
||||||
|
SELECT p
|
||||||
|
FROM Pais p JOIN FETCH p.continente
|
||||||
|
WHERE p.continente.codigo ='02'
|
||||||
|
AND p.nombrePais LIKE 'Sa%'
|
||||||
|
""";
|
||||||
|
// try con recursos "cerrables": Session
|
||||||
|
try (Session sesion = GestorSesionesHibernate.getSession();) {
|
||||||
|
// Obsoleto
|
||||||
|
// Query<Pais> sentenciaConsulta = sesion.createQuery(sentenciaHQL, Pais.class);
|
||||||
|
|
||||||
|
SelectionQuery<Pais> sentenciaConsulta = sesion.createSelectionQuery(sentenciaHQL, Pais.class);
|
||||||
|
listaPaises = sentenciaConsulta.getResultList();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarPais(Pais pais) {
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Session
|
||||||
|
try (Session sesion = GestorSesionesHibernate.getSession();) {
|
||||||
|
transaccion = sesion.beginTransaction();
|
||||||
|
|
||||||
|
if (!sesion.contains(pais))
|
||||||
|
sesion.merge(pais);
|
||||||
|
|
||||||
|
transaccion.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int identificador) {
|
||||||
|
Pais pais = null;
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Session
|
||||||
|
try (Session sesion = GestorSesionesHibernate.getSession();) {
|
||||||
|
pais = sesion.find(Pais.class, identificador);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,59 @@
|
|||||||
|
package es.palomafp.aadd.inm.dao.jdbc;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
import es.palomafp.aadd.inm.dao.IContinenteDAO;
|
||||||
|
import es.palomafp.aadd.inm.gestor.GestorConexionJDBC;
|
||||||
|
import es.palomafp.aadd.inm.vo.Continente;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* ContinenteDaoJDBC: Clase que implementa el interfaz IContinenteDAO
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 31 oct 2025
|
||||||
|
*/
|
||||||
|
public class ContinenteDaoJDBC implements IContinenteDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void anyadirContinente(Continente continente) {
|
||||||
|
String sentenciaSQL = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES ('%s', '%s')";
|
||||||
|
|
||||||
|
sentenciaSQL = String.format(sentenciaSQL, continente.getCodigo(), continente.getNombreContinente());
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Connection, Statement
|
||||||
|
try (Connection conexion = GestorConexionJDBC.obtenerConexionJDBC();
|
||||||
|
Statement stm = conexion.createStatement();) {
|
||||||
|
stm.executeUpdate(sentenciaSQL);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eliminarContinentePorCodigo(String codigo) {
|
||||||
|
String sentenciaBorradoPaisesSQL = "DELETE FROM T_PAIS WHERE cod_continente='%s'";
|
||||||
|
sentenciaBorradoPaisesSQL = String.format(sentenciaBorradoPaisesSQL, codigo);
|
||||||
|
|
||||||
|
String sentenciaBorradoContinenteSQL = "DELETE FROM T_CONTINENTE WHERE codigo='%s'";
|
||||||
|
sentenciaBorradoContinenteSQL = String.format(sentenciaBorradoContinenteSQL, codigo);
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Connection, Statement
|
||||||
|
try (Connection conexion = GestorConexionJDBC.obtenerConexionJDBC();
|
||||||
|
Statement stm = conexion.createStatement();) {
|
||||||
|
|
||||||
|
// Se desactiva 'autocommit' para manejar la transacción manualmente
|
||||||
|
conexion.setAutoCommit(false);
|
||||||
|
stm.executeUpdate(sentenciaBorradoPaisesSQL);
|
||||||
|
stm.executeUpdate(sentenciaBorradoContinenteSQL);
|
||||||
|
|
||||||
|
// Confirmamos la transacción
|
||||||
|
conexion.commit();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
106
src/main/java/es/palomafp/aadd/inm/dao/jdbc/PaisDaoJDBC.java
Normal file
106
src/main/java/es/palomafp/aadd/inm/dao/jdbc/PaisDaoJDBC.java
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package es.palomafp.aadd.inm.dao.jdbc;
|
||||||
|
|
||||||
|
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 es.palomafp.aadd.inm.dao.IPaisDAO;
|
||||||
|
import es.palomafp.aadd.inm.gestor.GestorConexionJDBC;
|
||||||
|
import es.palomafp.aadd.inm.vo.Continente;
|
||||||
|
import es.palomafp.aadd.inm.vo.Pais;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* PaisDaoJDBC: Clase que implementa el interfaz IPaisDAO
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 31 oct 2025
|
||||||
|
*/
|
||||||
|
public class PaisDaoJDBC implements IPaisDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerListaPaisesAmericanosEmpiezanSa() {
|
||||||
|
List<Pais> listaPaises = null;
|
||||||
|
String sentenciaSQL = """
|
||||||
|
SELECT *
|
||||||
|
FROM T_PAIS p join T_CONTINENTE c
|
||||||
|
on cod_continente= codigo
|
||||||
|
WHERE codigo='02' AND p.nombre_pais LIKE 'Sa%'
|
||||||
|
""";
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Connection, Statement, ResultSet
|
||||||
|
try (Connection conexion = GestorConexionJDBC.obtenerConexionJDBC();
|
||||||
|
Statement stm = conexion.createStatement();
|
||||||
|
ResultSet resultado = stm.executeQuery(sentenciaSQL);) {
|
||||||
|
listaPaises = new ArrayList<Pais>();
|
||||||
|
while (resultado.next()) {
|
||||||
|
// Completar los atributos de continente
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(resultado.getString("codigo"));
|
||||||
|
continente.setNombreContinente(resultado.getString("nombre_continente"));
|
||||||
|
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(resultado.getInt("identificador"));
|
||||||
|
pais.setNombrePais(resultado.getString("nombre_pais"));
|
||||||
|
pais.setCapital(resultado.getString("capital"));
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
listaPaises.add(pais);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizarPais(Pais pais) {
|
||||||
|
String sentenciaSQL = "update T_PAIS set capital= '%s' WHERE identificador=%d";
|
||||||
|
sentenciaSQL = String.format(sentenciaSQL, pais.getCapital(), pais.getIdentificador());
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Connection, Statement
|
||||||
|
try (Connection conexion = GestorConexionJDBC.obtenerConexionJDBC();
|
||||||
|
Statement stm = conexion.createStatement();) {
|
||||||
|
stm.executeUpdate(sentenciaSQL);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int identificador) {
|
||||||
|
Pais pais = null;
|
||||||
|
String sentenciaSQL = """
|
||||||
|
SELECT *
|
||||||
|
FROM T_PAIS p join T_CONTINENTE c
|
||||||
|
on cod_continente= codigo
|
||||||
|
WHERE identificador=%d
|
||||||
|
""";
|
||||||
|
sentenciaSQL = String.format(sentenciaSQL, identificador);
|
||||||
|
|
||||||
|
// try con recursos "cerrables": Connection, Statement, ResultSet
|
||||||
|
try (Connection conexion = GestorConexionJDBC.obtenerConexionJDBC();
|
||||||
|
Statement stm = conexion.createStatement();
|
||||||
|
ResultSet resultado = stm.executeQuery(sentenciaSQL);) {
|
||||||
|
if (resultado.next()) {
|
||||||
|
// Completar los atributos de continente
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(resultado.getString("codigo"));
|
||||||
|
continente.setNombreContinente(resultado.getString("nombre_continente"));
|
||||||
|
|
||||||
|
pais = new Pais();
|
||||||
|
pais.setIdentificador(resultado.getInt("identificador"));
|
||||||
|
pais.setNombrePais(resultado.getString("nombre_pais"));
|
||||||
|
pais.setCapital(resultado.getString("capital"));
|
||||||
|
pais.setContinente(continente);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
package es.palomafp.aadd.inm.gestor;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* GestorConexionJDBC: Clase que realiza ....
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 6 nov 2025
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GestorConexionJDBC {
|
||||||
|
|
||||||
|
// Constructor privado para evitar instanciación
|
||||||
|
private GestorConexionJDBC() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection obtenerConexionJDBC() {
|
||||||
|
Connection conexion = null;
|
||||||
|
String driverJDBC= GestorFicheroConfiguracion.getValorDeConfiguracion("jdbc.driver");
|
||||||
|
try {
|
||||||
|
Class.forName(driverJDBC);
|
||||||
|
|
||||||
|
String urlJDBC= GestorFicheroConfiguracion.getValorDeConfiguracion("jdbc.url");
|
||||||
|
String usuario= GestorFicheroConfiguracion.getValorDeConfiguracion("jdbc.usuario");
|
||||||
|
String password= GestorFicheroConfiguracion.getValorDeConfiguracion("jdbc.password");
|
||||||
|
|
||||||
|
conexion=DriverManager.getConnection(urlJDBC, usuario, password);
|
||||||
|
} catch (ClassNotFoundException | SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return conexion;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
package es.palomafp.aadd.inm.gestor;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* GestorFicheroConfiguracion: Clase que se encarga de gestionar el fichero de configuración.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 31 oct 2025
|
||||||
|
*/
|
||||||
|
public class GestorFicheroConfiguracion {
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "config/conf.properties";
|
||||||
|
private static Properties propiedades;
|
||||||
|
|
||||||
|
private GestorFicheroConfiguracion() {
|
||||||
|
// Constructor privado para evitar instanciación
|
||||||
|
}
|
||||||
|
static {
|
||||||
|
propiedades= new Properties();
|
||||||
|
try {
|
||||||
|
// Fichero que está en el classpath
|
||||||
|
propiedades.load(GestorFicheroConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION));
|
||||||
|
|
||||||
|
// Fichero con ruta relativa al proyecto
|
||||||
|
// propiedades.load(new FileInputStream(RUTA_FICHERO_CONFIGURACION));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static String getValorDeConfiguracion(String clave) {
|
||||||
|
String valor = null;
|
||||||
|
valor = propiedades.getProperty(clave);
|
||||||
|
return valor;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package es.palomafp.aadd.inm.gestor;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* GestorSesionesHibernate: Clase que realiza ....
|
||||||
|
*
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 4 dic 2025
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GestorSesionesHibernate {
|
||||||
|
private static SessionFactory sessionFactory = null;
|
||||||
|
|
||||||
|
private GestorSesionesHibernate() {// Constructor privado para evitar instanciación
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga la configuración desde hibernate.cfg.xml
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
sessionFactory = new Configuration().configure().buildSessionFactory();
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
System.err.println("Error en SessionFactory: " + ex);
|
||||||
|
throw new ExceptionInInitializerError(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Session getSession() {
|
||||||
|
return sessionFactory.openSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void cerrarFactoria() {
|
||||||
|
if (sessionFactory != null) {
|
||||||
|
sessionFactory.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/main/java/es/palomafp/aadd/inm/vo/Continente.java
Normal file
48
src/main/java/es/palomafp/aadd/inm/vo/Continente.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package es.palomafp.aadd.inm.vo;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Continente: Clase que se usa para mapear la tabla de continentes
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 5 dic 2025
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="T_CONTINENTE")
|
||||||
|
public class Continente {
|
||||||
|
@Id
|
||||||
|
@Column(name="codigo", columnDefinition = "char(2)")
|
||||||
|
private String codigo;
|
||||||
|
|
||||||
|
@Column(name="nombre_continente", length=30, nullable=true)
|
||||||
|
private String 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 + "]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
75
src/main/java/es/palomafp/aadd/inm/vo/Pais.java
Normal file
75
src/main/java/es/palomafp/aadd/inm/vo/Pais.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package es.palomafp.aadd.inm.vo;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.FetchType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.JoinColumn;
|
||||||
|
import jakarta.persistence.ManyToOne;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Continente: Clase que se usa para mapear la tabla de Países
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 5 dic 2025
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="T_PAIS")
|
||||||
|
public class Pais {
|
||||||
|
@Id
|
||||||
|
@Column(name="identificador")
|
||||||
|
private int identificador;
|
||||||
|
|
||||||
|
@Column(name="nombre_pais", length=50)
|
||||||
|
private String nombrePais;
|
||||||
|
|
||||||
|
@Column(name="capital", length=20)
|
||||||
|
private String capital;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name="cod_continente")
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Pais [identificador=" + identificador + ", nombrePais=" + nombrePais + ", capital=" + capital
|
||||||
|
+ ", continente=" + continente + "]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
5
src/main/resources/config/conf.properties
Normal file
5
src/main/resources/config/conf.properties
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Parámetros de conexión a la base de datos MapaMundi en SGDB MySQL
|
||||||
|
jdbc.driver = org.postgresql.Driver
|
||||||
|
jdbc.url = jdbc:postgresql://5.189.134.76:5555/mapamundi
|
||||||
|
jdbc.usuario = isinm
|
||||||
|
jdbc.password = isidoro+nm-123
|
||||||
46
src/main/resources/hibernate.cfg.xml
Normal file
46
src/main/resources/hibernate.cfg.xml
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?xml version='1.0' encoding='utf-8'?>
|
||||||
|
<!DOCTYPE hibernate-configuration PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Configuration DTD//EN"
|
||||||
|
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
|
<hibernate-configuration>
|
||||||
|
<session-factory>
|
||||||
|
<property name="connection.url">jdbc:mysql://192.168.1.36:3306/Mapa_Mundi</property>
|
||||||
|
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
|
||||||
|
|
||||||
|
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||||
|
|
||||||
|
<property name="connection.username">root</property>
|
||||||
|
<property name="connection.password">mysql_123</property>
|
||||||
|
|
||||||
|
<!-- DB schema will be updated if needed -->
|
||||||
|
<property name="hbm2ddl.auto">none</property>
|
||||||
|
<property name="show_sql">true</property>
|
||||||
|
<property name="format_sql">true</property>
|
||||||
|
|
||||||
|
<!-- En tu hibernate.cfg.xml, AÑADE esto: -->
|
||||||
|
<property name="hibernate.jpa.compliance.proxy">true</property>
|
||||||
|
<property name="hibernate.allow_instantiation_without_explicit_id">false</property>
|
||||||
|
<property name="hibernate.bytecode.provider">bytebuddy</property> <!-- o "javassist" -->
|
||||||
|
|
||||||
|
<!-- Propiedades adicionales importantes -->
|
||||||
|
<property name="hibernate.enable_lazy_load_no_trans">false</property>
|
||||||
|
<property name="hibernate.max_fetch_depth">3</property>
|
||||||
|
<property name="hibernate.jdbc.batch_size">10</property>
|
||||||
|
<property name="hibernate.order_updates">true</property>
|
||||||
|
<property name="hibernate.order_inserts">true</property>
|
||||||
|
|
||||||
|
<!-- Configuración específica de Hibernate 7 -->
|
||||||
|
<property name="jakarta.persistence.lock.timeout">0</property>
|
||||||
|
<property name="jakarta.persistence.query.timeout">0</property>
|
||||||
|
|
||||||
|
<!-- Estadísticas para debug (opcional) -->
|
||||||
|
<property name="hibernate.generate_statistics">false</property>
|
||||||
|
<property name="hibernate.session.events.log">false</property>
|
||||||
|
|
||||||
|
<!-- Mapeo de clases -->
|
||||||
|
<mapping class="es.palomafp.aadd.inm.vo.Continente"></mapping>
|
||||||
|
<mapping class="es.palomafp.aadd.inm.vo.Pais"></mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</session-factory>
|
||||||
|
</hibernate-configuration>
|
||||||
Loading…
Reference in New Issue
Block a user