0% found this document useful (0 votes)
113 views

What Is JDBC

JDBC is an API that allows Java programs to connect to databases. It provides a standard interface for connecting to different database systems and allows programs to send SQL statements to a database and retrieve the results. The key classes and interfaces in JDBC are used to establish a connection to the database, execute queries, and process the results. JDBC drivers implement the standard JDBC interface and provide database-specific functionality to enable communication with the database.

Uploaded by

honaday945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

What Is JDBC

JDBC is an API that allows Java programs to connect to databases. It provides a standard interface for connecting to different database systems and allows programs to send SQL statements to a database and retrieve the results. The key classes and interfaces in JDBC are used to establish a connection to the database, execute queries, and process the results. JDBC drivers implement the standard JDBC interface and provide database-specific functionality to enable communication with the database.

Uploaded by

honaday945
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

What is JDBC ?

JDBC is an acronym for Java Database Connectivity. It’s an advancement


for ODBC ( Open Database Connectivity ).
JDBC is an standard API specification developed in order to move data from
frontend to backend.
This API consists of classes and interfaces written in Java. It basically acts
as an interface or channel between our Java program and databases i.e it
establishes a link between the two so that a programmer could send data
from Java code and store it in the database for future use.
Why JDBC came into existence ?
As previously told JDBC is an advancement for ODBC, ODBC being platform
dependent had a lot of drawbacks. ODBC API was written in C,C++, Python,
Core Java and as we know above languages (except Java and some part of
Python )are platform dependent . Therefore to remove dependence, JDBC
was developed by database vendor which consisted of classes and
interfaces written in Java.

JDBC Driver
JDBC Driver is a software component that enables java application to interact with the database.
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)

1) JDBC-ODBC bridge driver

The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge
method calls into the ODBC function calls. This is now discouraged because of thin driver.
In Java 8, the JDBC-ODBC Bridge has been removed.

Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends that
you use JDBC drivers provided by the vendor of your database instead of the JDBC-
ODBC Bridge.

Advantages:

o easy to use.
o can be easily connected to any database.

Disadvantages:

o Performance degraded because JDBC method call is converted into the ODBC
function calls.
o 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
of the database API. It is not written entirely in java.
Advantage:

o performance upgraded than JDBC-ODBC bridge driver.

Disadvantage:

o The Native driver needs to be installed on the each client machine.


o The Vendor client library needs to be installed on client machine.

3) Network Protocol driver


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:

o No client side library is required because of application server that can perform
many tasks like auditing, load balancing, logging etc.

Disadvantages:

o Network support is required on client machine.


o Requires database-specific coding to be done in the middle tier.
o Maintenance of Network Protocol driver becomes costly because it requires
database-specific coding to be done in the middle tier.

4) Thin driver

The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is wh
driver. It is fully written in Java language.
Advantage:

o Better performance than all other drivers.


o No software is required at client side or server side.

Disadvantage:

o Drivers depend on the Database.

Steps for connectivity between Java program and database

1. Loading the Driver


To begin with, we first need load the driver or register it before using it in the
program . Registration is to be done once in our program. We can register a
driver in one of two ways mentioned below :
 Class.forName() : Here we load the driver’s class file into memory at the
runtime. No need of using new or creation of object .The following
example uses Class.forName() to load the Oracle driver –

Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(): DriverManager is a Java inbuilt class
with a static member register. Here we call the constructor of the driver
class at compile time . The following example uses
DriverManager.registerDriver()to register the Oracle driver –

2. Create the connections


After loading the driver, establish connections using :

Connection con = DriverManager.getConnection(url,user,password)


DriverManager.getConnection(
"jdbc:mysql://localhost:3306/data","root","root");

user – username from which your sql command prompt can be accessed.
password – password from which your sql command prompt can be
accessed.
con: is a reference to Connection interface.
url : Uniform Resource Locator.

3. Create a statement
Once a connection is established we can interact with the database. The
JDBCStatement, CallableStatement, and PreparedStatement interfaces
define the methods that enable us to send SQL commands and receive data
from our database.
Use of JDBC Statement is as follows:
Statement st = con.createStatement();
Here, con is a reference to Connection interface used in previous step .
4. Execute the query
Now comes the most important part i.e executing the query. Query here is an
SQL Query . Now we know we can have multiple types of queries. Some of
them are as follows:
 Query for updating / inserting table in a database.
 Query for retrieving data .
The executeQuery() method of Statement interface is used to execute
queries of retrieving values from the database. This method returns the
object of ResultSet that can be used to get all the records of a table.
The executeUpdate(sql query) method of Statement interface is used to
execute queries of updating/inserting .
int m = st.executeUpdate(sql);
if (m==1)
System.out.println("inserted successfully : "+sql);
else
System.out.println("insertion failed");
Here sql is sql query of the type String
5.Close the connections
By closing connection, objects of Statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close
the connection.
Example :
con.close();

Example

import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}

ResultSet interface
The object of ResultSet maintains a cursor pointing to a row of a table. Initially, cursor points
to before the first row.
By default, ResultSet object can be moved forward only and it is not updatable.
But we can make this object to move forward and backward direction by passing either
TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method
as well as we can make this object as updatable by:

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,


ResultSet.CONCUR_UPDATABLE);
Commonly used methods of ResultSet interface
1) public boolean next(): is used to move the cursor to the one row next from the
current position.
2) public boolean previous(): is used to move the cursor to the one row previous from the
current position.
3) public boolean first(): is used to move the cursor to the first row in result set object.
4) public boolean last(): is 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): is 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): is used to return the data of specified column index of
the current row as int.
8) public int getInt(String columnName): is used to return the data of specified column
name of the current row as int.
9) public String getString(int columnIndex): is used to return the data of specified column
index of the current row as String.
10) public String getString(String columnName): is used to return the data of specified
column name of the current row as String.

You might also like