Saturday, 21 January 2017

TRANSACTION MANAGEMENT

What is Transaction

One or more than one command executed within session is called Transaction. In a transaction all the commands are executed on database or none of the command executed.

Note : Change the autoCommit as false

1. void setAutoCommit(boolean autoCommit) - Sets this connection's auto-commit mode to the given state.
2. boolean getAutoCommit() - Retrieves the current auto-commit mode for this connection object.


PROGRAM TO FIND AUTOCOMMIT MODE OF ORACLE DRIVER

package Package1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test26 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
boolean b = con.getAutoCommit();
System.out.println(b);
if(b==true)
con.setAutoCommit(false);
Statement st = con.createStatement();
int c=st.executeUpdate("insert into dept values(5,'xyz','bbb')");
System.out.println(c);
ResultSet rs = st.executeQuery("select * from dept");
rs.next();
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
}

Output -
true
1
69 kkk ddd


No comments:

Post a Comment