first commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
|
target/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
*.class
|
||||||
+232
@@ -0,0 +1,232 @@
|
|||||||
|
CREATE DATABASE Mapa_Mundi;
|
||||||
|
USE Mapa_Mundi;
|
||||||
|
|
||||||
|
-- Eliminar tablas
|
||||||
|
DROP TABLE IF exists T_PAIS;
|
||||||
|
DROP TABLE IF exists T_CONTINENTE;
|
||||||
|
|
||||||
|
-- DDL de T_CONTINENTE
|
||||||
|
CREATE table IF not exists T_CONTINENTE (
|
||||||
|
codigo char(2),
|
||||||
|
nombre_continente varchar(30),
|
||||||
|
PRIMARY KEY (codigo)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- DDL de T_PAIS
|
||||||
|
CREATE TABLE IF not exists T_PAIS (
|
||||||
|
cod_continente char(2),
|
||||||
|
identificador int auto_increment,
|
||||||
|
nombre_pais varchar(50),
|
||||||
|
capital varchar(20),
|
||||||
|
PRIMARY KEY (identificador),
|
||||||
|
CONSTRAINT FK_PAIS_CONTINENTE
|
||||||
|
FOREIGN KEY (cod_continente) REFERENCES T_CONTINENTE(codigo)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Información de Continentes
|
||||||
|
INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES ("01","África");
|
||||||
|
INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES ("02","América");
|
||||||
|
INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES ("03","Asia");
|
||||||
|
INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES ("04","Europa");
|
||||||
|
INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES ("05","Oceanía");
|
||||||
|
|
||||||
|
-- Información de Paises
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",1, "Angola", "Luanda");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",2, "Argelia", "Argel");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",3, "Benin", "Porto-Novo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",4, "Botsuana", "Gaberones");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",5, "Burkina Faso", "Uagadugú");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",6, "Burundi", "Buyumbura");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",7, "Cabo Verde", "Praia");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",8, "Camerún", "Yaundé");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",9, "Chad", "Yamena");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",10, "Comoras", "Moroni");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",11, "Costa De Marfil", "Yamusukro, Abiyán");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",12, "Egipto", "El Cairo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",13, "Eritrea", "Asmara");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",14, "Etiopía", "Adís Abeba");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",15, "Gabón", "Libreville");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",16, "Gambia", "Banjul");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",17, "Ghana", "Accra");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",18, "Guinea", "Conakry");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",19, "Guinea Ecuatorial", "Malabo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",20, "Guinea-Bissau", "Bissau");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",21, "Kenia", "Nairobi");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",22, "Lesoto", "Maseru");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",23, "Liberia", "Monrovia");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",24, "Libia", "Trípoli");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",25, "Madagascar", "Antananarivo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",26, "Malaui", "Lilongüe");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",27, "Mali", "Bamako");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",28, "Marruecos", "Rabat");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",29, "Mauricio", "Port Louis");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",30, "Mauritania", "Nuakchot");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",31, "Mozambique", "Maputo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",32, "Namibia", "Windhoek");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",33, "Níger", "Niamey");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",34, "Nigeria", "Abuya");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",35, "República Centroafricana", "Bangui");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",36, "República Del Congo", "Brazzaville");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",37, "República Democrática Del Congo", "Kinshasa");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",38, "República Saharaui", "El Aaiún");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",39, "Ruanda", "Kigali");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",40, "Santo Tomé Y Príncipe", "Santo Tomé");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",41, "Senegal", "Dakar");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",42, "Seychelles", "Victoria");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",43, "Sierra Leona", "Freetown");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",44, "Somalia", "Mogadiscio");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",45, "Suazilandia", "Mbabane");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",46, "Sudáfrica","Pretoria");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",47, "Sudán Del Norte", "Jartum");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",48, "Sudán Del Sur", "Yuba");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",49, "Tanzania", "Dodoma");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",50, "Togo", "Lomé");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",51, "Túnez", "Túnez");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",52, "Uganda", "Kampala");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",53, "Yibuti", "Yibuti");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",54, "Zambia", "Lusaka");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("01",55, "Zimbabue", "Harare");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",56, "Antigua Y Barbuda", "Saint John's");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",57, "Argentina", "Buenos Aires");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",58, "Bahamas", "Nassau");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",59, "Barbados", "Bridgetown");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",60, "Belice", "Belmopán");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",61, "Bolivia", "Sucre, La Paz");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",62, "Brasil", "Brasilia");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",63, "Canadá", "Ottawa");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",64, "Chile", "Santiago De Chile");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",65, "Colombia", "Bogotá");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",66, "Costa Rica", "San José");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",67, "Cuba", "La Habana");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",68, "Dominica", "Roseau");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",69, "Ecuador", "Quito");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",70, "El Salvador", "San Salvador");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",71, "Estados Unidos de América", "Washington D. C.");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",72, "Granada", "Saint George's");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",73, "Guatemala", "Ciudad De Guatemala");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",74, "Guyana", "Georgetown");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",75, "Haití", "Puerto Príncipe");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",76, "Honduras", "Tegucigalpa");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",77, "Jamaica", "Kingston");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",78, "México", "México D. F.");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",79, "Nicaragua", "Managua");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",80, "Panamá", "Ciudad De Panamá");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",81, "Paraguay", "Asunción");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",82, "Perú", "Lima");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",83, "Puerto Rico", "San Juan");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",84, "República Dominicana", "Santo Domingo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",85, "San Cristóbal Y Nieves", "Basseterre");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",86, "San Vicente Y Las Granadinas", "Kingstown");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",87, "Santa Lucía", "Castries");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",88, "Surinam", "Paramaribo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",89, "Trinidad Y Tobago", "Puerto España");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",90, "Uruguay", "Montevideo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("02",91, "Venezuela", "Caracas");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",92, "Afganistán", "Kabul");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",93, "Arabia Saudita", "Riad");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",94, "Bangladés", "Daca");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",95, "Baréin", "Manamá");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",96, "Brunei", "Bandar Seri Begawan");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",97, "Bután", "Timbu");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",98, "Camboya", "Pnon Pehn");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",99, "Catar", "Doha");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",100, "China", "Pekín");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",101, "Chipre", "Nicosia");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",102, "Corea Del Norte", "Pyongyang");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",103, "Corea Del Sur", "Seúl");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",104, "Emiratos Arabes Unidos", "Abu Dabi");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",105, "Filipinas", "Manila");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",106, "India", "Nueva Delhi");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",107, "Indonesia", "Yakarta");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",108, "Irán", "Teherán");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",109, "Iraq", "Bagdad");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",110, "Israel", "Jerusalén");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",111, "Japón", "Tokio");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",112, "Jordania", "Ammán");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",113, "Kazajistán", "Astaná");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",114, "Kirguistán", "Biskek");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",115, "Kuwait", "Ciudad De Kuwait");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",116, "Laos", "Vientián");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",117, "Líbano", "Beirut");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",118, "Malasia","Kuala Lumpur");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",119, "Maldivas", "Malé");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",120, "Mongolia", "Ulan Bator");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",121, "Myanmar (Birmania)", "Naypyidaw");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",122, "Nepal", "Katmandú");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",123, "Omán", "Mascate");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",124, "Pakistán", "Islamabad");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",125, "Palestina", "Ramala");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",126, "Singapur", "Singapur");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",127, "Siria", "Damasco");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",128, "Sri Lanka", "Colombo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",129, "Tailandia", "Bangkok");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",130, "Taiwan", "Taipeh");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",131, "Tayikistán", "Dusambé");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",132, "Timor Oriental", "Dili");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",133, "Turkmenistán", "Asjabad");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",134, "Turquía", "Ankara");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",135, "Uzbekistán", "Tashkent");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",136, "Vietnam", "Hanoi");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("03",137, "Yemen", "Saná");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",138, "Albania", "Tirana");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",139, "Alemania", "Berlín");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",140, "Andorra", "Andorra La Vieja");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",141, "Armenia", "Ereván");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",142, "Austria", "Viena");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",143, "Azerbaiyán", "Bakú");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",144, "Bélgica", "Bruselas");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",145, "Bielorrusia", "Minsk");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",146, "Bosnia Y Herzegovina", "Sarajevo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",147, "Bulgaria", "Sofía");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",148, "Croacia", "Zagreb");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",149, "Dinamarca", "Copenhague");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",150, "Eslovaquia", "Bratislava");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",151, "Eslovenia", "Lublijana");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",152, "España", "Madrid");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",153, "Estonia", "Tallín");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",154, "Finlandia", "Helsinki");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",155, "Francia", "París");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",156, "Georgia", "Tiflis");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",157, "Grecia", "Atenas");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",158, "Hungría", "Budapest");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",159, "Irlanda", "Dublín");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",160, "Islandia", "Reikiavik");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",161, "Italia", "Roma");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",162, "Letonia", "Riga");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",163, "Liechtenstein", "Vaduz");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",164, "Lituania", "Vilna");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",165, "Luxemburgo", "Luxemburgo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",166, "Malta", "La Valeta");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",167, "Moldavia", "Chisinau");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",168, "Mónaco", "Mónaco");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",169, "Montenegro", "Podgorica");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",170, "Noruega", "Oslo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",171, "Países Bajos", "Amsterdam");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",172, "Polonia", "Varsovia");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",173, "Portugal", "Lisboa");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",174, "Reino Unido", "Londres");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",175, "República Checa", "Praga");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",176, "República De Macedonia", "Skopje");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",177, "Rumania", "Bucarest");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",178, "Rusia", "Moscú");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",179, "San Marino", "Ciudad De San Marino");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",180, "Serbia", "Belgrado");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",181, "Suecia", "Estocolmo");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",182, "Suiza", "Berna");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",183, "Ucrania", "Kiev");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("04",184, "Vaticano", "Ciudad Del Vaticano");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",185, "Australia", "Canberra");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",186, "Fiyi", "Suva");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",187, "Islas Marshall", "Majuro");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",188, "Islas Salomón", "Honiara");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",189, "Kiribati", "Tarawa");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",190, "Micronesia", "Palikir");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",191, "Nauru", "Yaren");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",192, "Nueva Zelanda", "Wellington");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",193, "Palaos", "Melekeok");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",194, "Papúa Nueva Guinea", "Port Moresby");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",195, "Samoa", "Apia");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",196, "Tonga", "Nuku'alofa");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",197, "Tuvalu", "Funafuti");
|
||||||
|
INSERT INTO T_PAIS(cod_continente, identificador, nombre_pais ,capital) VALUES ("05",198, "Vanuatu", "Port Vila");
|
||||||
|
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
# aadd_conect03: Operaciones DML con JDBC, patrón DAO, capa de servicios y transacción (MySQL)
|
||||||
|
|
||||||
|
## Descripción
|
||||||
|
|
||||||
|
Proyecto en Java que realiza operaciones básicas de manipulación de datos (DML) sobre las tablas `T_CONTINENTE` y `T_PAIS` de la base de datos `Mapa_Mundi`, administrada por MySQL. Se utiliza el API JDBC, el patrón DAO y una capa de servicios. Todas las sentencias SQL se ejecutan con `PreparedStatement`.
|
||||||
|
|
||||||
|
Es la evolución de `aadd_conect02` con cuatro novedades:
|
||||||
|
|
||||||
|
- **`PreparedStatement`** en lugar de `Statement` (refactorización de la implementación JDBC de País y nueva implementación de Continente).
|
||||||
|
- **Capa de servicios** (`PaisService` y `ContinenteService`): `AppPrincipal` ya no abre conexiones, solo llama a los servicios.
|
||||||
|
- **Excepción de aplicación** `MapaMundiException` (checked), única excepción que sale de las capas de acceso a datos y servicios.
|
||||||
|
- **Transacción** en el borrado de un continente: se borran sus países y el propio continente, o no se borra nada.
|
||||||
|
|
||||||
|
Los datos de conexión no están escritos en el código: se leen del fichero `application.properties` a través de la clase `GestorConfiguracion`. Al usar una versión moderna de JDBC, ya no hace falta indicar la clase del driver.
|
||||||
|
|
||||||
|
## Estructura del proyecto
|
||||||
|
|
||||||
|
- `aadd_conect03/`
|
||||||
|
- `src/org/palomafp/aadd/dta/`
|
||||||
|
- `AppPrincipal.java`: clase principal que ejecuta los cinco pasos del enunciado a través del servicio
|
||||||
|
- `model/`
|
||||||
|
- `Pais.java`: clase del modelo (un país)
|
||||||
|
- `Continente.java`: clase del modelo (un continente)
|
||||||
|
- `dao/`
|
||||||
|
- `IPaisDAO.java`: interfaz del DAO de países
|
||||||
|
- `IContinenteDAO.java`: interfaz del DAO de continentes
|
||||||
|
- `impl/`
|
||||||
|
- `PaisDaoJDBC.java`: implementación JDBC con `PreparedStatement`
|
||||||
|
- `ContinenteDaoJDBC.java`: implementación JDBC con `PreparedStatement`
|
||||||
|
- `service/`
|
||||||
|
- `PaisService.java`: casos de uso de países
|
||||||
|
- `ContinenteService.java`: casos de uso de continentes (incluye la transacción)
|
||||||
|
- `excepciones/`
|
||||||
|
- `MapaMundiException.java`: excepción de aplicación (checked)
|
||||||
|
- `gestores/`
|
||||||
|
- `GestorConfiguracion.java`: carga `application.properties` y expone sus valores
|
||||||
|
- `GestorConexionBBDD.java`: crea las conexiones a la base de datos
|
||||||
|
- `config/`
|
||||||
|
- `application.properties`: configuración (datos de conexión a la BBDD)
|
||||||
|
- `MapaMundi.sql`: script de creación de la base de datos
|
||||||
|
- `pom.xml`
|
||||||
|
- `README.md`
|
||||||
|
|
||||||
|
## Base de datos
|
||||||
|
|
||||||
|
El script `MapaMundi.sql` crea la base de datos `Mapa_Mundi` con dos tablas:
|
||||||
|
|
||||||
|
| Tabla | Columnas |
|
||||||
|
|---|---|
|
||||||
|
| `T_CONTINENTE` | `codigo` (PK), `nombre_continente` |
|
||||||
|
| `T_PAIS` | `cod_continente` (FK a `T_CONTINENTE`), `identificador` (PK, autoincremental), `nombre_pais`, `capital` |
|
||||||
|
|
||||||
|
Contiene 5 continentes y 198 países (47 de ellos en Europa, código `04`).
|
||||||
|
|
||||||
|
Como `T_PAIS.cod_continente` es clave foránea de `T_CONTINENTE.codigo`, **no se puede borrar un continente que tenga países**. Por eso el borrado se hace en orden: primero los países (hijos) y después el continente (padre).
|
||||||
|
|
||||||
|
## Arquitectura en capas
|
||||||
|
|
||||||
|
```
|
||||||
|
AppPrincipal → Service → DAO → Base de datos
|
||||||
|
```
|
||||||
|
|
||||||
|
| Capa | Responsabilidad |
|
||||||
|
|---|---|
|
||||||
|
| `AppPrincipal` | Decide qué se ejecuta y qué se imprime. No conoce conexiones ni SQL |
|
||||||
|
| Servicio | Representa los casos de uso. Abre y cierra la conexión y controla la transacción |
|
||||||
|
| DAO | Ejecuta las sentencias SQL de una tabla. Recibe la conexión por constructor y no la cierra |
|
||||||
|
| Modelo | Clases que representan una fila de cada tabla |
|
||||||
|
|
||||||
|
## Componentes principales
|
||||||
|
|
||||||
|
- **`Pais` y `Continente`**: clases del modelo con sus atributos, getters, setters y `toString()`.
|
||||||
|
- **`IPaisDAO` e `IContinenteDAO`**: interfaces con las operaciones de acceso a datos. Todos sus métodos declaran `throws MapaMundiException`.
|
||||||
|
- **`PaisDaoJDBC` y `ContinenteDaoJDBC`**: implementaciones JDBC. Reciben la `Connection` por constructor y la guardan en un atributo `private final`. Cada método crea un `PreparedStatement` con `try-with-resources`. Las `SQLException` se capturan y se relanzan envueltas en `MapaMundiException`, conservando la causa original.
|
||||||
|
- **`PaisService` y `ContinenteService`**: abren la conexión con `try-with-resources` (se cierra sola), crean el DAO pasándosela y delegan en él. Los fallos al abrir o cerrar la conexión también se convierten en `MapaMundiException`.
|
||||||
|
- **`MapaMundiException`**: excepción checked con dos constructores (`mensaje` y `mensaje, causa`). Es la única que conocen las capas superiores.
|
||||||
|
- **`GestorConfiguracion`**: clase de utilidad (constructor privado) que carga `application.properties` en un bloque `static` y ofrece un getter por propiedad.
|
||||||
|
- **`GestorConexionBBDD`**: clase de utilidad que monta la URL (`sgdb.conexion.url` + `bbdd.nombre`) y devuelve una `Connection` nueva. Quien la recibe es responsable de cerrarla.
|
||||||
|
- **`AppPrincipal`**: crea el `ContinenteService` y ejecuta los cinco pasos, cada uno en un método privado. Captura `MapaMundiException` en un único punto.
|
||||||
|
|
||||||
|
## Configuración
|
||||||
|
|
||||||
|
Fichero `application.properties`:
|
||||||
|
|
||||||
|
```properties
|
||||||
|
# Información relativa a la base de datos
|
||||||
|
sgdb.conexion.url=jdbc:mysql://192.168.1.62:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=myuser
|
||||||
|
bbdd.clave=mypass
|
||||||
|
```
|
||||||
|
|
||||||
|
La URL de conexión se forma concatenando `sgdb.conexion.url` y `bbdd.nombre`, es decir, `jdbc:mysql://192.168.1.62:3306/Mapa_Mundi`. Para ejecutar el proyecto en otro equipo hay que cambiar la IP (o usar `localhost`) y las credenciales.
|
||||||
|
|
||||||
|
## Métodos de `IPaisDAO` / `PaisDaoJDBC`
|
||||||
|
|
||||||
|
| Método | Sentencia SQL | Devuelve |
|
||||||
|
|---|---|---|
|
||||||
|
| `obtenerTodos()` | `SELECT` de todos los países | `List<Pais>` |
|
||||||
|
| `obtenerPorId(int identificador)` | `SELECT ... WHERE identificador = ?` | `Pais`, o `null` si no existe |
|
||||||
|
| `insertar(Pais pais)` | `INSERT` con los cuatro campos del país | nada |
|
||||||
|
| `actualizar(Pais pais)` | `UPDATE` de continente, nombre y capital por identificador | nada |
|
||||||
|
| `borrar(int identificador)` | `DELETE ... WHERE identificador = ?` | nada |
|
||||||
|
|
||||||
|
## Métodos de `IContinenteDAO` / `ContinenteDaoJDBC`
|
||||||
|
|
||||||
|
| Método | Sentencia SQL | Devuelve |
|
||||||
|
|---|---|---|
|
||||||
|
| `obtenerTodos()` | `SELECT` de todos los continentes | `List<Continente>` |
|
||||||
|
| `obtenerPorCodigo(String codigo)` | `SELECT ... WHERE codigo = ?` | `Continente`, o `null` si no existe |
|
||||||
|
| `insertar(Continente continente)` | `INSERT` con código y nombre | nada |
|
||||||
|
| `actualizar(Continente continente)` | `UPDATE` del nombre por código | nada |
|
||||||
|
| `borrar(String codigo)` | `DELETE ... WHERE codigo = ?` | nada |
|
||||||
|
|
||||||
|
## Métodos de los servicios
|
||||||
|
|
||||||
|
`PaisService` ofrece `obtenerTodos`, `obtenerPorId`, `insertar`, `actualizar` y `borrar`. `ContinenteService` ofrece:
|
||||||
|
|
||||||
|
| Método | Acción |
|
||||||
|
|---|---|
|
||||||
|
| `obtenerTodos()` | Devuelve todos los continentes |
|
||||||
|
| `obtenerPorCodigo(String codigo)` | Devuelve un continente, o `null` si no existe |
|
||||||
|
| `insertar(Continente continente)` | Inserta un continente |
|
||||||
|
| `actualizar(Continente continente)` | Actualiza el nombre de un continente |
|
||||||
|
| `borrarConPaises(String codigo)` | Borra los países del continente y después el continente, **dentro de una misma transacción** |
|
||||||
|
|
||||||
|
Las operaciones simples usan una conexión en modo *autocommit*: cada sentencia se confirma sola.
|
||||||
|
|
||||||
|
## Transacción en `borrarConPaises`
|
||||||
|
|
||||||
|
El enunciado exige que el borrado de los países y el del continente se ejecuten juntos: si una falla, se deshacen todas.
|
||||||
|
|
||||||
|
1. Se abre **una sola conexión** y se desactiva el *autocommit* (`setAutoCommit(false)`), lo que inicia la transacción.
|
||||||
|
2. Se crean `PaisDaoJDBC` y `ContinenteDaoJDBC` con **esa misma conexión**. Es imprescindible: una transacción vive dentro de una conexión, y por eso los DAO la reciben por constructor en lugar de abrirla ellos.
|
||||||
|
3. Se recorren los países y se borran uno a uno los del continente indicado.
|
||||||
|
4. Se borra el continente.
|
||||||
|
5. `commit()` confirma los dos borrados a la vez.
|
||||||
|
6. Si un DAO lanza `MapaMundiException`, se hace `rollback()` (los países vuelven) y se relanza la misma excepción hacia `main`.
|
||||||
|
7. Cualquier `SQLException` de abrir, confirmar o cerrar la conexión se convierte en `MapaMundiException`.
|
||||||
|
|
||||||
|
## Métodos de `AppPrincipal`
|
||||||
|
|
||||||
|
| Método | Acción |
|
||||||
|
|---|---|
|
||||||
|
| `main(String[] args)` | Crea el servicio y ejecuta los cinco pasos en orden. Si el continente 04 no existe, termina sin borrar nada. Captura `MapaMundiException` en un único punto |
|
||||||
|
| `obtenerContinentes(servicio)` | Paso 1. Consulta la lista de continentes y la imprime |
|
||||||
|
| `obtenerContinentePorCodigo(servicio)` | Paso 2. Consulta el continente 04, lo imprime y lo devuelve para los pasos 4 y 5 |
|
||||||
|
| `borrarContinente(servicio)` | Paso 3. Borra los países del continente y después el continente (transacción) y comprueba que ya no existe |
|
||||||
|
| `insertarContinente(servicio, continente)` | Paso 4. Inserta el continente obtenido en el paso 2 |
|
||||||
|
| `actualizarContinente(servicio, continente)` | Paso 5. Cambia el nombre a "Paneuropa" y actualiza el continente |
|
||||||
|
|
||||||
|
Los valores `"04"` y `"Paneuropa"` están definidos como constantes (`CODIGO_CONTINENTE` y `NUEVO_NOMBRE`).
|
||||||
|
|
||||||
|
## Pasos del programa
|
||||||
|
|
||||||
|
| Paso | Operación | Resultado esperado |
|
||||||
|
|---|---|---|
|
||||||
|
| 1 | Consultar e imprimir la lista de continentes | 5 continentes |
|
||||||
|
| 2 | Consultar e imprimir el continente 04 | `Continente [codigo=04, nombreContinente=Europa]` |
|
||||||
|
| 3 | Borrar los países del continente 04 y después el continente | `Consulta tras borrar: null` |
|
||||||
|
| 4 | Insertar el continente del paso 2 | La consulta devuelve Europa |
|
||||||
|
| 5 | Actualizar el nombre | La consulta devuelve `Paneuropa` |
|
||||||
|
|
||||||
|
## Salida por consola
|
||||||
|
|
||||||
|
Salida obtenida al ejecutar `AppPrincipal` con la base de datos recién restaurada:
|
||||||
|
|
||||||
|
```
|
||||||
|
=== 1. Lista de continentes ===
|
||||||
|
Continente [codigo=01, nombreContinente=África]
|
||||||
|
Continente [codigo=02, nombreContinente=América]
|
||||||
|
Continente [codigo=03, nombreContinente=Asia]
|
||||||
|
Continente [codigo=04, nombreContinente=Europa]
|
||||||
|
Continente [codigo=05, nombreContinente=Oceanía]
|
||||||
|
|
||||||
|
=== 2. Continente con código 04 ===
|
||||||
|
Continente [codigo=04, nombreContinente=Europa]
|
||||||
|
|
||||||
|
=== 3. Borrado ===
|
||||||
|
Consulta tras borrar: null
|
||||||
|
|
||||||
|
=== 4. Inserción ===
|
||||||
|
Consulta tras insertar: Continente [codigo=04, nombreContinente=Europa]
|
||||||
|
|
||||||
|
=== 5. Actualización ===
|
||||||
|
Consulta tras actualizar: Continente [codigo=04, nombreContinente=Paneuropa]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Aspectos a tener en cuenta
|
||||||
|
|
||||||
|
- El continente insertado en el paso 4 es el mismo objeto `Continente` consultado en el paso 2, guardado en memoria antes de borrarlo. Por eso el paso 2 debe ir antes del 3.
|
||||||
|
- El paso 4 solo reinserta el continente, no sus países: tras la ejecución, `Paneuropa` queda **sin países** (`0` filas en `T_PAIS` con `cod_continente = '04'`).
|
||||||
|
- El borrado de países se hace listando todos y filtrando por continente en el servicio, de forma que los DAO se mantienen en los cinco métodos básicos y el servicio decide el orden y la transacción.
|
||||||
|
- `PreparedStatement` evita la inyección SQL y resuelve el problema de los textos con apóstrofo (por ejemplo `Nuku'alofa`), que con `Statement` rompían el `INSERT` o el `UPDATE`.
|
||||||
|
- Los DAO no abren ni cierran la conexión; lo hace el servicio. `AppPrincipal` no conoce JDBC.
|
||||||
|
- `PaisService` está completo, pero `AppPrincipal` solo usa `ContinenteService`, porque el enunciado solo pide operaciones sobre continentes.
|
||||||
|
- La carpeta `config` está configurada como *source folder* en Eclipse, por lo que `application.properties` está en el classpath.
|
||||||
|
- Tras la ejecución, Europa queda con el nombre `Paneuropa` y sin países. Para volver al estado original hay que ejecutar de nuevo `MapaMundi.sql`.
|
||||||
|
|
||||||
|
## Prueba del rollback
|
||||||
|
|
||||||
|
Para comprobar que la transacción deshace los cambios:
|
||||||
|
|
||||||
|
1. Restaurar los datos ejecutando `MapaMundi.sql`.
|
||||||
|
2. Añadir temporalmente en `ContinenteService.borrarConPaises`, justo antes de `continenteDao.borrar(codigo);`:
|
||||||
|
```java
|
||||||
|
if (true) {
|
||||||
|
throw new MapaMundiException("fallo de prueba");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
3. Ejecutar `AppPrincipal`. Aparece `Error: fallo de prueba` y el programa termina.
|
||||||
|
4. Comprobar en la base de datos que los países siguen ahí:
|
||||||
|
```sql
|
||||||
|
SELECT COUNT(*) FROM T_PAIS WHERE cod_continente = '04';
|
||||||
|
```
|
||||||
|
Debe devolver `47`.
|
||||||
|
5. Eliminar el `throw` de prueba y restaurar los datos.
|
||||||
|
|
||||||
|
## Ejecución
|
||||||
|
|
||||||
|
1. Ejecutar `MapaMundi.sql` en un cliente MySQL (por ejemplo DBeaver) para crear la base de datos.
|
||||||
|
2. Crear el usuario con permisos sobre la base de datos:
|
||||||
|
```sql
|
||||||
|
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';
|
||||||
|
GRANT ALL PRIVILEGES ON Mapa_Mundi.* TO 'myuser'@'%';
|
||||||
|
FLUSH PRIVILEGES;
|
||||||
|
```
|
||||||
|
3. Ajustar `config/application.properties` a la IP y credenciales del servidor MySQL.
|
||||||
|
4. Ejecutar `AppPrincipal` como aplicación Java.
|
||||||
|
5. Comprobar en la consola la salida de los cinco pasos.
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# Información relativa a la base de datos
|
||||||
|
sgdb.conexion.url=jdbc:mysql://192.168.1.62:3306/
|
||||||
|
bbdd.nombre=Mapa_Mundi
|
||||||
|
bbdd.usuario=myuser
|
||||||
|
bbdd.clave=mypass
|
||||||
@@ -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_conect03</groupId>
|
||||||
|
<artifactId>aadd_conect03</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>26</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package org.palomafp.aadd.dta;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
import org.palomafp.aadd.dta.service.ContinenteService;
|
||||||
|
|
||||||
|
public class AppPrincipal {
|
||||||
|
|
||||||
|
private static final String CODIGO_CONTINENTE = "04";
|
||||||
|
private static final String NUEVO_NOMBRE = "Paneuropa";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ContinenteService servicio = new ContinenteService();
|
||||||
|
|
||||||
|
try {
|
||||||
|
obtenerContinentes(servicio);
|
||||||
|
|
||||||
|
Continente continente = obtenerContinentePorCodigo(servicio);
|
||||||
|
if (continente == null) {
|
||||||
|
System.out.println("No existe el continente " + CODIGO_CONTINENTE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
borrarContinente(servicio);
|
||||||
|
insertarContinente(servicio, continente);
|
||||||
|
actualizarContinente(servicio, continente);
|
||||||
|
|
||||||
|
} catch (MapaMundiException e) {
|
||||||
|
System.err.println("Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Consultar la lista de continentes e imprimirla
|
||||||
|
private static void obtenerContinentes(ContinenteService servicio) throws MapaMundiException {
|
||||||
|
System.out.println("=== 1. Lista de continentes ===");
|
||||||
|
List<Continente> listaContinentes = servicio.obtenerTodos();
|
||||||
|
for (Continente continente : listaContinentes) {
|
||||||
|
System.out.println(continente);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Consultar el continente 04 e imprimirlo
|
||||||
|
private static Continente obtenerContinentePorCodigo(ContinenteService servicio) throws MapaMundiException {
|
||||||
|
System.out.println("\n=== 2. Continente con código " + CODIGO_CONTINENTE + " ===");
|
||||||
|
Continente continente = servicio.obtenerPorCodigo(CODIGO_CONTINENTE);
|
||||||
|
System.out.println(continente);
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Borrar los países del continente y después el continente (transacción)
|
||||||
|
private static void borrarContinente(ContinenteService servicio) throws MapaMundiException {
|
||||||
|
System.out.println("\n=== 3. Borrado ===");
|
||||||
|
servicio.borrarConPaises(CODIGO_CONTINENTE);
|
||||||
|
System.out.println("Consulta tras borrar: " + servicio.obtenerPorCodigo(CODIGO_CONTINENTE));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Insertar el continente obtenido en el punto 2
|
||||||
|
private static void insertarContinente(ContinenteService servicio, Continente continente) throws MapaMundiException {
|
||||||
|
System.out.println("\n=== 4. Inserción ===");
|
||||||
|
servicio.insertar(continente);
|
||||||
|
System.out.println("Consulta tras insertar: " + servicio.obtenerPorCodigo(CODIGO_CONTINENTE));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Actualizar el nombre del continente
|
||||||
|
private static void actualizarContinente(ContinenteService servicio, Continente continente) throws MapaMundiException {
|
||||||
|
System.out.println("\n=== 5. Actualización ===");
|
||||||
|
continente.setNombreContinente(NUEVO_NOMBRE);
|
||||||
|
servicio.actualizar(continente);
|
||||||
|
System.out.println("Consulta tras actualizar: " + servicio.obtenerPorCodigo(CODIGO_CONTINENTE));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
List<Continente> obtenerTodos() throws MapaMundiException;
|
||||||
|
Continente obtenerPorCodigo(String codigo) throws MapaMundiException;
|
||||||
|
void insertar(Continente continente) throws MapaMundiException;
|
||||||
|
void actualizar(Continente continente) throws MapaMundiException;
|
||||||
|
void borrar(String codigo) throws MapaMundiException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
List<Pais> obtenerTodos() throws MapaMundiException;
|
||||||
|
Pais obtenerPorId(int identificador) throws MapaMundiException;
|
||||||
|
void insertar(Pais pais) throws MapaMundiException;
|
||||||
|
void actualizar(Pais pais) throws MapaMundiException;
|
||||||
|
void borrar(int identificador) throws MapaMundiException;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao.impl;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoJDBC implements IContinenteDAO {
|
||||||
|
|
||||||
|
private final Connection conexion;
|
||||||
|
|
||||||
|
public ContinenteDaoJDBC(Connection conexion) {
|
||||||
|
this.conexion = conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerTodos() throws MapaMundiException {
|
||||||
|
List<Continente> lista = new ArrayList<>();
|
||||||
|
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql);
|
||||||
|
ResultSet rs = ps.executeQuery()) {
|
||||||
|
while(rs.next()) {
|
||||||
|
Continente continente = new Continente();
|
||||||
|
continente.setCodigo(rs.getString("codigo"));
|
||||||
|
continente.setNombreContinente(rs.getString("nombre_continente"));
|
||||||
|
lista.add(continente);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener los continentes", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerPorCodigo(String codigo) throws MapaMundiException {
|
||||||
|
Continente continente = null;
|
||||||
|
String sql = "SELECT codigo, nombre_continente FROM T_CONTINENTE WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, codigo);
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
continente = new Continente();
|
||||||
|
continente.setCodigo(rs.getString("codigo"));
|
||||||
|
continente.setNombreContinente(rs.getString("nombre_continente"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertar(Continente continente) throws MapaMundiException {
|
||||||
|
String sql = "INSERT INTO T_CONTINENTE (codigo, nombre_continente) VALUES (?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, continente.getCodigo());
|
||||||
|
ps.setString(2, continente.getNombreContinente());
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizar(Continente continente) throws MapaMundiException {
|
||||||
|
String sql = "UPDATE T_CONTINENTE SET nombre_continente = ? WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, continente.getNombreContinente());
|
||||||
|
ps.setString(2, continente.getCodigo());
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrar(String codigo) throws MapaMundiException {
|
||||||
|
String sql = "DELETE FROM T_CONTINENTE WHERE codigo = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, codigo);
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
package org.palomafp.aadd.dta.dao.impl;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoJDBC implements IPaisDAO {
|
||||||
|
|
||||||
|
private final Connection conexion;
|
||||||
|
|
||||||
|
public PaisDaoJDBC(Connection conexion) {
|
||||||
|
this.conexion = conexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerTodos() throws MapaMundiException {
|
||||||
|
List<Pais> lista = new ArrayList<>();
|
||||||
|
String sql = "SELECT cod_continente, identificador, nombre_pais, capital FROM T_PAIS";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
|
||||||
|
while (rs.next()) {
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setCodContinente(rs.getString("cod_continente"));
|
||||||
|
pais.setIdentificador(rs.getInt("identificador"));
|
||||||
|
pais.setNombrePais(rs.getString("nombre_pais"));
|
||||||
|
pais.setCapital(rs.getString("capital"));
|
||||||
|
lista.add(pais);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener los países", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPorId(int identificador) throws MapaMundiException {
|
||||||
|
Pais pais = null;
|
||||||
|
String sql = "SELECT cod_continente, identificador, nombre_pais, capital FROM T_PAIS WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setInt(1, identificador);
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
pais = new Pais();
|
||||||
|
pais.setCodContinente(rs.getString("cod_continente"));
|
||||||
|
pais.setIdentificador(rs.getInt("identificador"));
|
||||||
|
pais.setNombrePais(rs.getString("nombre_pais"));
|
||||||
|
pais.setCapital(rs.getString("capital"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al obtener el país " + identificador, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertar(Pais pais) throws MapaMundiException {
|
||||||
|
String sql = "INSERT INTO T_PAIS (cod_continente, identificador, nombre_pais, capital) VALUES (?, ?, ?, ?)";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, pais.getCodContinente());
|
||||||
|
ps.setInt(2, pais.getIdentificador());
|
||||||
|
ps.setString(3, pais.getNombrePais());
|
||||||
|
ps.setString(4, pais.getCapital());
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al insertar el país " + pais.getNombrePais(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actualizar(Pais pais) throws MapaMundiException {
|
||||||
|
String sql = "UPDATE T_PAIS SET cod_continente = ?, nombre_pais = ?, capital = ? WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, pais.getCodContinente());
|
||||||
|
ps.setString(2, pais.getNombrePais());
|
||||||
|
ps.setString(3, pais.getCapital());
|
||||||
|
ps.setInt(4, pais.getIdentificador());
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al actualizar el país " + pais.getIdentificador(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void borrar(int identificador) throws MapaMundiException {
|
||||||
|
String sql = "DELETE FROM T_PAIS WHERE identificador = ?";
|
||||||
|
|
||||||
|
try (PreparedStatement ps = conexion.prepareStatement(sql)) {
|
||||||
|
ps.setInt(1, identificador);
|
||||||
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error al borrar el país " + identificador, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.palomafp.aadd.dta.excepciones;
|
||||||
|
|
||||||
|
public class MapaMundiException extends Exception {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.palomafp.aadd.dta.gestores;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class GestorConexionBBDD {
|
||||||
|
|
||||||
|
private GestorConexionBBDD() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConexion() throws SQLException {
|
||||||
|
String url = GestorConfiguracion.getConexionUrl() + GestorConfiguracion.getNombreBBDD();
|
||||||
|
String usuario = GestorConfiguracion.getUsuarioBBDD();
|
||||||
|
String clave = GestorConfiguracion.getClaveBBDD();
|
||||||
|
|
||||||
|
return DriverManager.getConnection(url, usuario, clave);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package org.palomafp.aadd.dta.gestores;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class GestorConfiguracion {
|
||||||
|
|
||||||
|
private static final String CONFIG = "application.properties";
|
||||||
|
private static final Properties PROPS = new Properties();
|
||||||
|
|
||||||
|
static {
|
||||||
|
try (var is = GestorConfiguracion.class.getClassLoader().getResourceAsStream(CONFIG)){
|
||||||
|
if (is == null) {
|
||||||
|
throw new IllegalStateException("No se encuentra " + CONFIG + " en el classpath.");
|
||||||
|
}
|
||||||
|
PROPS.load(is);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("No se pudo leer " + CONFIG, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private GestorConfiguracion( ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getConexionUrl() {
|
||||||
|
return PROPS.getProperty("sgdb.conexion.url");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getNombreBBDD() {
|
||||||
|
return PROPS.getProperty("bbdd.nombre");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUsuarioBBDD() {
|
||||||
|
return PROPS.getProperty("bbdd.usuario");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getClaveBBDD() {
|
||||||
|
return PROPS.getProperty("bbdd.clave");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package org.palomafp.aadd.dta.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombreContinente;
|
||||||
|
|
||||||
|
public Continente() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente(String codigo, String nombreContinente) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombreContinente = 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 + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.palomafp.aadd.dta.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 + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package org.palomafp.aadd.dta.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.impl.ContinenteDaoJDBC;
|
||||||
|
import org.palomafp.aadd.dta.dao.impl.PaisDaoJDBC;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.dta.gestores.GestorConexionBBDD;
|
||||||
|
import org.palomafp.aadd.dta.model.Continente;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public class ContinenteService {
|
||||||
|
|
||||||
|
public List<Continente> obtenerTodos() throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
|
||||||
|
return continenteDao.obtenerTodos();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al obtener los continentes", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Continente obtenerPorCodigo(String codigo) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
|
||||||
|
return continenteDao.obtenerPorCodigo(codigo);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al obtener el continente " + codigo, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertar(Continente continente) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
|
||||||
|
continenteDao.insertar(continente);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al insertar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizar(Continente continente) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
|
||||||
|
continenteDao.actualizar(continente);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al actualizar el continente " + continente.getCodigo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Borra los países del continente y después el continente, todo dentro de una misma transacción.
|
||||||
|
*/
|
||||||
|
public void borrarConPaises(String codigo) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
try {
|
||||||
|
conexion.setAutoCommit(false);
|
||||||
|
|
||||||
|
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
|
||||||
|
IContinenteDAO continenteDao = new ContinenteDaoJDBC(conexion);
|
||||||
|
|
||||||
|
// 1. Borrar los países del continente
|
||||||
|
for (Pais pais : paisDao.obtenerTodos()) {
|
||||||
|
if (codigo.equals(pais.getCodContinente())) {
|
||||||
|
paisDao.borrar(pais.getIdentificador());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Borrar el continente
|
||||||
|
continenteDao.borrar(codigo);
|
||||||
|
|
||||||
|
// 3. Confirmar los dos borrados a la vez
|
||||||
|
conexion.commit();
|
||||||
|
|
||||||
|
} catch (MapaMundiException e) {
|
||||||
|
conexion.rollback(); // Si algo falla deshace todo
|
||||||
|
throw e; // Se lanza la misma excepción que se atrapó hacia arriba (al main)
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error en el borrado del continente " + codigo, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package org.palomafp.aadd.dta.service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.dta.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.dta.dao.impl.PaisDaoJDBC;
|
||||||
|
import org.palomafp.aadd.dta.excepciones.MapaMundiException;
|
||||||
|
import org.palomafp.aadd.dta.gestores.GestorConexionBBDD;
|
||||||
|
import org.palomafp.aadd.dta.model.Pais;
|
||||||
|
|
||||||
|
public class PaisService {
|
||||||
|
|
||||||
|
public List<Pais> obtenerTodos() throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
|
||||||
|
return paisDao.obtenerTodos();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al obtener los países", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pais obtenerPorId(int identificador) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
|
||||||
|
return paisDao.obtenerPorId(identificador);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al obtener el país " + identificador, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertar(Pais pais) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
|
||||||
|
paisDao.insertar(pais);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al insertar el país " + pais.getNombrePais(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actualizar(Pais pais) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
|
||||||
|
paisDao.actualizar(pais);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al actualizar el país " + pais.getIdentificador(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void borrar(int identificador) throws MapaMundiException {
|
||||||
|
try (Connection conexion = GestorConexionBBDD.getConexion()) {
|
||||||
|
IPaisDAO paisDao = new PaisDaoJDBC(conexion);
|
||||||
|
paisDao.borrar(identificador);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapaMundiException("Error de conexión al borrar el país " + identificador, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user