100% found this document useful (1 vote)
87 views

Java Database Connectivity: What Is JDBC?

JDBC is an API that allows Java programs to connect to databases. There are 4 types of JDBC drivers: 1) Type 1 drivers use ODBC to translate JDBC calls to database calls. They are slow and not suitable for high transactions. 2) Type 2 drivers use a native client library specific to the database. They are faster than Type 1 but not fully platform independent. 3) Type 3 drivers use a middleware tier between Java and the database for communication. They are fully Java-based and platform independent. 4) Type 4 drivers communicate directly with a database server using a network protocol. They have the best performance and are fully Java-based and platform independent
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
87 views

Java Database Connectivity: What Is JDBC?

JDBC is an API that allows Java programs to connect to databases. There are 4 types of JDBC drivers: 1) Type 1 drivers use ODBC to translate JDBC calls to database calls. They are slow and not suitable for high transactions. 2) Type 2 drivers use a native client library specific to the database. They are faster than Type 1 but not fully platform independent. 3) Type 3 drivers use a middleware tier between Java and the database for communication. They are fully Java-based and platform independent. 4) Type 4 drivers communicate directly with a database server using a network protocol. They have the best performance and are fully Java-based and platform independent
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

CHAPTER 13 JAVA DATABASE CONNECTIVITY 1

13.1 What is JDBC?

i) JDBC is an API (Application Programming Interface) which consists of a set of java classes, interfaces and Exception.
ii) With the help of JDBC programming interface java programmer can request a connection with database, then send the
query statement using SQL and receive the results for processing.
iii) JDBC supports 2 packages and 4 types of Driver and these are:
Packages:
a) Java.sql.*;
b) Javax.sql.*;
Drivers:
a) Type 1: JDBC-ODBC bridge driver
b) Type 2: Native-API driver
c) Type 3: Net-Protocol driver
d) Type 4: Native-Protocol driver
13.2 JDBC API
The JDBC API is the industry standard for database-independent connectivity between the Java programming language and
a wide range of databases.
The JDBC API makes it possible to do three things:
 Establish a connection with a database or access any tabular data source
 Send SQL statements

 Process the results

The following table describes the interfaces, classes, and exceptions (classes thrown as exceptions) that make up the JDBC API.
package are extensions to the standard JDBC interfaces and
In the table, interfaces belonging to the javax.sql
are contained in the Java 2 SDK, Enterprise Edition.

Interface/class/exception Description
Interfaces:
Interface used to establish a connection to a database. SQL
1. java.sql.Connection
statements run within the context of a connection.
2. java.sql.DatabaseMetaData Interface used to return information about the database.
Interface used to locate the driver for a particular database
3. java.sql.Driver
management system.
Interface used to send precompiled SQL statements to the database
4. java.sql.PreparedStatement
server and obtain results.
Interface used to process the results returned from executing an
5. java.sql.ResultSet
SQL statement.
Interface used to return information about the columns in a
6. java.sql.ResultSetMetaData
ResultSet object.
Interface used to send static SQL statements to the database
7. java.sql.Statement
server and obtain results.
8. javax.sql.ConnectionEventListener Receives events that a PooledConnection object generates.
Factory for PooledConnection objects. A ConnectionPoolDataSource
9. javax.sql.ConnectionPoolDataSource
object is usually registered with a JNDI service.
A factory for Connection objects. A DataSource object is usually
10 javax.sql.DataSource
registered with a JNDI service provider.
A PooledConnection object represents a physical connection to a
11. javax.sql.PooledConnection
data source.
Classes:
12. java.sql.Date Subclass of java.util.Date used for the SQL DATE data type.
13. java.lang.DriverManager Class used to manage a set of JDBC drivers.
14. java.sql.DriverPropertyInfo Class used to discover and supply properties to a connection.
15. java.sql.Time Subclass of java.util.Date used for the SQL TIME data type.
16. java.sql.TimeStamp Subclass of java.util.Date used for the SQL TIMESTAMP data type.
Class used to define constants that are used to identify standard
17. java.sql.Types
SQL data types, such as CHAR, INTEGER, and DECIMAL.
18. java.sql.String Class used to identify text data types such as CHAR.
Exception classes:
19. java.sql.SQLException Exception that provides information about a database error.
20. java.sql.SQLWarning Exception that provides information about a database warning.

By: Biswaranjan Mohapatra


CHAPTER 13 JAVA DATABASE CONNECTIVITY 2

13.3 JDBC Drivers


A JDBC driver is a software component enabling a Java application to interact with a database.
To connect with individual databases, JDBC (the Java Database Connectivity API) requires drivers for each database. The
JDBC driver gives out the connection to the database and implements the protocol for transferring the query and result
between client and database.

There are 4 types of JDBC drivers known as:

1. Type1: JDBC-ODBC bridge driver

2. Native-API partly java technology-enabled driver

3. Net-protocol fully java technology-enabled driver

4. Native-protocol fully java technology-enabled driver

13.3.1 Type-1: JDBC-ODBC bridge driver

i) The Type-1 driver is a JDBC-ODBC bridge plus an ODBC (Open Data Base Connectivity) driver and delegates the
work of data access to ODBC API.

ii) The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver.

iii) The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

iv) The Steps are : Client -> JDBC Driver -> ODBC Driver -> Database

Advantages:

 Almost any database, for which ODBC driver is


installed, can be accessed.

 A type 1 driver is easy to install.

Disadvantages:

 It is not applicable for a high transaction


environment, because it is slowest of all.
 It does not support all the feathers of java.

13.3.2 Type-2: Native-API partly java technology-enabled driver

i) It is the drivers where there is a java application which connects to JDBC-API and then it connects to
database server where it finds a vender specific language/native language and the job of that language
is to convert the query in to database understandable language.

ii) Type-2 drivers force developers to write platform-specific code because type-2 drivers do not use the
full java API.

Advantages  Better performance than Type 1 Driver


(JDBC-ODBC Bridge).

By: Biswaranjan Mohapatra


CHAPTER 13 JAVA DATABASE CONNECTIVITY 3

 Provides Fastest performance than all 3


drivers as it calls native APIs
(MySQL,Oracle...etc).

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

By: Biswaranjan Mohapatra


CHAPTER 13 JAVA DATABASE CONNECTIVITY 4

13.3.3 Type-3: Net-protocol fully java technology-enabled driver

1. 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.

2. The middle-tier (application server) converts JDBC calls directly or indirectly into the vendor-
specific database protocol.

3. Follows a three tier communication approach and can interface to multiple databases - Not vendor
specific.

4. Functionality: Client -> JDBC Driver -> Middleware-Net Server -> Any Database...

Advantages  Requires database-specific coding to be done


in the middle tier.
 Since the communication between client and  An extra layer added may result in a time-
the middleware server is database bottleneck. But typically this is overcome by
independent, there is no need for the vendor providing efficient middleware services.
db library on the client machine.
 The Middleware Server (which can be a full
fledged J2EE Application server) can provide
typical middleware services like caching
(connections, query results, and so on), load
balancing, logging, auditing etc.

 Eg. for the above include jdbc driver features


in Weblogic.

o Can be used in internet since there


is no client side software needed.

o At client side a single driver can


handle any database. (It works
provided the middleware supports
that database!)

Disadvantages

13.3.4 Type-4: Native-protocol fully java technology-enabled driver

1. 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.
2. Type 4 drivers, coded entirely in Java, communicate directly with a vendor's database, usually
through socket connections. No translation or middleware layers are required, improving
performance.

3. Completely implemented in Java to achieve platform independence.

4. This type includes (for example) the widely-used Oracle thin driver -
oracle.jdbc.driver.OracleDriver which connects using a format configuration of
jdbc:oracle:thin:@URL

5. Functionality: Client -> Native-protocol JDBC Driver -> database server

Advantages  The JVM can manage all aspects of the


application-to-database connection; this can
facilitate debugging.
 These drivers don't translate the requests into
an intermediary format (such as ODBC), nor
do they need a middleware layer to service Disadvantages
requests. This can enhance performance
considerably.
 Clients require a separate driver for each
database.

By: Biswaranjan Mohapatra


CHAPTER 13 JAVA DATABASE CONNECTIVITY 5

 Drivers are database dependent.

By: Biswaranjan Mohapatra


CHAPTER 13 JAVA DATABASE CONNECTIVITY 6

13.4 Steps to connect the program to the database

Step1: Import the concern package:


Import java.sql.*;
Step2: Load the Driver:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Step3: Make the Connection:
Connection con=DriverManager.getConnection("jdbc:odbc:DNS Name","userid","password");

Step4: Create jdbc statement


Statement st=con.createStatement();

Step5: Execute the statement


ResultSet rs=st.executeQuery("select * from employee");

Step6: Navigating through the result set

while(rs.next())
{
String name=rs.getString("Emp_name");
int sal=rs.getInt("sal");
System.out.println(name+" : "+sal):
}

Step7: close the connection


con.close();
st.close();
rs.close();
Example

By: Biswaranjan Mohapatra


Type1 Driver Example:
Type 4 Driver Example:
import java.sql.*;
public class JDBC import java.sql.*;
{ public class JDBC_type4
public static void main(String args[]) throws Exception {
{ public static void main(String args[]) throws Exception
try {
{ try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); {
Connectioncon=DriverManager.getConnection("jdbc:odbc:biswa Class.forName("oracle.jdbc.driver.OracleDriver");
", Connection
"scott","tiger"); con=DriverManager.getConnection("jdbc:oracle:thin:@
localhost: 1521: XE","scott","tiger");
Statement st=con.createStatement();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from employee");
ResultSet rs=st.executeQuery("select * from employee");
while(rs.next())
while(rs.next())
{
{
String name=rs.getString("Emp_name");
String name=rs.getString("Emp_name");
int sal=rs.getInt("sal");
int sal=rs.getInt("sal");
System.out.println(name+" : "+sal):
System.out.println(name+" : "+sal):
}
}
}
}
catch(SQLException e)
catch(SQLException e)
{
{
System.out.println(e.getMessage());
System.out.println(e.getMessage());
}
}
}
}
}
}
--X--

You might also like