You are on page 1of 4

Transaction Management:

Logical database

JDBC Program session

Physical
Database

session

JDBC Program
Methods of Connection:

 Boolean getAutoCommit()
Retrieves the current auto-commit mode for this Connection object.
 Void setAutoCommit(Boolean autoCommit)
Sets this connection’s auto-commit mode to the given state.
 Void commit()
Makes all changes made since the previous commit/rollback permanent and
releases any database locks currently held by this Connection object.
 Void rollback()
Undoes all changes made in the current transaction and releases any database locks
currently held by this connection object.
 Savepoint setSavepoint()
Creates an unnamed savepoint in the current transaction and returns the new
Savepoint object that represents it.
 Savepoint setSavepoint(String name)
Creates a savepoint with the given name in the current transaction and returns the
new Savepoint object that represents it
 Void rollback (Savepoint savepoint)
Undoes all changes made after the given Savepoint object was set

//program to change the status of autocommit to false

package com;

import java.sql.Connection;
import java.sql.DriverManager;

public class AutocommitEg {

public static void main(String[] args) throws Exception{


Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
boolean b = con.getAutoCommit();
System.out.println(b);
if(b==true)
con.setAutoCommit(false);

b = con.getAutoCommit();
System.out.println(b);
}
}

 Before closing connection or session, the changes done within session is


committed within database.

package com;
import java.sql.*;

public class autoCommitEg2 {

public static void main (String[] args) throws Exception {


Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr");
con.setAutoCommit(false);
Statement st = con.createStatement();
int c = st.executeUpdate("update employees set salary=salary+100
where first_name='John'");
System.out.println(c);
con.commit();
}

RowSets:
A RowSet is an object that encapsulates a set of rows from either Java Database Connectivity (JDBC)
result sets or tabular data sources like a file or spreadsheet. RowSets support component-based
development models like JavaBeans, with a standard set of properties and an event notification
mechanism.

Q: What are the different types of RowSet?

A: There are two types of RowSet. They are:

Connected: - A connected RowSet object connects to the database once and remains
connected until the application terminates.

Disconnected: - A disconnected RowSet object connects to the database, executes a query


to retrieve the data from the database and then closes the connection. A program may change the
data in a disconnected RowSet while it is disconnected. Modified data can be updated in the
database after a disconnected RowSet re-establishes the connection.

Javax.sql.RowSet.package:
1. JdbcRowSet - connected rowset
2. CacheRowSet  disconnected rowset
3. WebRowSet  disconnected rowset

1. JdbcRowSet
A JdbcRowSet is a connected RowSet, that is, it continually maintains its connection to a
database using a JDBC technology-enabled driver, it also effectively makes the driver a
JavaBeans component.
JdbcRowSet is an interface, which is implemented by jdbc driver developer.
How to create JdbcRowSet object?
Eg:
package com;
import java.sql.*;
import javax.sql.rowset.*;
import oracle.jdbc.rowset.*;

public class JdbcRowSetEg {

public static void main(String[] args) throws Exception {

JdbcRowSet j = new OracleJDBCRowSet();


j.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
j.setUsername("hr");
j.setPassword("hr");
j.execute();
j.setCommand("select employee_id,first_name,salary from employees");
while(j.next())

System.out.println(j.getInt(1)+","+j.getString(2)+","+j.getFloat(3));
}

CachedRowSet:
A CachedRowSet object is a container for rows of data that caches its rows in memory,
which makes it possible to operate without always being connected to its data
source/database.
It is JavaBeans component and is scrollable, updatable, and serializable.

You might also like