2016년 7월 16일 토요일

java SQLite-jdbc 연결하기 // jar파일 연결하기

1. sqlite-jdbc-3.8.10.1 다운
- 다운 받으면 jar파일이다.
- 이 jar파일을 이클립스를 이용해 연결한다.
- 나중에 백업할 때나 프로젝트를 이동할때 jar파일이 누락되는 것을 방지하고자, jar파일을 프로젝트안에 ext라는 폴더안에 넣어두자.

2. jar 연결(이클립스)
 - 프로젝트 우클릭하여 properties 선택
 - java build path -> libraries -> add external jars 선택
 - 추가하고픈 jar파일 선택
 - ok 선택
 - 끝.

3. sample.java 을 이용해서 연습해보자.

- 생성한 db파일은 파이어폭스 확장 프로그램인 sqlite로 보기좋게 열수 있다.

------------------------------------------------------------------------------------------------------
code
------------------------------------------------------------------------------------------------------

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document; //웹표준, Documnet
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;



public class MainEx2 {

public static void main(String[] args) {
//  네이버 오픈 api로 xml 연결하는걸 연습하자
String strUrl =  "http://openapi.naver.com/search?key=c1b406b32dbbbbeee5f2a36ddc14067f&query=%EC%A3%BC%EC%8B%9D&target=news&start=1&display=10";
//  InputStream 나중에 닫아야돼 
InputStream is =null;
try {
URL url = new URL(strUrl);
// URLConnection 이용하여 웹페이지와 연결
URLConnection conn =  url.openConnection();
is = conn.getInputStream();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // try~catch
//  InputStream 으로 값을 XML 기능으로 파싱해오는 과정
//         싱글톤
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = null ;
if(is != null) doc = db.parse(is);
if(doc != null) {
// item 노드들을 불러오기
NodeList list = doc.getElementsByTagName("item");
System.out.println("itemCount : " + list.getLength());
// item노드 첫번째 가져오기
Node node = list.item(0);
// 첫번째 item 노드내의 노드들을 가져오기
NodeList itemLists = node.getChildNodes();
System.out.println("itemListsCount : " + itemLists.getLength());
// 첫번째 item노드의 첫번째 노드를 불러오기(타이틀)
Node titleNode =  itemLists.item(0);
System.out.println("Title : " + titleNode.getTextContent());
} // if
} catch (ParserConfigurationException e) {
e.printStackTrace();
}  catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // try~catch
if(is != null){
    try { is.close();
} catch (IOException e) { e.printStackTrace();
} // try~catch
}  // if

} //main

} // class

댓글 없음:

댓글 쓰기