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

JDBC (Autosaved)

JDBC (Java Database Connectivity) is an API that allows Java programs to execute SQL statements and retrieve results from a database. There are two main types of storage areas: temporary storage areas like JVM memory that clear when the program ends, and permanent storage areas like file systems and databases that persist data. Databases are better than file systems for storing large amounts of structured data as they support queries, security, and prevent data inconsistencies. The JDBC architecture uses DriverManager to manage JDBC drivers that allow communication between Java programs and different database types through a standardized interface.

Uploaded by

aviaravind753
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)
15 views

JDBC (Autosaved)

JDBC (Java Database Connectivity) is an API that allows Java programs to execute SQL statements and retrieve results from a database. There are two main types of storage areas: temporary storage areas like JVM memory that clear when the program ends, and permanent storage areas like file systems and databases that persist data. Databases are better than file systems for storing large amounts of structured data as they support queries, security, and prevent data inconsistencies. The JDBC architecture uses DriverManager to manage JDBC drivers that allow communication between Java programs and different database types through a standardized interface.

Uploaded by

aviaravind753
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/ 45

JDBC

(Java Database
Connectivity)
Storage areas or options
Storage areas or options
As the part of our Applications, we required to store our data like
customers information, Billing Information, Calls Information etc..
To store this Data, we required Storage Areas. There are 2 types of
Storage Areas.
Temporary Storage Areas
Permanent Storage Areas
Temporary Storage Areas:

These are the Memory Areas where Data will be stored temporarily.

Ex: All JVM Memory Areas (like Heap Area, Method Area, Stack Area etc).
Once JVM shutdown all these Memory Areas will be cleared automatically.
Permanent Storage Areas:

Also known as Persistent Storage Areas.


Here we can store Data permanently.
Ex: File Systems, Databases, Data warehouses, Big Data Technologies
File Systems:
File Systems can be stored unstructured data.
File Systems can be provided by Local operating System.
File Systems are best suitable to store very less Amount of Information.
Limitations:

We cannot store huge amount of data.


There is no Query Language support and hence operations will become
very complex.
There is no security for the data.
There is no mechanism to prevent duplicate data. Hence there may be a
chance of data inconsistency problems.
To overcome the above problems of File Systems, we should go for
Databases.
Databases:
We can store huge amount of data in the Databases.
Query language support is available for every Database and hence we can
perform Database operations very easily.
To access data present in the Database, compulsory username and pwd must
be required. Hence data is secured.
Inside Database data will be stored in the form of Tables. While developing
database table schemas, Database admin follow various normalization
techniques and can implement various constraints like unique key constrains,
primary key constraints etc which prevent data duplication.
 Hence there is no chance of Data Inconsistency Problems.
Limitations of Databases:
Database cannot hold very huge amount of information like terabytes of
Data.
Database can provide support only for structured data (Tabular Data OR
Relational Data) and cannot provide support for semi structured data
(like XML Files) and Unstructured Data (like Video Files, Audio Files,
Images etc)
To overcome these problems we should go for more advanced storage
areas like Big Data Technologies, Data warehouses etc..
JDBC
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.
It is a specification from Sun microsystems that provides a standard
abstraction(API or Protocol) for java applications to communicate with
various databases.
It provides the language with java database connectivity standards.
JDBC is a Technology, which can be used to communicate with
Database from Java Application.
JDBC Features:
JDBC API is standard API. We can communicate with any Database
without rewriting our Application i.e. it is Database independent API.
 JDBC Drivers are developed in Java and hence JDBC Concept is
applicable for any Platform. i.e. JDBC is platform independent technology.
By using JDBC API, we can perform basic CRUD operations very easily.
We can also perform complex operations (like Inner Joins, Outer Joins,
calling Stored Procedures etc) very easily by using JDBC API.
 JDBC API supported by large number of vendors and they developed
multiple Products based on JDBC API.
Difference between ODBC and JDBC

ODBC JDBC

ODBC Stands for Open Database Connectivity. JDBC Stands for java database connectivity.

Introduced by Microsoft in 1992. Introduced by SUN Micro Systems in 1997.

We can use ODBC for any language like C,C++,Java etc. We can use JDBC only for Java languages.

We can choose ODBC only windows platform. We can Use JDBC in any platform.

Mostly ODBC Driver developed in native languages like


JDBC Stands for java database connectivity.
C,C++.

For Java applications it is not recommended to use ODBC


For Java application it is highly recommended to use JDBC
because performance will be down due to internal
because there are no performance & platform dependent
conversion and applications will become platform
problem.
Dependent.

ODBC is procedural. JDBC is object oriented.


JDBC Architecture
JDBC Architecture
JDBC Architecture
Application: It is a java applet or a servlet that communicates with a data
source.
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:
DriverManager: It plays an important role in the JDBC architecture. It
uses some database-specific drivers to effectively connect enterprise
applications to databases.
JDBC drivers: To communicate with a data source through JDBC, you
need a JDBC driver that intelligently communicates with the respective
data source.
DriverManager:

It is the Key Component in JDBC Architecture.


DriverManager is a Java Class present in java.sql package.
It is responsible to manage all Database drivers available in our system.
DriverManager is responsible to register and unregister Database Drivers.
DriverManager.registerDriver(Driver);
DriverManager.unregisterDriver(Driver);
DriverManager is responsible to establish connection to the Database with
the help of Driver Software.
Connection con = DriverManager.getConnection (jdbcurl, username, pwd);
JDBC API
JDBC API provides several Classes and Interfaces.
Programmer can use these Classes and Interfaces to communicate
with the Database.
Driver Software Vendor can use JDBC API while developing Driver
Software.
JDBC API defines 2 Packages
java.sql Package:
javax.sql Package:
java.sql Package:

Interface Summary
Interface Description
Array The mapping in the Java programming language for the SQL type ARRAY.
Blob The representation (mapping) in the Java™ programming language of an
SQL BLOB value.

CallableStatement The interface used to execute SQL stored procedures.


Clob The mapping in the Java™ programming language for the SQL CLOB type.
Connection A connection (session) with a specific database.
DatabaseMetaData Comprehensive information about the database as a whole.
Driver The interface that every driver class must implement.
Class Summary
Class Description
Date A thin wrapper around a millisecond value that allows JDBC to identify this as an
SQL DATE value.

DriverManager The basic service for managing a set of JDBC drivers.


NOTE: The DataSource interface, new in the JDBC 2.0 API, provides another way to
connect to a data source.

DriverPropertyInfo Driver properties for making a connection.


javax.sql Package:
Interface Summary
Interface Description

ConnectionPoolDataSource A factory for PooledConnection objects.

DataSource A factory for connections to the physical data source that this DataSource object represents.

PooledConnection An object that provides hooks for connection pool management.


RowSet The interface that adds support to the JDBC API for the JavaBeans™ component model.
RowSetInternal The interface that a RowSet object implements in order to present itself to
a RowSetReader or RowSetWriter object.

RowSetListener An interface that must be implemented by a component that wants to be notified when a
significant event happens in the life of a RowSet object.

RowSetMetaData An object that contains information about the columns in a RowSet object.
Class Summary
Class Description

ConnectionEvent An Event object that provides information about the source of a connection-
related event.

RowSetEvent An Event object generated when an event occurs to a RowSet object.

StatementEvent A StatementEvent is sent to all StatementEventListeners which were


registered with a PooledConnection.
Types of drivers
While communicating with Database, we have to convert Java calls into
Database specific calls and Database specific calls into Java calls. For this
driver software is required.
There are many drivers are available. But based on functionality all drivers
are divided into 4 Types.
Type-1 Driver (JDBC-ODBC Bridge Driver OR Bridge Driver)
Type-2 Driver (Native API-Partly Java Driver OR Native Driver)
Type-3 Driver (All Java Net Protocol Driver OR Network Protocol Driver OR
Middleware Driver)
Type-4 Driver (All Java Native Protocol Driver OR Pure Java Driver OR Thin
Driver)
Type-1 Driver
Type-1 Driver:
Type-1 driver provided by Sun Micro Systems as the part of JDK. But
this Support is available until 1.7 version only.
Internally this driver will take support of ODBC Driver to communicate
with Database.
Type-1 Driver converts JDBC Calls (Java Calls) into ODBC Calls and
ODBC Driver converts ODBC calls into Database specific Calls.
Hence Type-1 Driver acts as Bridge between JDBC and ODBC.
Advantages :
It is very easy to use and maintain.
We are not required to install any separate Software because it is
available as the Part of JDK.
Type-1 Driver won't communicates directly with the Database. Hence
it is Database Independent Driver. Because of this migrating from one
Database to another Database will become very easy.
Limitations:
It is the slowest Driver among all JDBC Drivers (Snail Driver), because first
it will convert JDBC Calls into ODBC Calls and ODBC Driver converts
ODBC Calls into Database specific Calls.

This Driver internally depends on ODBC Driver, which will work only on
Windows Machines. Hence Type-1 Driver is Platform Dependent Driver.

No Support from JDK 1.8 Version onwards.


Type-2 Driver
It is also known as Native API -partly Java Driver OR Native Driver.
Type-2 Driver is exactly same as Type-1 Driver except that ODBC Driver
is replaced with vendor specific Native Libraries.
Type-2 Driver internally uses vendor specific native libraries to
communicate with Database.
Native libraries means the set of Functions written in Non-Java (Mostly C
OR C++).
We have to install Vendor provided Native Libraries on the Client Machine.
Type-2 Driver converts JDBC Calls into Vendor specific Native Library
Calls, which can be understandable directly by Database Engine.
Type-2 Driver
Advantages:
When compared with Type-1 Driver Performance is High, because it
required only one Level Conversion from JDBC to Native Library
Calls.
No need of arranging ODBC Drivers.
When compared with Type-1 Driver, Portability is more because
Type-1 Driver is applicable only for Windows Machines.
Limitations:
Internally this Driver using Database specific Native Libraries and hence it
is Database dependent Driver. Because of this migrating from one Database
to another Database will become Difficult.
This Driver is Platform Dependent Driver.
On the Client Machine compulsory we should install Database specific
Native Libraries.
There is no Guarantee for every Database Vendor will provide This Driver.
(Oracle is providing Type-2 Driver but MySql won’t providing this Driver)
Type-3 Driver
Also known as All Java Net Protocol Driver OR Network Protocol Driver
OR Middleware Driver.

Type-3 Driver converts JDBC Calls into Middleware Server specific Calls.
Middleware Server can convert Middleware Server specific calls into
Database specific calls.

Internally Middleware Server may use Type-1, 2 OR 4 Drivers to


communicates with Database.
Type-3 Driver
Type-3 Driver converts JDBC Calls into Middleware Server specific
Calls.

Middleware Server can convert Middleware Server specific Calls into


Database specific Calls.

Internally Middleware Server may use Type-1, 2 OR 4 Drivers to


communicates with Database.
Advantages
This Driver won't communicate with Database directly and hence it is
Database Independent driver.

This Driver is Platform Independent Driver.

No need of ODBC Driver OR Vendor specific Native Libraries


Limitations
Because of having Middleware Server in the Middle, there may be a
chance of performance problems.

We need to purchase Middleware Server and hence the cost of this Driver
is more when compared with remaining drivers.

Ex: IDS Driver (Internet Database Access Server)

The only Driver which is both platform independent and Database


independent is Type-3 driver. Hence it is recommended to use
Type-4 Driver:
Also known as Pure Java Driver OR Thin Driver.
This Driver is developed to communicate with the Database directly
without taking support of ODBC Driver or vendor specific Native
Libraries OR Middleware Server.
This Driver uses Database specific native protocols to communicate with
the Database.
This Driver converts JDBC Calls directly into Database specific Calls.
This Driver developed only in Java and hence it is also known as Pure
Java Driver. Because of this Type-4 Driver is Platform Independent
Driver.
This Driver won't require any Native Libraries at Client side and hence it
is light weighted. Because of this it is treated as Thin Driver.
Type-4 Driver
Advantages:
It won't require any Native Libraries, ODBC Driver OR Middleware
Server
It is Platform Independent Driver
It uses Database vendor specific native protocol and hence Security is
more.
Limitations
The only Limitation of this Driver is, it is Database Dependent Driver
because it is communicating with the Database directly.

Ex:
Connector/J Driver for MySQL
Thin Driver for Oracle
Standard Steps for developing JDBC Application

Load and register Driver Class


Establish Connection between Java Application and Database
Create Statement Object
Send and execute SQL Query
Process Result from ResultSet
Close Connection

You might also like