Professional Documents
Culture Documents
Chapter 5 AJP
Chapter 5 AJP
ODBC
• ODBC stands for Open Database
Connectivity
• It is a standard or open application
programming interface (API) for accessing a
database.
• ODBC provides an interface for database
access on Windows environment.
JDBC
• JDBC stands for Java Database Connectivity.
• It is a standard Java API for connecting
programs written in Java to the data in
relational databases.
• JDBC works with Java on a variety of
platforms, such as Windows, Mac OS, and
the various versions of UNIX.
JDBC Driver
• JDBC Driver is a software component that
enables java application to interact with the
database. There are 4 types of JDBC drivers:
⮚JDBC-ODBC bridge driver
⮚Native-API driver (partially java driver)
⮚JDBC-Net pure Java/ Network-Protocol driver (fully
java driver)
⮚Pure Java Driver /Thin driver / Database-Protocol
driver(fully java driver)
JDBC-ODBC bridge driver
• The JDBC type 1 driver, also known as the JDBC-ODBC
bridge driver.
• The JDBC-ODBC bridge driver uses ODBC driver to
connect to the database. The JDBC-ODBC bridge driver
converts JDBC method calls into the ODBC function calls.
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:
• easy to use.
• can be easily connected to any database.
Disadvantages:
• Performance degraded because JDBC method call is
converted into the ODBC function calls.
• The ODBC driver needs to be installed on the client
machine.
Native API driver
• The JDBC type 2 driver, also known as the 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:
• performance upgraded than JDBC-ODBC bridge
driver.
Disadvantage:
• The Native driver needs to be installed on the
each client machine.
• The Vendor client library needs to be installed on
client machine.
JDBC-Net pure Java Driver
• The JDBC type 3 driver, also known as the Pure Java driver for
database middleware. It 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 a vendor-specific database protocol. It is fully
written in java.
Advantage:
• No client side library is required because of
application server that can perform many tasks
like auditing, load balancing, logging etc.
Disadvantages:
• Network support is required on client machine.
• Requires database-specific coding to be done in
the middle tier.
• Maintenance of Network Protocol driver
becomes costly because it requires database-
specific coding to be done in the middle tier.
Thin driver
• 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.
• That is why it is known as thin driver. It is fully written in Java
language.
Advantage:
• Better performance than all other drivers.
• No software is required at client side or server
side.
Disadvantage:
• Drivers depend on the Database.
JDBC two tier model
• In a two-tier model, a Java application communicates
directly with the database, via the JDBC driver.
• 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.
JDBC three tier model
• In a three-tier model, a Java application
communicates with a middle tier component that
functions as an application server. The application
server talks to a given database using JDBC.
• 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.
Application Server?
An application server is a program that resides on the server-side, and it’s a server programmer providing
business logic behind any application. This server can be a part of the network or the distributed network.
Working
They are basically used in a web-based application that has 3 tier architecture. The position at which the
application server fits in is described below:
● Tier 1 – This is a GUI interface that resides at the client end and is usually a thin client (e.g. browser)
● Tier 2 – This is called the middle tier, which consists of the Application Server.
● Tier 3 – This is the 3rd tier which is backend servers. E.g., a Database Server.
As we can see, they usually communicate with the webserver for serving any request that is coming from
clients. The client first makes a request, which goes to the webserver. The web server then sends it to the
middle tier, i.e. the application server, which further gets the information from 3 rd tier (e.g. database server)
and sends it back to the webserver. The web server further sends back the required information to the client.
Different approaches are being utilized to process requests through the web servers, and some of them are
approaches like JSP (Java server pages), CGI(Common Gateway Interface), ASP (Active Server Pages), Java
Approach II - DriverManager.registerDriver()
The second approach you can use to register a driver, is to use the static
DriverManager.registerDriver() method. Ex.
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver( myDriver );
2. Creating connection
• The DriverManager.getConnection() method is used
to establish connection with the database.
con.close();
Example to Connect Java Application
with mysql database
import java.sql.*;
class MysqlCon{
public static void main(String args[])
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Emp","roo
t","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}
catch(Exception e) {
System.out.println(e); }
}
Connection with Oracle Database
PreparedStatement interface
The PreparedStatement interface is a subinterface of Statement. It is used to execute
parameterized query.
Let's see the example of parameterized query:
1. String sql="insert into emp values(?,?,?)";
As you can see, we are passing parameter (?) for the values. Its value will be set by
calling the setter methods of PreparedStatement.
Why use PreparedStatement?
Improves performance: The performance of the application will be faster if you use
PreparedStatement interface because query is compiled only once.
How to get the instance of PreparedStatement?
The prepareStatement() method of Connection interface is used to return the object of
PreparedStatement. Syntax:
1. public PreparedStatement prepareStatement(String query)throws
SQLException{}
Methods of PreparedStatement interface
The important methods of PreparedStatement interface are given below:
Method Description
public void setInt(int paramIndex, int sets the integer value to the given parameter
value) index.
public void setString(int paramIndex, sets the String value to the given parameter
String value) index.
public void setFloat(int paramIndex, sets the float value to the given parameter
float value) index.
public void setDouble(int sets the double value to the given parameter
paramIndex, double value) index.
public int executeUpdate() executes the query. It is used for create, drop,
insert, update, delete etc.
if (row > 0) {
System.out.println("A row has been inserted successfully.");
}
sql = "SELECT * FROM Contacts";
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
int id = result.getInt("Contact_ID");
String fullname = result.getString("Full_Name");
String email = result.getString("Email");
String phone = result.getString("Phone");
System.out.println(id + ", " + fullname + ", " + email + ", " + phone);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}