-
๐ฅPJT๋ช : Enjoy Trip Project
-
๐ถโโ๏ธ๋จ๊ณ: DB PJT
-
๐ ์งํ์ผ์: 2024.04.12
-
๐ฉโ๐ปํ์: ๊น์ง์, ์ ๋ฏผ๊ฒฝ
-
- ์๊ตฌ์ฌํญ์ ๋ถ์ํ๊ณ DB ๋ชจ๋ธ๋ง์ ํ ์ ์๋ค.
- DB ๋ชจ๋ธ๋ง ๋ ์ํฐํฐ๋ฅผ ํ ์ด๋ธ๋ก ๊ตฌ์ถํ ์ ์๋ค.
- EnjoyTrip ํ๋ก์ ํธ์ DB ์์ญ์ ์ญํ ์ ์ดํดํ๊ณ , DAO ํด๋์ค์ DB ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ํ์ฌ ๊ตฌํํ ์ ์๋ค.
- ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ์ฌ ์๋น์ค๋ฅผ ์ ๊ณตํ๋ค.
- ์ง์ญ๋ณ, ์ปจํ ์ธ ๋ณ ๊ด๊ด์ง ์ ๋ณด Table์ ์ค๊ณํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๊ณ , ์กฐํํ ์ ์๋ DAO ํด๋์ค ๊ตฌํ.
- ํ์์ ๋ณด๋ฅผ ๊ด๋ฆฌํ ์ ์๋ Table ์ค๊ณ ๋ฐ DAO ํด๋์ค ๊ตฌํ.
- ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ์ฌ ๊ฒ์ํ ์ ๋ ฌ ์๋น์ค ๊ตฌํ.
- ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ์ ์ด์ฉํ์ฌ ๊ด๊ด์ง ์ต๋จ ๊ฒฝ๋ก ์ถ์ฒ ์๋น์ค ๊ตฌํ.
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema excuseMe
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema excuseMe
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `excuseMe` DEFAULT CHARACTER SET utf8 ;
USE `excuseMe` ;
-- -----------------------------------------------------
-- Table `excuseMe`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `excuseMe`.`user` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR(16) NOT NULL,
`password` VARCHAR(20) NOT NULL,
`email` VARCHAR(45) NOT NULL,
`phone` VARCHAR(45) NOT NULL,
`role` ENUM("user", "admin") NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC) VISIBLE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `excuseMe`.`board`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `excuseMe`.`board` (
`user_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
`created_date` TIMESTAMP NOT NULL,
`view` INT NOT NULL,
`like` INT NOT NULL,
`title` VARCHAR(100) NOT NULL,
`contents` TEXT NOT NULL,
INDEX `user_id_idx` (`user_id` ASC) VISIBLE,
PRIMARY KEY (`id`),
CONSTRAINT `board_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `excuseMe`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `excuseMe`.`comment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `excuseMe`.`comment` (
`post_id` INT NOT NULL,
`user_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
`created_date` TIMESTAMP NOT NULL,
`like` INT NOT NULL,
`contents` TEXT NOT NULL,
INDEX `post_id_idx` (`post_id` ASC) VISIBLE,
PRIMARY KEY (`id`),
INDEX `user_id_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `commnet_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `excuseMe`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `comment_post_id`
FOREIGN KEY (`post_id`)
REFERENCES `excuseMe`.`board` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `excuseMe`.`tour`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `excuseMe`.`tour` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`addr` VARCHAR(200) NOT NULL,
`tel` VARCHAR(200) NOT NULL,
`img` VARCHAR(2083) NOT NULL,
`type` VARCHAR(8) NOT NULL,
`map_x` FLOAT NOT NULL,
`map_y` FLOAT NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `excuseMe`.`tour_like`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `excuseMe`.`tour_like` (
`tour_id` INT NOT NULL,
`user_id` INT NOT NULL,
INDEX `use_id_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `like_tour_id`
FOREIGN KEY (`tour_id`)
REFERENCES `excuseMe`.`tour` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `like_use_id`
FOREIGN KEY (`user_id`)
REFERENCES `excuseMe`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `excuseMe`.`tour_route`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `excuseMe`.`tour_route` (
`user_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
`len` INT NOT NULL,
`list` TEXT NOT NULL,
INDEX `user_id_idx` (`user_id` ASC) VISIBLE,
PRIMARY KEY (`id`),
CONSTRAINT `route_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `excuseMe`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
package com.ssafy.trip.db;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.ssafy.dbUtil.DButil;
public class UpdateTripDB {
public static void main(String[] args) throws IOException {
StringBuilder urlBuilder = new StringBuilder("https://apis.data.go.kr/B551011/KorService1/areaBasedList1"); /*URL*/
urlBuilder.append("?" + URLEncoder.encode("serviceKey","UTF-8") + "=nFhFOGxQE8IEkx4ZLuIMOGL9XEg%2Bzhxc2%2BK3Bl%2FfF8woFKQSXtW9KybG%2Bg8F593hJXGLw9USKI9vEzbnX6np7w%3D%3D"); /*Service Key*/
urlBuilder.append("&" + URLEncoder.encode("numOfRows","UTF-8") + "=" + URLEncoder.encode("1000", "UTF-8")); /*ํ ํ์ด์ง ๊ฒฐ๊ณผ ์*/
urlBuilder.append("&" + URLEncoder.encode("MobileOS","UTF-8") + "=" + URLEncoder.encode("ETC", "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("MobileApp","UTF-8") + "=" + URLEncoder.encode("AppTest", "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("_type","UTF-8") + "=" + URLEncoder.encode("xml", "UTF-8"));
for(int pageNum = 1; pageNum<=52;pageNum++) {
StringBuilder urlTmp = new StringBuilder(urlBuilder.toString());
urlTmp.append("&" + URLEncoder.encode("pageNo","UTF-8") + "=" + URLEncoder.encode(Integer.toString(pageNum), "UTF-8")); /*ํ์ด์ง๋ฒํธ*/
URL url = new URL(urlTmp.toString());
System.out.println(urlTmp.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
System.out.println("Response code: " + conn.getResponseCode());
BufferedReader rd;
if(conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) {
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
System.out.println(sb.toString());
Connection sqlConn = null;
PreparedStatement pstmt = null;
System.out.println(urlTmp.toString());
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(sb.toString())));
NodeList nodeList = document.getElementsByTagName("item");
sqlConn = DButil.getInstance().getConnection();
StringBuilder sql = new StringBuilder();
sql.append("insert into tour (name,addr,tel,img,type,map_x,map_y) \n");
sql.append("values (?,?,?,?,?,?,?)");
pstmt = sqlConn.prepareStatement(sql.toString());
int len = nodeList.getLength();
for(int i=0;i<len;i++) {
Node node = nodeList.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("---------------------------------------------");
System.out.println();
System.out.println("๊ด๊ด์ง : " + element.getElementsByTagName("title").item(0).getTextContent());
System.out.println("๊ด๊ด์ง ์ฅ์ 1 : " + element.getElementsByTagName("addr1").item(0).getTextContent());
System.out.println("๊ด๊ด์ง ์ฅ์ 2 : " + element.getElementsByTagName("contenttypeid").item(0).getTextContent());
System.out.println("์ ํ๋ฒํธ : " + element.getElementsByTagName("tel").item(0).getTextContent());
System.out.println("์ด๋ฏธ์ง : " + element.getElementsByTagName("firstimage").item(0).getTextContent());
pstmt.setString(1,element.getElementsByTagName("title").item(0).getTextContent());
pstmt.setString(2,element.getElementsByTagName("addr1").item(0).getTextContent());
pstmt.setString(3,element.getElementsByTagName("tel").item(0).getTextContent());
pstmt.setString(4,element.getElementsByTagName("firstimage").item(0).getTextContent());
pstmt.setString(5,element.getElementsByTagName("contenttypeid").item(0).getTextContent());
pstmt.setString(6,element.getElementsByTagName("mapx").item(0).getTextContent());
pstmt.setString(7,element.getElementsByTagName("mapy").item(0).getTextContent());
pstmt.executeUpdate();
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch(SAXException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- compareByLatest() : ๋น๊ตํจ์๋ฅผ ์์ฑํ์ฌ ์ต์ ์์ผ๋ก ์ ๋ ฌ
- quickSort() : ํต ์ ๋ ฌ ํจ์ ์์ฑ
- sortByLatest() : '์ต์ ์'์ ์ ํํ ๊ฒฝ์ฐ, 1๋ฒ ์คํ, 2๋ฒ ์คํํ์ฌ ๊ฒ์ํ ๋ชฉ๋ก ํ์ด์ง ๊ฐฑ์
- ์กฐํ์ (popular)
- comparePopular() : ๋น๊ตํจ์๋ฅผ ์์ฑํ์ฌ ์กฐํ์์ผ๋ก ์ ๋ ฌ
- quickSort() : ํต ์ ๋ ฌ ํจ์ ์์ฑ
- sortByPopular() : '์กฐํ์'์ ์ ํํ ๊ฒฝ์ฐ, 1๋ฒ ์คํ, 2๋ฒ ์คํํ์ฌ ๊ฒ์ํ ๋ชฉ๋ก ํ์ด์ง ๊ฐฑ์
- ๋๋ง์ ์ฌํ ๊ณํ ๊ธฐ๋ฅ์์ ๋ฐฉ๋ฌธํ๊ณ ์ ํ๋ ๊ด๊ด์ง๋ค์ ๋ํ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํด์ค
- ๊ธฐ์กด ๋ฐฉ๋ฌธํ ์ง์ ์ ๋ํ ์ด ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํด์ฃผ๋ ํจ์ ์ญ์
- stores ๋ฐฐ์ด์ ์ ์ญ ๋ณ์๋ก ์ค์
- ์ ์ ๊ฐ ๊ด๊ด์ง๋ฅผ ์ ํํ ๋๋ง๋ค stores์ ์ถ๊ฐํด์ฃผ๊ณ stores์ ์ด๋ฏธ ํฌํจ๋์ด ์๋ ๊ด๊ด์ง๋ฉด ๋ฐฐ์ด์์ ์ญ์
- ์ ์ ๊ฐ ๊ด๊ด์ง๋ฅผ ์ ํํ ๋๋ง๋ค findShortestPath()๋ฅผ ํตํด ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ์ํจ --- findShortestPath()๋ ๋ฉ๋ชจ์ด์ ์ด์ ์ ํตํด ๊ณผ๋ถํ๋ฅผ ๋ฐฉ์งํ๊ณ ์ dp๋ก ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํ์ฌ ๊ตฌํํ์์
- ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ์ํ ๋ ์๋, ๊ฒฝ๋ ๊ธฐ์ค์ผ๋ก ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ธฐ ์ํด calculateDistance() ํจ์๋ฅผ ์ฌ์ฉํจ
- findShortestPath()๋ ์ต๋จ๊ฒฝ๋ก ๊ด๊ด์ง์ ์ด์ ๊ด๊ด์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ด๊ธด ๋ฐฐ์ด์ ๋ฐํํจ
- ๋ฐํ๋ ๋ฐ์ดํฐ ๊ฐ์ ์ง๋ ์ผ์ชฝ ์๋จ์ ์์นํ clickedPlacesList๋ด๋ถ ๋ฆฌ์คํธ ํญ๋ชฉ์ผ๋ก ์ถ๊ฐํจ
- ์ด์ ๊ด๊ด์ง์ ๊ฑฐ๋ฆฌ๊ฐ ๋ด๊ธด ๋ฐฐ์ด์ ํ์ฉํ์ฌ ์ด ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ณ clickedPlacesList ul ๋ด๋ถ์ ๊ฐ์ ์ถ๊ฐํจ
- ์ ์ ๊ฐ ๊ด๊ด์ง๋ฅผ ์ ํํ ๋๋ง๋ค 3~8๋ฒ์ ๋ฐ๋ณต