Programming/JAVA / / 2019. 11. 17. 18:39

java jdbc oracle select 사용방법

반응형

1. JDBC 드라이버를 로딩

  • Class.forName(“orale.jdbc.driver.OracleDriver”)

2. Connection 객체를 생성

  • conn = DriverManager.getConnection(url, id, pw)

3. PreparedStatement 객체 생성, 객체 생성시 SQL 저장

  • PreparedStaement - SQL문을 데이터베이스에 보내기위한 객체입니다.
  • pstmt = conn.preparedStatement(sql)

4. SQL 문장을 실행 후 결과를 리턴

  • SQL 문장 실행 후, 변경된 row 수를 int type 으로 리턴합니다.
  • pstmt.executeQuery()

5. close 

  • ResultSet close
  • PreparedStatement close
  • Connection close

 

 

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
package oracle;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class OracleTest {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String url ="jdbc:oracle:thin:@localhost:1521:orcl";
        String id = "aaaa4444";
        String pw = "aaaa4444";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, id, pw);
            String sql = "SELECT * FROM  wex001m";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            System.out.println("success");
            while(rs.next()) {
                System.out.println(rs.getString(1)+rs.getString(2)+rs.getString(3)+rs.getString(4)+rs.getString(5));
 
 
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null) {rs.close();}
            }catch(Exception e) {
                e.printStackTrace();
            }
            try {
                if(pstmt!=null) {pstmt.close();}            
            }catch(Exception e) {
                e.printStackTrace();
            }
            try {
                if(conn!=null) {conn.close();}
                
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}
 
cs
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유