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

JDBC

This document discusses Java Database Connectivity (JDBC), which provides APIs for Java programs to connect to and interact with databases. It describes the key components and architecture of JDBC, including the JDBC driver manager, drivers, interfaces, and classes. The document also outlines the steps to create a basic JDBC application, such as loading the appropriate driver, establishing a database connection, executing queries, processing result sets, and closing resources.

Uploaded by

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

JDBC

This document discusses Java Database Connectivity (JDBC), which provides APIs for Java programs to connect to and interact with databases. It describes the key components and architecture of JDBC, including the JDBC driver manager, drivers, interfaces, and classes. The document also outlines the steps to create a basic JDBC application, such as loading the appropriate driver, establishing a database connection, executing queries, processing result sets, and closing resources.

Uploaded by

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

BY

S. NAVYA
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY
JDBC

 JDBC refers to the Java Database Connectivity. It provides java API that allows
Java programs to access database management systems (relational database).
The JDBC API consists of a set of interfaces and classes which enables java
programs to execute SQL statements. Interfaces and classes in JDBC API are
written in java

 JDBC is an API(Application programming interface) used in java programming


to interact with databases. The classes and interfaces of
JDBC allow the application to send requests made by users to the specified
database..
Purpose of JDBC

 Enterprise applications created using the JAVA EE technology need to interact with
databases to store application-specific information. So, interacting with a database
requires efficient database connectivity, which can be achieved by using the ODBC
(Open database connectivity) driver. This driver is used with JDBC to interact or
communicate with various kinds of databases such as Oracle, MS Access, Mysql, and
SQL server database.
Components of JDBC
 There are generally four main components of JDBC through which it can interact with a database.
They are as mentioned below:
1. JDBC API: It provides various methods and interfaces for easy communication with the database. It
provides two packages as follows, which contain the java SE and Java EE platforms to exhibit
WORA(write once run anywhere) capabilities.
 java.sql.*;
 It also provides a standard to connect a database to a client application.
2. JDBC Driver manager: It loads a database-specific driver in an application to establish a connection
with a database. It is used to make a database-specific call to the database to process the user request.

3. JDBC Test suite: It is used to test the operation(such as insertion, deletion, updation) being
performed by JDBC Drivers.

4. JDBC-ODBC Bridge Drivers: It connects database drivers to the database. This bridge translates the
JDBC method call to the ODBC function call. It makes use of the sun.jdbc.odbc package which includes a
native library to access ODBC characteristics.
Architecture of JDBC
1. Application: It is a java applet or a servlet that communicates with a
data source.
2. The JDBC API: The JDBC API allows Java programs to execute SQL
statements and retrieve results. Some of the important classes and
interfaces defined in JDBC API are as follows:
3. DriverManager: It plays an important role in the JDBC architecture. It
uses some database-specific drivers to effectively connect enterprise
applications to databases.
4. JDBC drivers: To communicate with a data source through JDBC, you
need a JDBC driver that intelligently communicates with the respective
data source.
JDBC Drivers

 JDBC drivers are client-side adapters (installed on the client


machine, not on the server) that convert requests from Java
programs to a protocol that the DBMS can understand. There are
4 types of JDBC drivers:
1. Type-1 driver or JDBC-ODBC bridge driver
2. Type-2 driver or Native-API driver
3. Type-3 driver or Network Protocol driver
4. Type-4 driver or Thin driver
Types of JDBC Architecture(2-tier and 3-tier)

 The JDBC architecture consists of two-tier and three-tier processing models to access a database. They
are as described below:
1. Two-tier model: A java application communicates directly to the data source. The JDBC driver enables
the communication between the application and the data source. When a user sends a query to the data
source, the answers for those queries are sent back to the user in the form of results.
The data source can be located on a different machine on a network to which a user is connected. This is
known as a client/server configuration, where the user’s machine acts as a client, and the machine has
the data source running acts as the server.

2. Three-tier model: In this, the user’s queries are sent to middle-tier services, from which the commands
are again sent to the data source. The results are sent back to the middle tier, and from there to the user.
This type of model is found very useful by management information system directors.
Interfaces of JDBC API
 A list of popular interfaces of JDBC API is given below:
• Driver interface
• Connection interface
• Statement interface
• PreparedStatement interface
• CallableStatement interface
• ResultSet interface
• ResultSetMetaData interface
• DatabaseMetaData interface
• RowSet interface
Classes of JDBC API

 A list of popular classes of JDBC API is given below:


• DriverManager class
• Blob class
• Clob class
• Types class
Working of JDBC

 Java application that needs to communicate with the database has


to be programmed using JDBC API. JDBC Driver supporting
data sources such as Oracle and SQL server has to be added in
java application for JDBC support which can be done
dynamically at run time. This JDBC driver intelligently
communicates the respective data source.
Creating JDBC Application
 There are following six steps involved in building a JDBC application −
• Import the packages − Requires that you include the packages containing the
JDBC classes needed for database programming. Most often, using import
java.sql.* will suffice.
• Open a connection − Requires using the DriverManager.getConnection() method
to create a Connection object, which represents a physical connection with the
database.
• Execute a query − Requires using an object of type Statement for building and
submitting an SQL statement to the database.
• Extract data from result set − Requires that you use the
appropriate ResultSet.getXXX() method to retrieve the data from the result set.
• Clean up the environment − Requires explicitly closing all database resources
versus relying on the JVM's garbage collection.
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getStrin
g(3));
//step5 close the connection object
con.close();
}catch(Exception e){ System.out.println(e);} } }

You might also like