//
you're reading...
JDBC

JDBC Code with Oracle – Transaction

import java.sql.*;

public class TransactionTest02 {
	
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try { 
			//new OracleDriver();
			Class.forName("oracle.jdbc.driver.OracleDriver");		
			
			String dbUrl = "jdbc:oracle:thin:@192.168.56.30:1521:TESTDB";
			String username = "testdbuser";
			String password = "testdbpwd";
			conn = DriverManager.getConnection(dbUrl, username, password);
			
			conn.setAutoCommit(false);
			pstmt = conn.prepareStatement("insert into emp(empno, ename) values(?, ?)");
			pstmt.setInt(1, 9669);
			pstmt.setString(2, "??");
			pstmt.executeUpdate();
			
			pstmt = conn.prepareStatement("insert into dept(deptno, dname) values(?, ?)");
			pstmt.setInt(1, 70);
			pstmt.setString(2, "???");
			pstmt.executeUpdate();
			
			Integer.parseInt("asdfasfs");						
			
			conn.commit();						
			System.out.println("????!");
		}catch(Exception e) {
			e.printStackTrace();
			try {
				if (conn != null) {
					conn.rollback();	
				}
			}catch(SQLException sqle)	{}
		}finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstmt != null) {
					pstmt.close();
				}
				if (conn != null) {
					conn.close();	
				}
			}catch(SQLException e){}	
		}
	}
}

Advertisements

About daviewning

I am an Oracle DBA

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: