Some of major classes and interfaces which are frequently used while developing a Java application are as follows:
• DriverManager class
• Driver interface
• Connection interface
• Statement interface
We’ll be covering the following topics in this tutorial:
It is a predefined class present/declared in java.sql package. It is a non-abstract class in the JDBC API. It has no constructor which implies that this class cannot be inherited or initialized directly.
All the methods or properties of this class are declared as static, so it is not necessary to construct an object of DriverManager Class. We can call these static methods using class name.
The main responsibility of the DriverManager class is preparing a connection by using Driver implementation that accepts the given JDBC URL.
Methods in DriverManager
(a) public static Connection getConnection (String url, String user name, String pswd);
Establishes connection between driver and database. This class selects a driver from the list of drivers and creates a connection by checking user parameter for which the connection is being made. Password parameter represents the password of the user.
(b) public static Driver getDriver (String url) throws SQLException
Locates the requested driver in the DriverManager class. The URL parameter specifies the URL of requested driver.
(c) public static void register (Driver drvr) throws SQLException:
Registers a requested driver with DriverManager Class.
(d) public static void deregister (Driver drvr) throws SQLException
Drops a driver from the list of the driver maintained by DriverManager.
It is a predefined interface present in java.sql. package. It is a basic interface that every JDBC driver provider has to implement. The java.sql.Driver is an Interface defined under JDBCAPI to describe an object that is responsible to establish connection to database. All the driver classes implement this interface to connect to a database.
Methods in Driver Interface
(a) public Connection connect (String url, Properties info)
This method establishes connectivity with database. The URL parameter specifies a URL that describes the database details to which the driver is to be connected. The info parameter specifies the information of the tag/value pair used in driver.
It is an interface present in java.sql package. This interface defines an abstraction to access the session, established with the database server. The JDBC connection helps to perform the following operation:
• Create JDBC Statement
• Control local transaction
Methods in Connection Interface
(a) public void close () throws SQLException
Closes the connection and releases the connection object associated with the connected database.
(b) public Statement createStatement () throws SQLException
Create a Statement object to send sql statements to the specified database.
(c) public PreparedStatement prepareStatement (String sql) throws SQLException
Create a PreparedStatement object to send sql statements over a connection.
(d) public CallableStatement prepareCall (String sql) throws SQLException
Create a CallableStatement object for calling database stored procedures.
(e) public void commit () throws SQLException
Commits the changes made in the previous commit and releases any database locks held by the current Connection object.
(f) public void rollback () throws SQLException
Rolls back all the transactions and releases any locks that are currently held by the Connection object.
It is a predefined interface present in java.sql package. It is a part of JDBC API that abstracts an object. It is responsible to execute sql statement and return the result. A
Statement object can open a single ResultSet object at a time.
The PreparedStatement interface is dealing with IN parameter whereas the CallableStatement interface deals with both IN and OUT.
Methods of Statement Interface
(a) public int executeUpdate (String sql) throws SQLException
Execute the Insert, Update and Delete Statement or the SQL DDL statements.
(b) public ResultSet executeQuery (String sql) throws SQLException
Execute a sql command and return a single ResultSet.
(c) public void close () throws SQLException
Closes the Statement object, therefore, it releases its control from the database and also from the connection.
(d) public ResultSet getResultSet () throws SQLException
Retrieves the current Resultset object generated by Statement object.
(e) public Connection getConnection () throws SQLException
Accepts the Connection object made to the database.
This interface provides methods for the retrieval of data returned by an SQL statement execution. A ResultSet maintains a cursor pointing to its current row of data. The most often used methods, namely, getXXX and updateXXX methods are present in this interface.
Methods in ResultSet
(a) public booleannext () throws SQLException
Moves the cursor down one row from its current position.
(b) public void close () throws SQLException
Releases this ResultSet object’s database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
(c) public booleanwasNull () throws SQLException
Reports whether the last column read had a value of SQL NULL.
(d) public StringgetXXX (int columnIndex) throws SQLException
Retrieves the value of the designated column in the current row of this ResultSet object as per the type of data.
(e) public void beforeFirst () throws SQLException
Moves the cursor to the front of this ResultSet object, just before the first row.
(f) public void afterLast () throws SQLException
Moves the cursor to the end of this ResultSet object, just after the last row.
(g) public booleanabsolute (int row) throws SQLException
Moves the cursor to the given row number in this ResultSet object.
(h) public booleanprevious () throws SQLException
Moves the cursor to the previous row in this ResultSet object.
(i) public void deleteRow () throws SQLException
Deletes the current row from this ResultSet object and from the underlying database.
(j) public void insertRow () throws SQLException
Inserts the contents of the insert row into this ResultSet object and into the database.