본문 바로가기

Learning/SQL

JDBC Test

 

  • 자바 웹 애플리케이션 (이클립스)과 오라클을 연결해주는 JDBC (Java DataBase Connectivity) 필요

  • 오라클 JDBC 드라이버를 다운로드 후 C:\Program Files\Java\jre1.8.0.251\jre\lib\ext에 옮겨놓음

  • 이클립스에서 확인

 

 

 

 

  • JDBC 테스트

1. 드라이버 로딩

Class.forName("oracle.jdbc.driver.OracleDriver");

 

2. DB 연결

Connection conn=null;

conn=DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

 

3. SQL문 준비

stmt=conn.createStatement();

 

4. SQL쿼리 실행하고 결과 받기

stmt.executeUpdate();

ResultSet rs=null;

rs=stmt.executeQuery(query);

 

 

 


 

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.oracle.test;
 
import java.sql.*//자바 sql 라이브러리 
 
public class GetEmp {
 
    public static void main(String[] args) {
        //(oracle) jdbc:oracle:thin:@localhost:1521:xe
        //(mySQL) jdbc:mysql://localhost:3306/db이름
//        미리 입력할 오라클 주소, 계정, 비밀번호를 변수로 저장
        String DB_URL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        String DB_USER = "scott";
        String DB_PASSWORD = "1234";
//      conn은 오라클 연결을 위한 객체
        Connection conn = null;
//      stmt는 sql문을 실행하기 위한 객체
        Statement stmt = null;
//      rs는 sql문 실행 후 결과값을 받아오는 객체
        ResultSet rs = null;
 
        String query = "SELECT * FROM emp";
        String query2 = "SELECT * FROM emp WHERE EMPNO=7369";
        
        try {
            //1. 오라클 드라이버 로딩   ( ojdbc6_g.jar )            
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e ) { //클래스를 못찾았을때 (드라이버 없을때 )
            e.printStackTrace();
        }
 
        try { //DB 연결 부분은 try-catch 문으로 예외 발생시 처리
            //2. DB 연결
            conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            //3. SQL 문 준비
            stmt = conn.createStatement();
            //4. SQL 쿼리 실행하고 결과 받기
            rs = stmt.executeQuery(query2);
 
            while (rs.next()) { // 테이블의 한 행씩 실행
                String empno = rs.getString("EMPNO");  // 첫번째 열
                String ename = rs.getString(2);
                String job = rs.getString(3);
                String mgr = rs.getString(4);
                String hiredate = rs.getString(5);
                String sal = rs.getString(6);
                String comm = rs.getString(7);
                String depno = rs.getString(8);
 
                System.out.println(empno + " : " + ename + " : " + job + " : " + mgr + " : " + hiredate + " : " + sal + " : " + comm + " : " + depno); 
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();         //ResultSet (쿼리 결과) 닫기
                stmt.close();        //State문 닫기
                conn.close();
            } catch (SQLException e) {}
        }
    }
 
}
 
cs

 

 

 

'Learning > SQL' 카테고리의 다른 글

JDBC SELECT  (0) 2020.06.23
JDBC INSERT  (0) 2020.06.23
DCL: 데이터 제어어  (0) 2020.06.23
시퀀스  (0) 2020.06.23
인덱스  (0) 2020.06.22