Java Notes 28 JDBC
Java Notes 28 JDBC
Q) What is JDBC?
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query
with the database.
We can use JDBC API to access tabular data stored in any relational database. By the help of JDBC
API, we can save, update, delete and fetch data from the database.
Before JDBC, ODBC API was the database API to connect and execute the query with the
database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform
dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses
JDBC drivers (written in Java language).
We can use JDBC API to handle database using Java program and can perform the following
activities:
JDBC architecture?
Description:
DBC Driver
JDBC Driver is a software component that enables java application to interact with the database.
There are 4 types of JDBC drivers:
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge
driver converts JDBC method calls into the ODBC function calls. This is now discouraged because of
thin driver.
Advantages:
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.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts
JDBC method calls into native calls of the database API. It is not written entirely in java.
Advantage:
Disadvantage:
The Network Protocol driver uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor-specific database protocol. It is fully written in java.
Advantage:
No client side library is required because of application server that can perform many
tasks like auditing, load balancing, logging etc.
Disadvantages:
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That
is why it is known as thin driver. It is fully written in Java language.
Advantage:
Disadvantage:
Q. What are the steps to connect to a jdbc database. Show demo with a prpgram?
Ans. Here are 5 steps to connect any java application with the database using JDBC. These
steps are as follows:
The forName() method of Class class is used to register the driver class. This method is used to
dynamically load the driver class.
The getConnection() method of DriverManager class is used to establish connection with the
database.
The createStatement() method of Connection interface is used to create statement. The object of
statement is responsible to execute queries with the database.
The executeQuery() method of Statement interface is used to execute queries to the database. This
method returns the object of ResultSet that can be used to get all the records of a table.
By closing connection object statement and ResultSet will be closed automatically. The close()
method of Connection interface is used to close the connection.
import java.sql.*;
class OracleCon{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
ResultSet rs=stmt.executeQuery("select * from emp"); //THIS IS A SQL QUERY TO RETRIEVE ALL THE
RECORDS FROM THE DATABASE
while(rs.next())
con.close();