2. What is JDBC?
• JDBC is Java application programming interface
that allows the Java programmers to access
database management system from Java code.
• It was developed by JavaSoft, a subsidiary of Sun
Microsystems.
• It is a java API which enables the java programs
to execute SQL statements.
3. • JDBC is actually a Java application programming
interface provides a mechanism for dynamically
loading the correct Java packages and drivers.
• It also helps to register them with the JDBC
Driver Manager that is used as a connection
factory for creating JDBC connections which
supports creating and executing statements such
as SQL INSERT, UPDATE and DELETE.
• Driver Manager is the backbone of the jdbc
architecture
4. In short JDBC helps the programmers to write java
applications that manage these three programming
activities:
1. It helps us to connect to a data source, like a
database.
2. It helps us in sending queries and updating statements
to the database and
3. Retrieving and processing the results received from
the database in terms of answering to your query.
5. Fig: A driver that acts as an interface between a client and Database Server
Client JDBC Driver Database Server
SQL Request
SQL Result
7. Process
A Java program that uses the JDBC API loads the specified driver for a
particular DBMS before it actually connects to a database. The JDBC
DriverManager class then sends all JDBC API calls to the loaded
driver.
• When opening a connection to a database it is the DriverManager' s
role to choose the most appropriate driver from the previously
loaded drivers. (ie, from the system property jdbc. Drivers)
8. • Driver types are used to categorize the technology
used to connect to the database. A JDBC driver
vendor uses these types to describe how their
product operates.
• Some JDBC driver types are better suited for
some applications than others.
9. Types of JDBC drivers
There are four types of JDBC drivers known as:
• JDBC-ODBC bridge plus ODBC driver, also called
Type 1.
• Native-API, partly Java driver, also called Type 2.
• JDBC-Net, pure Java driver, also called Type 3.
• Native-protocol, pure Java driver, also called Type 4.
11. • It is a database driver implementation that employs
the ODBC driver to connect to the database. The driver
converts JDBC method calls into ODBC function calls.
• The driver is platform-dependent as it makes use of
ODBC which in turn depends on native libraries of the
underlying operating system the JVM is running upon.
• Type 1 drivers also don't support the complete Java
command set and are limited by the functionality of
the ODBC driver.
• This driver is not written entirely in java, the use of this
driver is discouraged if the alternative of a pure-Java
driver is available.
12. • Advantages:
Almost any database for which ODBC driver is installed,
can be accessed.
Disadvantages:
• Performance overhead since the calls have to go
through the JDBC overhead bridge to the ODBC driver,
then to the native database connectivity interface.
• The ODBC driver needs to be installed on the client
machine.
• Not suitable for applets, because the ODBC driver
needs to be installed on the client.
14. • It is a database driver implementation that
uses the client-side libraries of the database.
The driver converts JDBC method calls into
native calls (the language in which database API is written) of the
database API.
15. Advantages
• As there is no implementation of jdbc-odbc bridge, its
considerably faster than a type 1 driver.
Disadvantages
• The vendor client library needs to be installed on the
client machine.
• Not all databases have a client side library
• This driver is platform dependent
• This driver supports all java applications except Applets
17. • The JDBC type 3 driver, also known as the Pure
Java Driver for Database Middleware, is a
database driver implementation which makes
use of a middle tier between the calling
program and the database.
• The middle-tier (application server)
converts JDBC calls directly or indirectly into
the vendor-specific database protocol.
• The type 3 driver is written entirely in Java.
18. • The same driver can be used for multiple
databases.
• It depends on the number of databases the
middleware has been configured to support.
• The type 3 driver is platform-independent as the
platform-related differences are taken care of by
the middleware.
• Also, making use of the middleware provides
additional advantages of security and firewall
access.
19. • Functions
• Follows a three tier communication approach.
• Can interface to multiple databases - Not vendor
specific.
• The JDBC Client driver written in
java, communicates with a middleware-net-
server using a database independent
protocol, and then this net server translates this
request into database commands for that
database.
• Thus the client driver to middleware
communication is database independent.
20. Advantages
• Since the communication between client and the
middleware server is database independent, there is no
need for the database vendor library on the client. The
client need not be changed for a new database.
• The middleware server (which can be a full fledged J2EE
Application server) can provide typical middleware
services like caching (of connections, query results, etc.),
load balancing, logging, and auditing.
• A single driver can handle any database, provided the
middleware supports it.
21. Disadvantages
• Requires database-specific coding to be done
in the middle tier.
• The middleware layer added may result in
additional latency, but is typically overcome by
using better middleware services.
23. • The JDBC type 4 driver, also known as the Direct to
Database Pure Java Driver, is a database driver
implementation that converts JDBC calls directly into a
vendor-specific database protocol.
• Written completely in Java, type 4 drivers are thus platform
independent.
• They install inside the Java Virtual Machine of the client.
• This provides better performance than the type 1 and type
2 drivers as it does not have the overhead of conversion of
calls into ODBC or database API calls.
• Unlike the type 3 drivers, it does not need associated
software to work.
24. • Advantages
• Completely implemented in Java to achieve platform
independence.
• These drivers don't translate the requests into an
intermediary format (such as ODBC).
• The client application connects directly to the database
server. No translation or middleware layers are used,
improving performance.
• The JVM can manage all aspects of the application-to-
database connection; this can facilitate debugging.
25. • Disadvantages
Drivers are database dependent, as different
database vendors use wildly different (and
usually proprietary) network protocols.
26. CONNECTING TO A DATABASE
JDBC applications must perform two steps to
connect to a database:
• 1. Load the JDBC driver
• 2. Connect to the driver
27. STEP 1: Load the JDBC Driver Using Class.forName
• The Class.forName method takes as its argument the
fully-qualified class name for the JDBC Driver. If it finds
the class, the method loads and links the class, and
returns the Class object representing the class.
To load the JDBC Driver, use it as the argument to
Class.forName method:
// Load the driver
Class.forName("com.mysql.jdbc.Driver");
28. • We can also load the driver by
registerDriver(Driver driver) : It registers the
driver with the DriverManager class register
methods.then the DriverManager class will load
the needed.
29. • STEP 2: Connect to the JDBC Driver Using
DriverManager.getConnection
• To connect to a MYSQL database through the JDBC
Driver, an application specifies:
• A database connection string in the form of a JDBC URL
• User authentication detail (user name and password)
• Applications specify this information as arguments to the
DriverManager.getConnection method.
30. Java URL Connection String
• DriverManager.getConnection requires at least
one argument, a character string specifying a
database connection URL. The URL takes the
following form:
• Jdbc : mysql: host_name: db_name: port:
optional connection information
31. • Jdbc:mysql: An identifying protocol and subprotocol string for
the Dharma JDBC Driver.
• :host_name :Name of the server system where the database
resides.
• :db_name: Name of the database.
• :port :The port number associated with the JDBC server on
• the host system.
• :optional connection information:The optional connection
information component of the URL is optional. If it is
specified, the port information must also be specified in order
for the string to be correctly parsed.
Connection con = DriverManager.getConnection
("jdbc:mysql://172.16.18.25:3306/insurance","roll1","roll2");
32. Eg:Creating a Database in MySQL
import java.io.*;
import java.sql.*;
public class CreateDatabase{
public static void main(String[] args) {
System.out.println("Database creation example!");
Connection con = null;
try{
//loading a driver
Class.forName("com.mysql.jdbc.Driver");
//Establish connection
con = DriverManager.getConnection
("jdbc:mysql://172.16.18.25:3306/insurance","roll1","roll2");
33. • try{
//create statement //
Statement st = con.createStatement();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Database name:");
String database = bf.readLine();
//execute the query
st.executeUpdate("CREATE DATABASE "+database);
System.out.println("1 row(s) affected");
//Closing the connection
con.close();
st.close();
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}}
34. To Execute a query
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“Select * from
student”);
While(rs.next())
System.out.println(“Firstname=“+rs.getString(“first
name”));
35. To create and insert in database
Statement st=con.createStatement();
String stud =“create table student”+”(age
integer,name varchar(40),”+”weight integer)”;
St.executeUpdate(stud);
Insert
String sql = "INSERT INTO student" + "VALUES
(10, 'Zara', 50)";
st.executeUpdate(sql);