Java Database Connectivity

Java Database Connectivity (JDBC) is Java's API for interacting with the relational databases. JDBC is a specification interface, while individual database vendors develop the drivers library adhering to JDBC.

The following is the syntax for a simple database connection and query execution to obtain the results into the object called ResultSet:

Connection dbConn = DriverManager.getConnection(databaseURL, username, password);
Statement qryStmt = dbConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet queryResults = qryStmt.executeQuery("SELECT <COLUMNS TO RETRIEVE> FROM <TABLES / VIEWS ALONG WITH CRITERIA>");

Here is the sample program for an UpdatableResultSet to retrieve, update, delete, and add a new row into an Oracle database:

package resultset;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdatableResultSet {
public static void main(String[] args) {
try (
//Load Driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
// Oracle Connection
Connection databaseConn = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");

// TYPE_SCROLL_INSENSITIVE: Indicates the ResultSet is scrollable
// but not sensitive to the data changes
// that underlies the ResultSet.
// CONCUR_UPDATABLE: Resultset concurrency mode is updatable
Statement queryStmt = databaseConn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
) {
// Disable auto-commit before executing the query
databaseConn.setAutoCommit(false);
ResultSet resultSet = queryStmt.executeQuery(
"select * from department");
// To update a row using the result set
resultSet.last();
System.out.println("*** Updating the row using ResultSet ***");
System.out.println(
resultSet.getRow() + ": " + + resultSet.getInt("deptId") +
", " + resultSet.getString("deptName") + ", " +
resultSet.getString("manager") + ", "
+ resultSet.getDouble("productivity") + ", " +
resultSet.getInt("effortHours"));
resultSet.updateDouble("productivity", 99.99);
// update a field value by its column name
resultSet.updateInt("effortHours", 99);
resultSet.updateRow(); // update the row in the database
//Print the row information after update operation
System.out.println(
resultSet.getRow() + ": " + + resultSet.getInt("deptId") + ", "
+ resultSet.getString("deptName") + ", "
+ resultSet.getString("manager") + ", "
+ resultSet.getDouble("productivity") + ", "
+ resultSet.getInt("effortHours"));

// Delete a row using the result set
resultSet.first();
System.out.println("*** Delete the row using ResultSet ***");
System.out.println(
resultSet.getRow() + ": " + + resultSet.getInt("deptId") + ", "
+ resultSet.getString("deptName") + ", "
+ resultSet.getString("manager") + ", "
+ resultSet.getDouble("productivity") + ", "
+ resultSet.getInt("effortHours"));
resultSet.deleteRow();
// delete the current row in the database

// A updatable ResultSet has feature to stage a
// row before inserting into the database
resultSet.moveToInsertRow();
resultSet.updateInt(1, 758);
// Update fields using the column number instead of column name
resultSet.updateString(2, "Human Resources");
resultSet.updateString(3, "Trump");
resultSet.updateDouble(4, 55.40);
resultSet.updateInt(5, 65);
resultSet.insertRow();
// row is inserted into the database

// Move the cursor to the starting of the ResultSet.
resultSet.beforeFirst();

databaseConn.commit(); // commit
} catch(SQLException ex) {
ex.printStackTrace();
}
}
}

The ResultSet has a bigger hierarchy to support additional features, as shown in the following diagram:

Each implementation of ResultSet shown in the preceding hierarchy adds its own new feature to it, as mentioned below.

RowSet implements the ResultSet interface. RowSet, along with the ResultSet behavior, it is scrollable, updatable, and supports the Java bean component model.

JdbcRowSet is a connected RowSet and a wrapper component for the scrollable and updatable ResultSet.

CachedRowSet and its subcomponents have the ability of disconnected access; they can also reconnect to the data source to capture the updates that happened during disconnect.

WebRowSet is an XML-based CachedRowSet.

JoinRowSet can perform the join operation such as SQL, even without connecting to the data source during join.

FilteredRowSet can filter the rows from the result, even without connecting to the data source during filter.

Let's now review how the distributed transactions are managed by JDBC with the help of JTA API from Java to interact with the distributed database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset