JDBC
The Java
Databesr Connectiity (JDBC) API enable java application to interact with a
database.
Types of drivers :
1. Type 1: Drivers of this type are generally dependent
on a native library, hence they have less portability. Ex. JDBC-ODBC bridge.
2. Type 2: Coded partly in
java and partly in native. These drivers use a native library specific to the
data source to which they connect. Ex. Oracle’s call interface (OCI) client
side driver.
3. Type 3: This type of
driver uses pure java client and communicate with a middleware server using a
db independent protocol, and middleware server then communicates the client’s
request to data source.
4. Type 4: Drivers that are
pure Java and implement the network protocol for a specific data source. The
client connects directly to the data source.
Basic steps to implement JDBC:
1. Load DB driver : by calling Class.forName() with drier class
name as an argument.
Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);
2. Create an oracle jdbc
connection
Connection dbConnection = DriverManager.getConnection(url,”loginName”,”Password”);
DriverManager
class manages JDBC drivers. Connection object contains metadata about database,
tables and columns. It also defines method to deal with transactions.
3. Create statements :
Types of
statements:
a. Statement : To execute
simple sql queries, without dynamic parameters.
connection.createStatement()
creates statement object.
createStatement(int resultSetType, int resultSetConcurrency);
createStatement(int resultSetType, int resultSetConcurrency);
b. PreparedStatement :
Execute precompiled sql queries with or without parameters.
prepareStatement(String SQL, int resultSetType, int resultSetConcurrency);
c. callableStatement : to
execute stored procedure.
Connection.prepareCall(“{call
proc1}”);
Will return
callable object to execute stored procedure proc1.
prepareCall(String sql, int resultSetType, int resultSetConcurrency);
4. Execute a SQL statement.
SQL statements
can be executed using 4 methods:
a. executeQuery() – returns
single ResultSet
b. executeUpdate() – to create
/ modify (insert,update,delete) tables
c. execute() – returns multiple
results.
d. executeBatch() – to submit
batch of commands.
ResultSet Interface
This interface exposes methods for retrieving and executing other queries, with other characteristics like concurrency , type and cursor hold-ability.
TYPE_FORWARD_ONLY [Default] : Cursor moves forward only
TYPE_SCROLL_INSENSITIVE: Cursor moves both forward and backward relative to the current position, and it can move to an absolute position. The result set is insensitive to changes made to the underlying data source while it is open
TYPE_SCROLL_SENSITIVE: Cursor can move both forward and backward relative to the current position, and it can move to an absolute position. The result set changes based on modifications in underlying data source while the result set remains open.
TYPE_SCROLL_INSENSITIVE: Cursor moves both forward and backward relative to the current position, and it can move to an absolute position. The result set is insensitive to changes made to the underlying data source while it is open
TYPE_SCROLL_SENSITIVE: Cursor can move both forward and backward relative to the current position, and it can move to an absolute position. The result set changes based on modifications in underlying data source while the result set remains open.
Transaction
By
default JDBC connection uses ato-commit mode, means every SQL statement is
treated as a transation and is automatically committed after its execution.
To
group multiple statements into one transaction, we need to disable auto commit
using setAutoCommit(false) method of connection class.
After
executing multiple statements we can commit transaction using commit()
connection.commit().
Transaction
can be rollback by using rollback().
Connection.rollback().
Isolation Levels
Isolation levels define locking strategy for
JDBC. Below table compares available isolation levels :
Isolation Level
|
Transactions
|
Dirty Reads
|
Non-Repeatable Reads
|
Phantom Reads
|
TRANSACTION_NONE
|
Not supported
|
Not applicable
|
Not applicable
|
Not applicable
|
TRANSACTION_READ_COMMITTED
|
Supported
|
Prevented
|
Allowed
|
Allowed
|
TRANSACTION_READ_UNCOMMITTED
|
Supported
|
Allowed
|
Allowed
|
Allowed
|
TRANSACTION_REPEATABLE_READ
|
Supported
|
Prevented
|
Prevented
|
Allowed
|
TRANSACTION_SERIALIZABLE
|
Supported
|
Prevented
|
Prevented
|
Prevented
|
Dirty Reads : One transaction is modifying any DB value , meanwhile other transaction reads it before first transaction commits.
Non-Repeatable Reads: Say, one transaction T1 have two read operations of same data. After first read data modifies through other transaction. Now for the same transaction T1 two different values for same data will be returned from DB. This is called non-repeatable reads.
Phantom Reads: For the same transaction at different time slots T1 and T2 if number of rows vary then it is phantom read.
Defining savepoints
Savepoints can be set by .setSavepoint().
Savepoint savepoint = connection.setSavepoint();
Transaction can be rollback by
Connection.rollback(savepoint);
The method
Connection.releaseSavepoint takes a Savepoint object as a parameter and removes it from the current transaction.
Sample Code :
No comments:
Post a Comment