본문 바로가기
2019/JDBC

JDBC Select

by SOLYI 2019. 11. 26.
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
package orcale_jdbc;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class selectTest {
 
    public static void main(String[] args) {
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String user = "scott";
        String pwd = "tiger";
        ResultSet rs = null;
        StringBuilder sql = new StringBuilder();
        sql.append(" select no, name, age, addr     ");
        sql.append(" from member1                     ");
        
        try {
            Class C = Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection(url, user, pwd);
            pstmt = conn.prepareStatement(sql.toString());
            
            rs = pstmt.executeQuery();
            while(rs.next()) {
                System.out.print(rs.getInt("no"));
                System.out.print(rs.getString("name"));
                System.out.print(rs.getInt("age")); 
                System.out.print(rs.getString("addr"));
                System.out.println();
            }
        }catch(Exception e) {
            System.out.println(e);
        }finally{
            if(rs!=null)     try { rs.close(); }     catch(Exception e) {}
            if(pstmt!=nulltry { pstmt.close(); }     catch(Exception e) {}
            if(conn!=null)     try { conn.close(); }     catch(Exception e) {}
            
        }    
    }
}
 
cs
반응형

'2019 > JDBC' 카테고리의 다른 글

JDBC delete  (0) 2019.11.26
JDBC update  (0) 2019.11.26
JDBC insert  (0) 2019.11.26