카테고리 없음 / / 2019. 11. 22. 19:00

java jdbc oracle commit() rollback() 사용방법

반응형

1. JDBC 드라이버를 로딩

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

2. Connection 객체를 생성

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

3. 자동 commit 해제

  • conn.setAutoCommit(false);

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

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

5. pstmt.set<데이터타입>(? 순서, 값)

  • ? 매개변수에 값을 지정합니다.
  • ex) pstmt.setString(1, 값), pstmt.setInt(2, 값)

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

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

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

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

8. 트랜젝션 발생

  • pstmt.setString(6,"없는컬럼");

9. commit 해주기

  • commit();

10. rollback

if(conn!=null) 
try{
conn.rollback();
System.out.println("rollback");
}catch(SQLException sqle) {}

 

--------------------------------------------------------------------------------

 

commit() , rollback() 한쌍으로 트랜젝션 발생시 다시 되돌림

pstmt.setString(1,"홍길동");

pstmt.setString(2"16");

pstmt.setString(3"서울시 서초동");

pstmt.setString(4"010-1234-5678");

pstmt.setString(5"abc@ddd.com");

해당 값이 insert 안됨

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
package oracle;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
public class OracleInsertTest {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = 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);
            conn.setAutoCommit(false);
            String sql = "INSERT INTO wex001m VALUES (?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,"홍길동");
            pstmt.setString(2"16");
            pstmt.setString(3"서울시 서초동");
            pstmt.setString(4"010-1234-5678");
            pstmt.setString(5"abc@ddd.com");
            pstmt.executeUpdate();
            pstmt.setString(1,"이순신");
            pstmt.setString(2"40");
            pstmt.setString(3"서울시 강남구");
            pstmt.setString(4"010-5678-1234");
            pstmt.setString(5"bac@ddb.com");
            pstmt.setString(6"없는컬럼");
            pstmt.executeUpdate();
            conn.commit();
            System.out.println("success");
        }catch(Exception e) {
            e.printStackTrace();
            if(conn!=null
            try{
                conn.rollback();
                System.out.println("rollback");
            }catch(SQLException sqle) {}
        }finally {
            try {
                if(pstmt!=null) {pstmt.close();}            
            }catch(Exception e) {
                e.printStackTrace();
            }
            try {
                if(conn!=null) {conn.close();}
                
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}
 
cs

 

 

 

반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유