티스토리 뷰
성공한 줄 알고 신나서 올렸는데 실패였다......
같은 값이 13개 700몇개씩 올라갔을 줄 누가 알았을까...
제발 성공하고 자자
선택한 폴더들이 필요하다.
create table 문 >>
- addr1 과 homepage 에서 data too long 에러가 나는 바람에 전부 아주 확 늘려주었다
CREATE TABLE go_api (
facltNm varchar(100),
addr1 varchar(100),
addr2 varchar(100),
induty varchar(100),
firstImageUrl varchar(200),
contentId varchar(100),
intro TEXT,
animalCmgCl varchar(100),
trlerAcmpnyAt varchar(100),
caravAcmpnyAt varchar(100),
lctCl varchar(100),
brazierCl varchar(100),
siteBottomCl1 varchar(50),
siteBottomCl2 varchar(50),
siteBottomCl3 varchar(50),
siteBottomCl4 varchar(50),
siteBottomCl5 varchar(50),
sbrsCl varchar(100),
doNm varchar(50),
eqpmnLendCl varchar(100),
homepage varchar(200),
mapX varchar(100),
mapY varchar(100)
);
searchKeyTO >>
package com.exam.search;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SearchkeyTO {
private String keysearch; // 검색키워드
private String facltNm; // 1. 캠핑장명
private String addr1; // 2. 캠핑장주소 = addr1 + addr2
private String addr2; // 3.
private String induty; // 4. 캠핑장유형(주요시설)
private String firstImageUrl; // 5. 대표이미지
private String contentId; // 6. 캠핑장아이디
private String intro; // 7. 캠핑장소개
private String animalCmgCl; // 8. 애완동물동반(불가능/가능/가능(소형견))
private String trlerAcmpnyAt; // 9. 개인트레일러입장
private String caravAcmpnyAt; // 10. 개인카라반입장
private String lctCl; // 11. 입지
private String brazierCl; // 12. 화로대 사용가능여부
// 바닥형태
private String siteBottomCl1; // 13. 잔디
private String siteBottomCl2; // 14. 파쇄석
private String siteBottomCl3; // 15. 테크
private String siteBottomCl4; // 16. 자갈
private String siteBottomCl5; // 17. 맨흙
private String sbrsCl; // 18. 부대시설(캠핑장 시설정보)
private String doNm; // 19. 시,도
private String eqpmnLendCl; // 20. 캠핑장비대여
private String homepage; // 21. 캠핑장 사이트주소
private String mapX; // 22. 경도(X)
private String mapY; // 23. 위도(Y)
}
insertGocamping >>
package com.exam.search;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
@Repository
public class InsertGocamping {
@Autowired
private JdbcTemplate jdbcTemplate;
private static String getValue(String tag, Element element) {
if(element.getElementsByTagName(tag).item(0)== null) {
return " ";
}
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node =(Node) nodes.item(0);
return node.getNodeValue();
}
public ArrayList<SearchkeyTO> gocampingparse(String sido) {
ArrayList<SearchkeyTO> lists = new ArrayList<SearchkeyTO>();
try {
DocumentBuilderFactory dbFactoty = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactoty.newDocumentBuilder();
Document document = dBuilder.parse( new URL("https://api.visitkorea.or.kr/openapi/service/rest/GoCamping/basedList?ServiceKey=74rd5r1MwtY%2F3dFG0s9I1UDtFBJBj1zjmm0VdZBNsJoslwPnviVh2PeV1vCg%2BtaHuMvN8G1f1PWIwKh3I%2BI0oQ%3D%3D&numOfRows=3107&pageNo=1&MobileOS=ETC&MobileApp=TestApp").openStream());
Element root = document.getDocumentElement();
System.out.println( root.toString() ); // [response: null] 출력됨
NodeList nList = document.getElementsByTagName("item");
SearchkeyTO kto = new SearchkeyTO();
//GocampingTO gto = new GocampingTO();
//ArrayList<SearchkeyTO> lists = gto.getCampLists();
for(int i = 0; i < nList.getLength(); i++) {
//System.out.println(nList.item(i).getAttributes().getNamedItem("facltNm").getNodeValue());
Node node = nList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if(( getValue("doNm", element) != " " )){
if( (getValue("doNm", element).toString().indexOf( sido )) != -1 ) {
//1
try {
System.out.println( "캠핑장명: " + getValue("facltNm", element) );
kto.setFacltNm( getValue("facltNm", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장명: 없음" );
kto.setFacltNm( "default" );
}
//2
try {
System.out.println( "캠핑장 주소1: " + getValue("addr1", element) );
kto.setAddr1( getValue("addr1", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장주소1: 없음" );
kto.setAddr1( "default" );
}
//3
try {
System.out.println( "캠핑장 주소2: " + getValue("addr2", element) );
kto.setAddr1( getValue("addr2", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장주소2: 없음" );
kto.setAddr2( "default" );
}
//4
try {
System.out.println( "캠핑장 유형: " + getValue("induty", element) );
kto.setInduty( getValue("induty", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장 유형: 없음" );
kto.setInduty( "default" );
}
//5
try {
System.out.println( "대표이미지: " + getValue("firstImageUrl", element) );
kto.setFirstImageUrl( getValue("firstImageUrl", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "대표이미지: 없음" );
kto.setFirstImageUrl( "default" );
}
//6
try {
System.out.println( "캠핑장 아이디: " + getValue("contentId", element) );
kto.setContentId( getValue("contentId", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장 아이디: 없음" );
kto.setContentId( "default" );
}
//7
try {
System.out.println( "캠핑장 소개: " + getValue("intro", element) );
kto.setIntro( getValue("intro", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장 소개: 없음" );
kto.setIntro( "default" );
}
//8
try {
System.out.println( "애완동물동반: " + getValue("animalCmgCl", element) );
kto.setAnimalCmgCl( getValue("animalCmgCl", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "애완동물동반: 없음" );
kto.setAnimalCmgCl( "default" );
}
//9
try {
System.out.println( "개인트레일러입장: " + getValue("trlerAcmpnyAt", element) );
kto.setTrlerAcmpnyAt( getValue("trlerAcmpnyAt", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "개인트레일러입장: 없음" );
kto.setTrlerAcmpnyAt( "default" );
}
//10
try {
System.out.println( "개인카라반입장: " + getValue("caravAcmpnyAt", element) );
kto.setCaravAcmpnyAt( getValue("caravAcmpnyAt", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "개인카라반입장: 없음" );
kto.setCaravAcmpnyAt( "default" );
}
//11
try {
System.out.println( "입지: " + getValue("lctCl", element) );
kto.setLctCl( getValue("lctCl", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "입지: 없음" );
kto.setLctCl( "default" );
}
//12
try {
System.out.println( "화로대 사용가능여부: " + getValue("brazierCl", element) );
kto.setBrazierCl( getValue("brazierCl", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "화로대 사용가능여부: 없음" );
kto.setBrazierCl( "default" );
}
//13
try {
System.out.println( "바닥형태-잔디: " + getValue("siteBottomCl1", element) );
kto.setSiteBottomCl1( getValue("siteBottomCl1", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "바닥형태-잔디: 없음" );
kto.setSiteBottomCl1( "default" );
}
//14
try {
System.out.println( "바닥형태-파쇄석: " + getValue("siteBottomCl2", element) );
kto.setSiteBottomCl2( getValue("siteBottomCl2", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "바닥형태-파쇄석: 없음" );
kto.setSiteBottomCl2( "default" );
}
//15
try {
System.out.println( "바닥형태-테크: " + getValue("siteBottomCl3", element) );
kto.setSiteBottomCl3( getValue("siteBottomCl3", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "바닥형태-테크: 없음" );
kto.setSiteBottomCl3( "default" );
}
//16
try {
System.out.println( "바닥형태-자갈: " + getValue("siteBottomCl4", element) );
kto.setSiteBottomCl4( getValue("siteBottomCl4", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "바닥형태-자갈: 없음" );
kto.setSiteBottomCl4( "default" );
}
//17
try {
System.out.println( "바닥형태-맨흙: " + getValue("siteBottomCl5", element) );
kto.setSiteBottomCl5( getValue("siteBottomCl5", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "바닥형태-맨흙: 없음" );
kto.setSiteBottomCl5( "default" );
}
//18
try {
System.out.println( "캠핑장 시설정보: " + getValue("sbrsCl", element) );
kto.setSbrsCl( getValue("sbrsCl", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장 시설정보: 없음" );
kto.setSbrsCl( "default" );
}
//19
try {
System.out.println( "시/도: " + getValue("doNm", element) );
kto.setDoNm( getValue("doNm", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "시/도: 없음" );
kto.setDoNm( "default" );
}
//20
try {
System.out.println( "캠핑장비대여: " + getValue("eqpmnLendCl", element) );
kto.setEqpmnLendCl( getValue("eqpmnLendCl", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장비대여: 없음" );
kto.setEqpmnLendCl( "default" );
}
//21
try {
System.out.println( "캠핑장 사이트주소: " + getValue("homepage", element) );
kto.setHomepage( getValue("homepage", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장 사이트주소: 없음" );
kto.setHomepage( "default" );
}
//22
try {
System.out.println( "경도(X): " + getValue("mapX", element) );
kto.setMapX( getValue("mapX", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "경도(X): 없음" );
kto.setMapX( "default" );
}
//23
try {
System.out.println( "위도(Y): " + getValue("mapY", element) );
kto.setMapY( getValue("mapY", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "위도(Y): 없음" );
kto.setMapY( "default" );
}
lists.add(kto);
}
}
}
};
System.out.println("끝");
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lists;
}
public int insertapi(ArrayList<SearchkeyTO> lists) {
int flag = 1;
for( SearchkeyTO kto : lists ) {
String sql = "insert into go_api values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
int result = jdbcTemplate.update(sql, kto.getFacltNm(), kto.getAddr1(), kto.getAddr2(), kto.getInduty(), kto.getFirstImageUrl(), kto.getContentId(), kto.getIntro(), kto.getAnimalCmgCl(), kto.getTrlerAcmpnyAt(), kto.getCaravAcmpnyAt(), kto.getLctCl(), kto.getBrazierCl(), kto.getSiteBottomCl1(), kto.getSiteBottomCl2(), kto.getSiteBottomCl3(), kto.getSiteBottomCl4(), kto.getSiteBottomCl5(), kto.getSbrsCl(), kto.getDoNm(), kto.getEqpmnLendCl(), kto.getHomepage(), kto.getMapX(), kto.getMapY() );
System.out.println( result );
if( result != 1 ) {
System.out.println( "insertapi 오류" );
flag = 1;
} else {
flag=0;
}
}
return flag;
}
}
if( (getValue("doNm", element).toString().indexOf( sido )) != -1 ) {
try {
System.out.println( "캠핑장명: " + getValue("facltNm", element) );
kto.setFacltNm( getValue("facltNm", element) );
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println( "캠핑장명: 없음" );
kto.setFacltNm( "default" );
}
facltNm 이라는 코드가 없어서 값이 null이 되면 오류가 나서 스트레스였는데,
이렇게 간단하게 try-catch로 exception처리를 하면 되는 거였다 흑..
아무튼 값이 없을때는 default로 넣어준다.
controller >>
insert_ok.jsp >>
1. controller를 먼저 실행, http://localhost:8080/insertgocamping.do
2. controller 코드처럼 String sido 부분에 "서울시"가 들어가져서 doNm이 서울시인 데이터를 갖고와서 DB에 넣어준다.
3. flag 로 정상insert 되는지 확인할 수 있다.
'FINAL PROJECT' 카테고리의 다른 글
[성공]DB에 "INSERT 고캠핑API" 하기 (0) | 2022.08.11 |
---|---|
[API -> DB] insert 시행착오겪는 중... (0) | 2022.08.11 |
[html/css]툴팁 사이즈 조정 (0) | 2022.08.11 |
KEYWORD 검색 (0) | 2022.08.10 |
[성공]고캠핑 api - xml 파싱 (0) | 2022.08.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Board
- reactjs
- 자바
- 연습문제
- Java
- create-react-app
- Doit
- 입문
- 레파지토리복사
- 깃업그레이드
- fork
- Vue.js
- 리액트설치
- 피라미드별
- gjt
- 토큰발행
- vue-cli
- 삼각형별
- 별찍기
- component
- Vue
- 별출력
- jqueryui
- 구구단
- Model2
- 리액트
- 토큰
- jquery
- 자바프로그래밍
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함