What Is JDBC
What Is JDBC
JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses
jdbc drivers to connect to the database.
Before JDBC, ODBC API was used to connect and execute query to the database. But ODBC
API uses ODBC driver that is written in C language which is plateform dependent and
unsecured. That is why Sun Microsystem has defined its own API (JDBC API) that uses JDBC
driver written in Java language.
JDBC Driver
JDBC Driver is a software component that enables java application to interact with the
database.There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
easy to use.
can be easily connected to any database.
Disadvantages:
Performance degraded because JDBC method call is converted into the ODBC function
calls.
The ODBC driver needs to be installed on the client machine.
There are 5 steps to connect any java application with the database in java using JDBC.
The forName() method of Class class is used to register the driver class. This method is
used to dynamically load the driver class.
Class.forName("oracle.jdbc.driver.OracleDriver");
2. Creating connection
The getConnection() method of DriverManager class is used to establish connection with
the database.
3. Creating statement
The createStatement() method of Connection interface is used to create statement. The object
of statement is responsible to execute queries with the database.
4. Executing queries
5. Closing connection
By closing connection object statement and ResultSet will be closed automatically. The
close() method of Connection interface is used to close the connection.
DriverManager class
The DriverManager class acts as an interface between user and drivers. It keeps track of the
drivers that are available and handles establishing a connection between a database and the
appropriate driver. The DriverManager class maintains a list of Driver classes that have
registered themselves by calling the method DriverManager.registerDriver().
A Connection is the session between java application and database. The Connection interface is a
factory of Statement, PreparedStatement, and DatabaseMetaData i.e. object of Connection can be
used to get the object of Statement and DatabaseMetaData. The Connection interface provide
many methods for transaction management like commit(),rollback() etc.
3) public void setAutoCommit(boolean status): is used to set the commit status.By default
it is true.
4) public void commit(): saves the changes made since the previous commit/rollback
permanent.
5) public void rollback(): Drops all changes made since the previous commit/rollback.
6) public void close(): closes the connection and Releases a JDBC resources immediately.
Statement interface
The Statement interface provides methods to execute queries with the database. The statement
interface is a factory of ResultSet i.e. it provides factory method to get the object of ResultSet.
2) public int executeUpdate(String sql): is used to execute specified query, it may be create,
drop, insert, update, delete etc.
3) public boolean execute(String sql): is used to execute queries that may return multiple
results.
ResultSet interface
The object of ResultSet maintains a cursor pointing to a particular row of data. Initially, cursor points to
before the first row.
1. public boolean next():used to move the cursor to the one row next from the current
position.
2. public boolean previous():used to move the cursor to the one row previous from the
current position
3. public boolean first(): used to move the cursor to the first row in result set object.
4. public boolean last(): used to move the cursor to the last row in result set object.
5. public boolean absolute(int row): is used to move the cursor to the specified row
number in the ResultSet object.
6. public boolean relative(int row): used to move the cursor to the relative row number in
the ResultSet object, it may be positive or negative.
7. public int getInt(int columnIndex): used to return the data of specified column index of
the current row as int.
8. public int getInt(String columnName): used to return the data of specified column
name of the current row as int.
9. public String getString(int columnIndex): used to return the data of specified column
index of the current row as String.
10. public String getString(String columnName): used to return the data of specified
column name of the current row as String.
ResultSetMetaData Interface
The metadata means data about data i.e. we can get further information from the data.
If you have to get metadata of a table like total number of column, column name, column type
etc. , ResultSetMetaData interface is useful because it provides methods to get metadata from the
ResultSet object.
Method Description
public String getColumnTypeName(int it returns the column type name for the
index)throws SQLException specified index.
public String getTableName(int it returns the table name for the specified
index)throws SQLException column index.
DatabaseMetaData interface
DatabaseMetaData interface provides methods to get meta data of a database such as database
product name, database product version, driver name, name of total number of tables, name of
total number of views etc.