Java Database Connectivity: What Is JDBC?
Java Database Connectivity: 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
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.
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:
Disadvantages:
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.
Disadvantages
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...
Disadvantages
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.
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
while(rs.next())
{
String name=rs.getString("Emp_name");
int sal=rs.getInt("sal");
System.out.println(name+" : "+sal):
}