Binary World

JAVA CLASS : 자바에 오라클 DB 연동하기(데이터 삽입) 본문

개발자의 길/JAVA

JAVA CLASS : 자바에 오라클 DB 연동하기(데이터 삽입)

모쿠 2017. 1. 23. 10:42

<자바와 오라클 연동>


JDBC: Java Database Connection

1. 데이터베이스 라이브러리를 프로젝트에 추가

2. DB와 연동하기 위해서 필요한 상수들의 정의(세팅)

3. JDBC 드라이버를 로드

4. DB와 Connection(연결)을 맺음

5. Statement 객체를 생성

6. SQL 문장을 작성

7. Statement 객체를 사용해서 SQL 문장을 실행(DB 서버로 SQL 문장을 전송)

8. DB 서버가 보내준 결과를 확인/처리


<DB에 테이블 작성 및 데이터 삽입>


* study.sql


create table ex_contact (

  cid number primary key,

  name varchar2(20) , 

  phone varchar2(20),

  email varchar2(100)

);


create sequence contact_pk;


insert into ex_contact 

values (contact_pk.nextval, '목혁', '010-0000-0000', 'text@gmail.com');

-- contact_pk.nextval : 1부터 순서대로 값을 인서트 시켜줌


insert into ex_contact

values (contact_pk.nextval, '목진', '010-333-2222', 'test@naver.com');


insert into ex_contact

values (contact_pk.nextval, '목진', '010-222-2222', 'test@naver.com');


update ex_contact

set name = '모쿠', phone ='23231', email = 'ttt@naver.com'

where cid = 2;


delete from ex_contact

where cid =3;


select * from ex_contact order by cid;


commit; 



<데이터베이스 라이브러리를 프로젝트에 추가하기>


1. ojdbc5.jar 파일을 복사한다.


2. 연결하고자 하는 자바 프로젝트에 폴더를 생성(libs)하고 파일은 붙여넣기 한다.


3. 복사한 ojdbc5.jar 파일에서 오른쪽 클릭 후, Build Path - Add to Build Path를 클릭하여 라이브러리를 빌드한다.


4. 생성된 레퍼런스 라이브러리를 확인한다.



<자바에서 DB 연결 및 테이블에 변수 삽입하기>


*JDBCMain01.java


package edu.java.contacttext;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;


import oracle.jdbc.driver.OracleDriver;


public class JDBCMain01 {

// 1. DB에 사용될 상수들

public static final String URL = "jdbc:oracle:thin:@localhost:1521:xe"; 

// thin : 메모리에 올리는 드라이버 이름

// 오라클에 접속 정보를 작성

public static final String USER = "scott"; // 오라클 계정 이름

public static final String PASSWD = "tiger"; // 오라클 계정 비밀번호

public static final String TABLE_NAME = "ex_contact"; // 테이블 이름

public static final String COL_CID = "cid"; // 첫번째 column 

public static final String COL_NAME = "name"; // 두번째 column

public static final String COL_PHONE = "phone"; // 세번째 column

  public static final String COL_EMAIL = "email"; // 네번째 column

public static void main(String[] args) {

System.out.println("JDBC 1");

Connection conn = null;

Statement stmt = null;

try {

// 2. Oracle JDBC 드라이버를 메모리에 로드

DriverManager.registerDriver(new OracleDriver());

System.out.println("드라이버 로드 성공");

// 3. DB와 Connection(연결)을 맺음

conn = DriverManager.getConnection(URL, USER, PASSWD);

System.out.println("DB 연결 성공");

// 4. Connection 객체를 사용해서 Statement 객체를 생성

stmt = conn.createStatement();

// 5. SQL 문장 작성

String sql_insert = "insert into " + TABLE_NAME

+ " values (contact_pk.nextval, '목진혁', 'ㅎ', 'ㅎ')";

System.out.println(sql_insert);

// 6. SQL 문장 실행

int result = stmt.executeUpdate(sql_insert);

// 7. 서버가 보낸 결과 처리

System.out.println(result + "행이 삽입됐습니다.");

} catch (SQLException e) {

e.printStackTrace();

} finally {

// 생성한 리소스 해제

try {

stmt.close(); // Statement 객체 해제

conn.close(); // Connection 객체 해제

} catch (Exception e) {

e.printStackTrace();

}


}


} // end JDBCMain01


} // end class JDBCMain01 




Comments