본문 바로가기

프로그래밍/JSP

JDBC - JDBC 프로그래밍 예제(1)

JDBC를 이용해서 EMP테이블의 데이터를 조작하는 예제

- select , insert , update , delete 사용


EMP테이블

JDBC 소스 코드

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class JDBCOracleExample {
    public static void main(String args[]) {
        Connection conn = null;
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
        String id = "student";
        String pw = "1234";
        try {
            //1. 드라이버 로드
            Class.forName("oracle.jdbc.driver.OracleDriver");
            
            //2. DB연결
            conn = DriverManager.getConnection(jdbcUrl, id, pw);
            System.out.println("데이터베이스에 접속했습니다");
            
            //입력 - insert 쿼리 처리
            String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
            
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,7777);
            pstmt.setString(2,"YOON");
            pstmt.setString(3,"SALESMAN");
            pstmt.setInt(4,7839);
            pstmt.setString(5,"95/10/24");
            pstmt.setInt(6,3200);
            pstmt.setInt(7,500);
            pstmt.setInt(8,40);
            
            int resultCnt = pstmt.executeUpdate();
            System.out.println("INSERT 쿼리가 실행되었습니다 (데이터입력)");
            System.out.println(resultCnt + " 개의 행이 입력되었습니다.");
            System.out.println("---------------------------------------");
            
            
            // 3. Statement 생성 : sql 문 데이터베이스로 실행 요청하기위해
            Statement stmt = conn.createStatement();
            
            //4. 요청할 SQL문
            //Select 쿼리
            String sql1 = "select * from emp order by empno";
            // Select의 결과를 ResultSet 객체에 담는다.
            ResultSet rs = stmt.executeQuery(sql1);
            
            //rs결과를 행단위로
            System.out.println("[부서리스트]");
            while(rs.next()) {
                System.out.println("--------------------");
                System.out.println("사원번호 : "+ rs.getInt("empno"));
                System.out.println("사원이름 : "+ rs.getString("ename"));
                System.out.println("담당업무 : "+ rs.getString("job"));
                System.out.println("상관사번 : "+ rs.getInt(4));
                System.out.println("입사일 : "+ rs.getString(5));
                System.out.println("급여 : "+ rs.getString(6));
                System.out.println("커미션 : "+ rs.getString(7));
                System.out.println("부서번호 : "+ rs.getString(8));
            }
            System.out.println("--------------------");
            
            //update쿼리
            String sql2 = "update emp set ename='JEONG' where ename='YOON'";
            pstmt = conn.prepareStatement(sql2);
            pstmt.executeUpdate();
            System.out.println("UPDATE 쿼리가 실행되었습니다 (데이터수정)");
            
            //delete쿼리 
            String sql3 = "DELETE FROM EMP WHERE EMPNO=7777";
            pstmt = conn.prepareStatement(sql3);
            pstmt.executeUpdate();
            System.out.println("Delete 쿼리가 실행되었습니다 (데이터삭제)");
            
            
            //4. 데이터베이스 연결 종료
            conn.close();
            stmt.close();
            pstmt.close();
            rs.close();
            
        }catch (ClassNotFoundException cnfe) {
            // TODO: handle exception
            System.out.println("데이터베이스 드라이버를 찾을 수 없습니다.");
        }catch (SQLException se) {
            System.out.println(se.getMessage());
            // TODO: handle exception
        }
    }
}
cs


'프로그래밍 > JSP' 카테고리의 다른 글

JDBC - JDBC 프로그래밍 예제(2)  (0) 2018.07.21
JDBC - JDBC란?  (0) 2018.07.21
필터 - 캐릭터 인코딩  (0) 2018.07.21
파일 업로드  (0) 2018.07.20
MVC 패턴 구현  (0) 2018.07.19