JDBC
JDBC
The classes and interfaces of JDBC API allow the application to send the
request to the specified database.
Purpose of JDBC
There are some enterprise applications created using the JAVA
EE(Enterprise Edition) technology. These applications need to interact
with databases to store application-specific information.
Applications of JDBC
JDBC is fundamentally a specification that provides a complete set of
interfaces. These interfaces allow for portable access to an underlying
database.
Java Applications
Java Applets
Enterprise JavaBeans (EJBs)
Java Servlets
Java ServerPages (JSPs)
All these different executables can use a JDBC driver to access a
database, and take advantage of the stored data. JDBC provides similar
capabilities as ODBC by allowing Java programs to contain database-
independent code.
There are mainly four main components of JDBC. These components help us to interact with
a database. The components of JDBC are as follows:
1. JDBC API: The JDBC API provides various classes, methods, and interfaces that are
helpful in easy communication with the database. It also provides two packages that contain
the Java SE(Standard Edition) and Java EE(Enterprise Edition) platforms to exhibit the
WORA(write once run everywhere) capabilities.
There is also a standard in JDBC API to connect a database to a client application.
2. JDBC Driver Manager: The Driver Manager of JDBC loads database-specific drivers in
an application. This driver manager establishes a connection with a database. It also makes a
database-specific call to the database so that it can process the user request.
3. JDBC Test suite: The Test Suite of JDBC helps to test the operation such as insertion,
deletion, updation that the JDBC Drivers perform.
4. JDBC-ODBC Bridge Drivers: The JDBC-ODBC Bridge Driver connects the database
drivers to the database. This bridge driver translates the JDBC method call to the ODBC
method call. It uses a package in which there is a native library to access ODBC
characteristics.
JDBC Driver
JDBC Driver is a software component that enables java application to interact with the
database. There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle
recommends that you use JDBC drivers provided by the vendor of your
database instead of the JDBC-ODBC Bridge.
Advantages:
o easy to use.
o can be easily connected to any database.
Disadvantages:
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts
JDBC method calls into native calls of the database API. It is not written entirely in java.
Advantage:
Disadvantage:
Advantage:
Disadvantages:
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol.
That is why it is known as thin driver. It is fully written in Java language.
Advantage:
Disadvantage:
Architecture of JDBC
The following figure shows the JDBC architecture:
Description of the Architecture:
1. Application: Application in JDBC is a Java applet or a Servlet that communicates with a data source.
2. JDBC API: JDBC API provides classes, methods, and interfaces that allow Java programs to execute
SQL statements and retrieve results from the database. Some important classes and interfaces defined
in JDBC API are as follows:
DriverManager
Driver
Connection
Statement
PreparedStatement
CallableStatement
ResultSet
SQL data
3. Driver Manager: The Driver Manager plays an important role in the JDBC architecture. The Driver
manager uses some database-specific drivers that effectively connect enterprise applications to
databases.
4. JDBC drivers: JDBC drivers help us to communicate with a data source through JDBC. We need a
JDBC driver that can intelligently interact with the respective data source.
Types of JDBC Architecture
There are two types of processing models in JDBC architecture: two-tier and three-tier. These models help us to
access a database. They are:
1. Two-tier model
In this model, a Java application directly communicates with the data source. JDBC driver provides
communication between the application and the data source. When a user sends a query to the data source, the
answers to those queries are given to the user in the form of results.
We can locate the data source on a different machine on a network to which a user is connected. This is called a
client/server configuration, in which the user machine acts as a client, and the machine with the data source acts
as a server.
2. Three-tier model
In the three-tier model, the query of the user queries goes to the middle-tier services. From the middle-tier
service, the commands again reach the data source. The results of the query go back to the middle tier.
From there, it finally goes to the user. This type of model is beneficial for management information system
directors.
JDBC Architecture
The JDBC API supports both two-tier and three-tier processing models for database access.
In the two-tier model, a Java applet or application talks directly to the data source. This
requires a JDBC driver that can communicate with the particular data source being accessed.
A user's commands are delivered to the database or other data source, and the results of those
statements are sent back to the user. The data source may be located on another machine to
which the user is connected via a network. This is referred to as a client/server configuration,
with the user's machine as the client, and the machine housing the data source as the server.
The network can be an intranet, which, for example, connects employees within a
corporation, or it can be the Internet.
In the three-tier model, commands are sent to a "middle tier" of services, which then sends
the commands to the data source. The data source processes the commands and sends the
results back to the middle tier, which then sends them to the user. MIS directors find the
three-tier model very attractive because the middle tier makes it possible to maintain control
over access and the kinds of updates that can be made to corporate data. Another advantage is
that it simplifies the deployment of applications. Finally, in many cases, the three-tier
architecture can provide performance advantages.
Figure 2: Three-tier Architecture for Data Access.
Until recently, the middle tier has often been written in languages such as C or C++, which offer fast
performance. However, with the introduction of optimizing compilers that translate Java bytecode into efficient
machine-specific code and technologies such as Enterprise JavaBeans™, the Java platform is fast becoming the
standard platform for middle-tier development. This is a big plus, making it possible to take advantage of Java's
robustness, multithreading, and security features.
With enterprises increasingly using the Java programming language for writing server code, the JDBC API is
being used more and more in the middle tier of a three-tier architecture. Some of the features that make JDBC a
server technology are its support for connection pooling, distributed transactions, and disconnected rowsets. The
JDBC API is also what allows access to a data source from a Java middle tier.
1. Class.forName("oracle.jdbc.driver.OracleDriver");
1. Connection con=DriverManager.getConnection(
2. "jdbc:oracle:thin:@localhost:1521:xe","system","password");
1. Statement stmt=con.createStatement();
By closing connection object statement and ResultSet will be closed automatically. The
close() method of Connection interface is used to close the connection.
1. con.close();
Create a Table
For example,
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","student");
Statement stmt=con.createStatement();
con.close();
}
}
The above example will fetch all the records of emp table.
To connect java application with the Oracle database ojdbc14.jar file is required to be loaded.
now,
Now execute the java program…This program will now display all
the content of the table we created in oracle database.
ResultSet interface
The object of ResultSet maintains a cursor pointing to a row of a table.
Initially, cursor points to before the first row.
By default, ResultSet object can be moved forward only and it is not updatable.
But we can make this object to move forward and backward direction by
passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in
createStatement(int,int) method as well as we can make this object as
updatable by:
1) public boolean next(): is used to move the cursor to the one row next from
the current position.
2) public boolean is used to move the cursor to the one row previous
previous(): from the current position.
3) public boolean first(): is used to move the cursor to the first row in result
set object.
4) public boolean last(): is used to move the cursor to the last row in result
set object.
7) public int getInt(int is used to return the data of specified column index
columnIndex): of the current row as int.
8) public int getInt(String is used to return the data of specified column name
columnName): of the current row as int.
10) public String is used to return the data of specified column name
getString(String of the current row as String.
columnName):
1. import java.sql.*;
2. class FetchRecord{
3. public static void main(String args[])throws Exception{
4.
5. Class.forName("oracle.jdbc.driver.OracleDriver");
6. Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhos
t:1521:xe","system","oracle");
7. Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SEN
SITIVE,ResultSet.CONCUR_UPDATABLE);
8. ResultSet rs=stmt.executeQuery("select * from emp765");
9.
10.//getting the record of 3rd row
11. rs.absolute(3);
12.System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)
);
13.
14.con.close();
15. }}
1. import java.sql.*;
2. class Dbmd{
3. public static void main(String args[]){
4. try{
5. Class.forName("oracle.jdbc.driver.OracleDriver");
6.
7. Connection con=DriverManager.getConnection(
8. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
9. DatabaseMetaData dbmd=con.getMetaData();
10.
11. System.out.println("Driver Name: "+dbmd.getDriverName());
12.System.out.println("Driver Version: "+dbmd.getDriverVersion());
13. System.out.println("UserName: "+dbmd.getUserName());
14.System.out.println("Database Product Name: "+dbmd.getDatabaseProduct
Name());
15. System.out.println("Database Product Version: "+dbmd.getDa
tabaseProductVersion());
16.
17. con.close();
18.}catch(Exception e){ System.out.println(e);}
19. }
20.}
Output:Driver Name: Oracle JDBC Driver
Driver Version: 10.2.0.1.0XE
Database Product Name: Oracle
Database Product Version: Oracle Database 10g Express Edition
Release 10.2.0.1.0 -Production
1. import java.sql.*;
2. class Dbmd2{
3. public static void main(String args[]){
4. try{
5. Class.forName("oracle.jdbc.driver.OracleDriver");
6.
7. Connection con=DriverManager.getConnection(
8. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
9.
10.DatabaseMetaData dbmd=con.getMetaData();
11. String table[]={"TABLE"};
12.ResultSet rs=dbmd.getTables(null,null,null,table);
13.
14.while(rs.next()){
15. System.out.println(rs.getString(3));
16.}
17.
18.con.close();
19.
20.}catch(Exception e){ System.out.println(e);}
21.
22.}
23. }
1. import java.sql.*;
2. class Dbmd3{
3. public static void main(String args[]){
4. try{
5. Class.forName("oracle.jdbc.driver.OracleDriver");
6.
7. Connection con=DriverManager.getConnection(
8. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
9.
10.DatabaseMetaData dbmd=con.getMetaData();
11. String table[]={"VIEW"};
12.ResultSet rs=dbmd.getTables(null,null,null,table);
13.
14.while(rs.next()){
15. System.out.println(rs.getString(3));
16.}
17.
18.con.close();
19.
20.}catch(Exception e){ System.out.println(e);}
21.
22.}
23. }