Cambiar código por identificador
This commit is contained in:
@@ -74,7 +74,7 @@ public class AppPrincipal {
|
||||
private Pais obtenerPais(int idPais) {
|
||||
Pais pais = null;
|
||||
|
||||
pais = paisDAO.obtenerPaisPorCodigo(idPais);
|
||||
pais = paisDAO.obtenerPaisPorIdentificador(idPais);
|
||||
return pais;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.palomafp.aadd.inm.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
List<Pais> obtenerListaPaises();
|
||||
Pais obtenerPaisPorCodigo(int idPais);
|
||||
Pais obtenerPaisPorIdentificador(int idPais);
|
||||
void insertarPais(Pais pais);
|
||||
void actualizarPais(Pais pais);
|
||||
void borrarPais(int idPais);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package org.palomafp.aadd.inm.dao.impl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -38,7 +38,7 @@ public class PaisDaoJDBC implements IPaisDAO {
|
||||
ON p.cod_continente=c.codigo
|
||||
""";
|
||||
// System.out.println(sentenciaSQL);
|
||||
try (Statement sentencia = conexion.createStatement();
|
||||
try (PreparedStatement sentencia = conexion.prepareStatement(sentenciaSQL);
|
||||
ResultSet resultados = sentencia.executeQuery(sentenciaSQL);) {
|
||||
while (resultados.next()) {
|
||||
int identificador = resultados.getInt("p.identificador");
|
||||
@@ -68,19 +68,19 @@ public class PaisDaoJDBC implements IPaisDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorCodigo(int idPais) {
|
||||
public Pais obtenerPaisPorIdentificador(int idPais) {
|
||||
Pais pais = null;
|
||||
|
||||
String sentenciaSQL = """
|
||||
SELECT *
|
||||
FROM T_PAIS p JOIN T_CONTINENTE c
|
||||
ON p.cod_continente=c.codigo
|
||||
WHERE identificador = %d
|
||||
WHERE identificador = ?
|
||||
""";
|
||||
sentenciaSQL = String.format(sentenciaSQL, idPais);
|
||||
// System.out.println(sentenciaSQL);
|
||||
try (Statement sentencia = conexion.createStatement();
|
||||
ResultSet resultados = sentencia.executeQuery(sentenciaSQL);) {
|
||||
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||
sentenciaPreparada.setInt(1, idPais);
|
||||
ResultSet resultados = sentenciaPreparada.executeQuery();
|
||||
if (resultados.next()) {
|
||||
int identificador = resultados.getInt("p.identificador");
|
||||
String nombrePais = resultados.getString("p.nombre_pais");
|
||||
@@ -110,13 +110,15 @@ public class PaisDaoJDBC implements IPaisDAO {
|
||||
public void insertarPais(Pais pais) {
|
||||
String sentenciaSQL = """
|
||||
INSERT INTO T_PAIS (identificador, nombre_pais, capital, cod_continente)
|
||||
VALUES (%d, '%s', '%s', '%s')
|
||||
VALUES (?, ?, ?, ?)
|
||||
""";
|
||||
sentenciaSQL = String.format(sentenciaSQL, pais.getIdentificador(), pais.getNombre(), pais.getCapital(),
|
||||
pais.getContinente().getCodigo());
|
||||
// System.out.println(sentenciaSQL);
|
||||
try (Statement sentencia = conexion.createStatement();) {
|
||||
sentencia.execute(sentenciaSQL);
|
||||
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||
sentenciaPreparada.setInt(1, pais.getIdentificador());
|
||||
sentenciaPreparada.setString(2, pais.getNombre());
|
||||
sentenciaPreparada.setString(3, pais.getCapital());
|
||||
sentenciaPreparada.setString(4, pais.getContinente().getCodigo());
|
||||
sentenciaPreparada.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -126,16 +128,18 @@ public class PaisDaoJDBC implements IPaisDAO {
|
||||
public void actualizarPais(Pais pais) {
|
||||
String sentenciaSQL = """
|
||||
UPDATE T_PAIS
|
||||
SET nombre_pais = '%s',
|
||||
capital = '%s',
|
||||
cod_continente= '%s'
|
||||
WHERE identificador= %d
|
||||
SET nombre_pais = ?,
|
||||
capital = ?,
|
||||
cod_continente= ?
|
||||
WHERE identificador= ?
|
||||
""";
|
||||
sentenciaSQL = String.format(sentenciaSQL, pais.getNombre(), pais.getCapital(),
|
||||
pais.getContinente().getCodigo(), pais.getIdentificador());
|
||||
// System.out.println(sentenciaSQL);
|
||||
try (Statement sentencia = conexion.createStatement();) {
|
||||
sentencia.execute(sentenciaSQL);
|
||||
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||
sentenciaPreparada.setString(1, pais.getNombre());
|
||||
sentenciaPreparada.setString(2, pais.getCapital());
|
||||
sentenciaPreparada.setString(3, pais.getContinente().getCodigo());
|
||||
sentenciaPreparada.setInt(4, pais.getIdentificador());
|
||||
sentenciaPreparada.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -145,12 +149,12 @@ public class PaisDaoJDBC implements IPaisDAO {
|
||||
public void borrarPais(int idPais) {
|
||||
String sentenciaSQL = """
|
||||
DELETE FROM T_PAIS
|
||||
WHERE identificador = %d
|
||||
WHERE identificador = ?
|
||||
""";
|
||||
sentenciaSQL = String.format(sentenciaSQL, idPais);
|
||||
// System.out.println(sentenciaSQL);
|
||||
try (Statement sentencia = conexion.createStatement();) {
|
||||
sentencia.execute(sentenciaSQL);
|
||||
try (PreparedStatement sentenciaPreparada = conexion.prepareStatement(sentenciaSQL);) {
|
||||
sentenciaPreparada.setInt(1, idPais);
|
||||
sentenciaPreparada.execute();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user