JDBC, which stands for Java Database Connectivity, is an API provided by the Java programming languages that provides a standard way to connect to relational databases. Following are the key database-independent tasks that are standardized by the JDBC API.
There are two key steps to connecting to a database using Java JDBC API
1. Load JDBC Driver - Every database that can be connected using JDBC API must have a corresponding JDBC Driver class that implements java.sql.Driver interface. Before you can make a connection to the database you must load the driver. From JDBC 4.0 onwards any JDBC driver found in the classpath is automatically loaded. Prior to JDBC 4.0 you have to load the driver specifically using Class.forName(JDBC Driver).
2. Open database connection -After loading the driver, you can open a connection to the database by calling getConnection() method on DriverManager class and passing the database connection URL, user id and password as arguments.
A Statement is an interface provided by the Java programming language that represents a SQL statement. You execute a statement object which returns a ResultSet object, which contains the database data returned by that query.
There are three kinds of statements. Statement, PreparedStatement and CallableStatement.
Statement is used to represent simple SQL queries that have no parameters.
PreparedStatement which extends Statement represents pre-compiled SQL statements that may contain input parameters.
CallableStatement which extends from PreparedStatement is used to execute stored procedures that may contain both input and output parameters.
You execute SQL queries by calling the execute() method on the statement object. Based on the type of query you can call either execute(), executeQuery() or executeUpdate().
execute() returns true if the query returns a result set. Use execute() if the query returns more than one result set. You can then get each result set by calling Statement.getResultSet().
executeQuery() returns one ResultSet object.
executeUpdate() is used to execute insert, delete or update SQL statements which update the rows of the database. execute() returns an integer representing the number of rows that were updated.
You can call a stored procedure by calling the execute() method on the CallableStatement object.
*** See complete answer in the Java Interview Guide.
ResultSet object contains the database data that is returned by the query. You can iterate and access the data from the result set via cursor which is a pointer pointing to one row in the ResultSet object.
*** See complete answer in the Java Interview Guide.
The ResultSet can be of three different types based on the flexibility of cursor movement and sensitivity of data in the database.
1. TYPE_FORWARD_ONLY - The result set cannot be scrolled. Its cursor can move forward only from before the first row to after the last row. You can move from one row to the next by calling next() on the result set.
*** See complete answer in the Java Interview Guide.
The concurrency of a ResultSet object defines if a ResultSet object can be updated or not. There are two types of ResultSet objects based on concurrence.
1. CONCUR_READ_ONLY - The ResultSet object cannot be updated using the ResultSet interface.
*** See complete answer in the Java Interview Guide.
Following are some commonly used methods to scroll a ResultSet.
next() - Moves the cursor forward one row from its current position.
previous() - Moves the cursor to the previous row in this ResultSet object.
first() - Moves the cursor to the first row in this ResultSet object.
*** See complete answer in the Java Interview Guide.
ResultSetMetaData object contains information regarding the types and properties of columns in ResultSet object. ResultSetMetaData object is retrieved by calling ...
*** See complete answer in the Java Interview Guide.
RowSet objects are derived from ResultSet and adds following capabilities...
*** See complete answer in the Java Interview Guide.
A RowSet object is considered either as connected or disconnected.
1. A connected RowSet is connected to a database, via a JDBC driver, throughout its life span.
*** See complete answer in the Java Interview Guide.
There are five different RowSet implementations.
1. JdbcRowSet – JdbcRowSet is an implementation of connected RowSet object. JdbcRowSet is similar to ResultSet. JdbcRowSet is commonly used to wrap a non-scrollable and read-only ResultSet object to add the scrolling and update capabilities to the ResultSet.
*** See complete answer in the Java Interview Guide.
A transaction is a set of statements that executes as a unit. So either all of the statements in a transaction are executed successfully or none of them are.
*** See complete answer in the Java Interview Guide.