AJP Chapter 1 JDBC Connectivity
AJP Chapter 1 JDBC Connectivity
Software Architectures
The two tiers are often called as Application layer includes JDBC
drivers, business logic and user interfaces whereas second layer i.e.
Database layer consists of RDBMS server.
Advantages:
• It is simple in design.
• Client-side scripting offloads work onto the
client Drawbacks:
• Fat client.
• It is inflexible.
What is JDBC?
The JDBC stands for Java Database Connectivity. What is this JDBC
besides a nifty acronym? It refers to several things, depending on
context:
• It’s a specification for using data sources in Java applets and
applications.
• It’s an API for using low-level JDBC drivers.
• It’s an API for creating the low-level JDBC drivers, which do the
actual connecting/transacting with data sources.
• It’s based on the X/Open SQL Call Level Interface (CLI) that defines
how client/server interactions are implemented for database
systems.
The JDBC defines every aspect of making data-aware Java
applications and applets. The low-level JDBC drivers perform the
database-specific translation to the high-level JDBC interface. This
interface is used by the developer so he doesn’t need to worry about the
database-specific syntax when connecting to and querying different
databases. The JDBC is a package, much like other Java packages such as
java.awt. It’s not currently a part of the standard Java Developer’s Kit
(JDK) distribution, but it is slated to be included as a standard part of the
general Java API as the java.sql package. Soon after its official
incorporation into the JDK and Java API, it will also become a standard
package in Java-enabled Web browsers, though there is no definite
timeframe for this inclusion. The exciting aspect of the JDBC is that the
drivers necessary for connection to their respective databases do not
require any pre-installation on the clients: A JDBC driver can be
downloaded along with an applet!
The JDBC project was started in January of 1996, and the
specification was frozen in June of 1996. Javasoft sought the input of
industry database vendors so that the JDBC would be as widely accepted
as possible when it was ready for release. And, as we can see from this
list of vendors who have already endorsed the JDBC, it’s sure to be widely
accepted by the software industry:
• Borland International, Inc.
• Bulletproof
• Cyber SQL Corporation
• DataRamp
• Dharma Systems, Inc.
JDBC drivers
Sun has defined four categories of JDBC drivers. The categories
delineate the differences in architecture for the drivers. One difference
between architectures lies in whether a given driver is implemented in
native code or in Java code. Native code means whatever machine code is
supported by a particular hardware configuration. For example, a driver
may be written in C and then compiled to run on a specific hardware
platform. Another difference lies in how the driver makes the actual
connection to the database. The four driver types are as follows:
This type of driver wraps a native API with Java classes. The Oracle
Call Interface (OCI) driver is an example of a Type 2 driver. Because a
Type 2 driver is implemented using local native code, it is expected to
have better performance than a pure Java driver. These drivers enable
JDBC programs to use database-specific APIs (normally written in C or
C++) that allow client programs to access databases via the Java Native
Interface. This driver type translates JDBC into database-specific code.
Type 2 drivers were introduced for reasons similar to the Type 1 ODBC
bridge driver.
These drivers take JDBC requests and translate them into a network
protocol that is not database specific. These requests are sent to a server,
Driver
Connection
PreparedStatement Statement
CallableStatement
7. Type DSN name and provide the required information such as user
name and password for the database (.mdb files) of Microsoft
Access Database etc. and click on OK button.
8. Our DSN name will get appeared in user data sources.
There are six different steps to use JDBC in our Java application
program.
These can be shown diagrammatically as below:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection DriverManager.getConncetion(String
url, String userID, String
password);
<protocol>:<subprotocol>:<dsn-name>
The ‘protocol’ is a JDBC protocol that is used to read the URL. The
‘subprotocol’ is JDBC driver name and ‘dsn-name’ is the name of the
database that we provided while creating JDBC Bridge though control
panel. We use the following URL for our application:
jdbc:odbc:customer
Conncetion con;
“pitch”);
Statement st = con.createStatement();
Here, the ‘customer’ is neither database name nor DSN name but it
is a table name.
The ResultSet object is assigned the results received from the DBMS
after the query is processed. The ResultSet object consists of methods
used to interact with data that is returned by the DBMS to Java
application program. For example, the next() method is used to proceed
throughout the result set. It returns true, if the data is available in result
set to read.
The ResultSet also contains several getXxx( ) methods to read the
value from particular column of current row. For example,
getString(“name”) will read the value from column ‘name’ in the form of
string. Instead of passing column name as parameter, we can pass
column as parameter also. Such as, getString(1). For example:
String
name;
int
age;
do
{
name =
rs.getString(“name”);
age = rs.getInt(“age”);
System.out.println(name+“=”+
age); } while(rs.next());
con.close();
Statement Objects
Once the connection to the database is opened, the Java application
creates and sends a query to access data contained in the database. One
of three type of statement objects is used to execute the query
immediatelt. A PreparedStatement is used to execute the compiled query
and CallableStetement is used to execute the stored procedure.
1. executeQuery()
2. executeUpdate()
For example:
For example:
if(st.execute())
rs = st.getResultSet();
public ResultSet
getResultSet() public int
getUpdateCount() public
boolean getMoreResults()
PreparedStatement object
ps.setInt(1, 100000);
ResultSet
executeQuery( ) int
executeUpdate( )
boolean execute( )
Example:
Consider the following database:
Output:
Students having marks > 70 are:
Rakhee
Rahul
Karthik
CallableStatement Object
CREATE PROCEDURE
sp_interest (id IN
INTEGER,
bal IN OUT
UPDATE account
SET balance = bal
WHERE account_id = id;
END;