JDBC Connection in Java
JDBC Connection in Java
Introduction
• JDBC is a Java API (set of classes and interfaces) for executing
database (SQL) statements in a Java program.
• History
Part of JDK Since JDK 1.1
Package: java.sql
• Use
Create tables, insert values into them, query tables, retrieve results
from queries, and update tables.
JDBC Architecture
Type IV Data
“Pure” Source
Type 1: JDBC-ODBC Bridge
• The JDBC to ODBC bridge driver connects Java Program to
Microsoft ODBC (Open Database Connectivity) data sources
• The Java 2 Software Development kit from Sun Microsystems
includes the JDBC-ODBC bridge
• This driver requires the ODBC driver on the client computer and
normally requires configuration of ODBC data sources
• Uses bridge to connect Java client to an ODBC database system.
Translates all JDBC calls into ODBC and sends them to ODBC driver
• This really refers to a C program, compiled to native code, that
interfaces the Java Virtual Machine to ODBC.
Server Data
Java Application Client
Source
JDBC JDBC-ODBC
ODBC ODBC
API Bridge
API Layer
Server
JDBC Vendor
JDBC Driver
API API
Data
Source
JDBC
JDBC Driver JDBC Driver Native
API
Server Driver
•JDBC calls are translated into a middleware vendor's protocol, and the
middleware converts those calls into a database's API.
•Type 3 JDBC drivers offer the advantage of being server-based, meaning
that they do not require native client code, which makes Type 3 drivers faster
than Type 1 and Type 2 drivers.
•Developers can also use a single JDBC driver to connect to multiple
databases.
Contd..
Advantages:
• Pure Java. No native code to be installed on client machine.
• Flexible when client wants to communicate with multiple databases
from different vendors.
• Useful for intranet applications.
• Features of connection pooling, data caching etc.
Disadvantages
• Type 3 drivers require database-specific coding to be done in the
middle tier.
• Slow- presence of gateway. Gateway reads data from database then
sends to client.
Type 4: Pure Java Drivers
• Also known as Native-protocol pure Java driver
• Type 4 JDBC drivers are direct-to-database pure Java drivers ("thin"
drivers).
• Convert JDBC API calls to direct network calls using vendor-specific
networking protocols by making direct connections with database
• They understand database-specific networking protocols and can access
database directly without any additional software.
• Are completely implemented in Java to achieve platform independence
• Each DBMS requires its own Type 4 driver; therefore, there are more
drivers to manage in a heterogeneous computing environment, but this
is outweighed by the fact that Type 4 drivers provide faster
performance and direct access to DBMS features.
• e.g. Oracle, mySQL
Server
Java Client
Application
Data
Source
JDBC
API JDBC Driver
CallableStatement
java.lang.Object
1. Load the database driver
2. Obtain a connection
3. Create and execute statements
4. Use result sets to navigate through the results
5. Close the connection
Code JDBC
import
SQL
java.sql.*; Utilities Driver DB
class useDB
{
} Connectivity
Driver Connection Statement ResultSet
Manager
SQL Data
Driver
Database
DriverManager
• Used to provide common access layer on the top of different database
drivers used in an application.
• The DriverManager class keeps track of the registered JDBC drivers
and creates database connections.
• DriverManager requires that each driver required by the application
must be registered before use, so that the DriverManager is aware of it
getConnection(“jdbc:odbc:myLib”)
Sybase Driver
Driver Manager
Oracle Driver
Connection
JDBC ODBC Driver
Driver Manager
• Application does not interacts directly with Driver object since
DriverManager is available.
• getConnection() iterates thru’ the drivers that are registered with the
DriverManager and asks each one in turn if it can handle the URL that
is passed.
• First driver that can satisfy the connection defined by the URL creates
a connection object that is passed back to the application by the way of
DriverManager
How to load Driver Manager
• Load the database driver using ClassLoader:
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("com.oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
/* Handle Exception */ }
• When a Driver class is loaded, it should create an instance of itself and register
it with the DriverManager.
• Class class: java.lang.Class- This method dynamically loads a class if it has
not already been loaded. The method returns a Class object that describes the
named class
forName
public static Class forName(String className) throws ClassNotFoundException
Connecting to a Database
• The Connection represents a single logical database transaction.
• The Connection Interface has methods for sending a series of SQL statements
to the database and managing the committing or aborting of those statements.
Type 1 JDBC Driver
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection
("jdbc:odbc:dsn");
}catch(SQLException e){
e.printStackTrace();
}
Contd..
Type 4 JDBC Driver
– Oracle Server
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:ite",
"scott", “tiger");
}catch(SQLException e){
e.printStackTrace();
}
Statement
• Statement interface provides workspace to create SQL query, execute
it and retrieve any results that are executed.