Professional Documents
Culture Documents
Developed By:
Prosenjit Roy
Lecturer In Computer Science & Technology
APC Ray Polytechnic
188, Raja Subodh Chandra Mallick Rd, Jadavpur,
Kolkata - 700032
Java Programming : Unit 7
Contents
Subunit
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 2
Java Programming : Unit 7
Applications of JDBC:
Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows for portable
access to an underlying database. It is the duty of each JDBC driver to implement these interfaces
according to the DBMS it will be catering.
Java can be used to write different types of executables, such as −
Java Applications
Java Applets
Java Servlets
Java Server Pages (JSPs)
Enterprise Java Beans (EJBs).
All of these different executables are able to use the JDBC API to access a certain database through a
suitable JDBC driver catering the specific DBMS managing the database.
Some database vendors already have JDBC drivers (e.g. Oracle). For those databases that do not have a
JDBC driver, you need to install the database's ODBC driver and the JDBC to ODBC Bridge supplied
by JavaSoft. The JDBC to ODBC Bridge has the advantage of letting the Java application programmer
have access to almost any database since the ODBC drivers are readily available for most databases. It
has the disadvantage of requiring yet another layer between the database and the JDBC, although in most
cases the performance is acceptable.
Sun’s Java-based JDBC provides the same capabilities as its elder counterpart viz. Microsoft’s C-based
ODBC, allowing Java programs to access diverse databases with database-independent code.
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 3
Java Programming : Unit 7
Yet, in spite of having such alluring potential, when Sun Microsystems originally released JDK 1.0 in
early 1995, Java was hugely handicapped by the fact that it had no integrated support for accessing
databases, and hence was not able to become an industrial-strength programming language.
JavaSoft saw the prospect of chaos in the marketplace and responded with the first release of the Java
Database Connectivity (JDBC) specification in May, 1996. The JDBC specification, guided by Rick
Cattell and Graham Hamilton at JavaSoft, has structural and conceptual similarities with Microsoft's C-
based Open Database Connectivity driver, which Microsoft introduced in the 1980s to bring some
conformity to client access to databases.
But in those early days, there were very few JDBC drivers from any database vendor, as would be
expected with any new technology — but the ODBC ecosystem, itself then at only v 2.0, was going
strong.
Sun recognized that Java wouldn’t have as much uptake without a functional data access solution — so
they came up with a prompt solution in the form of the JDBC-ODBC Bridge driver viz.
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 4
Java Programming : Unit 7
sun.jdbc.odbc.JdbcOdbcDriver to allow JDBC clients to connect to the database via a suitable ODBC
driver that were readily available in abundance.
As Java has gained favour as the language of choice for Internet and intranet applications, the number of
choices for JDBC drivers has grown. DBMS manufacturers and third-party vendors are increasingly
encouraged to build JDBC drivers that conformed to Sun’s specifications. Those firms that build JDBC
drivers for their product can tap into the growing Java applications market.
This is how Java has evolved to become a much-coveted top-notch front-end in the database-market.
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 5
Java Programming : Unit 7
In a more technical sense, it is the server programme and the client programme themselves (in the
running state) that are meant by the terms server and client respectively - not the hardware devices on
which they run.
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 6
Java Programming : Unit 7
Database servers are actually the database management system (DBMS) software (like, e.g., Oracle,
MySQL, Db2 etc.) that interact with the data source i.e. the database. Database clients, on the other hand,
are application programmes written in Java, PHP etc., which generate SQL statements, and send them to
the server.
The pattern for interaction is as follows:
The Client issues forth a database query in the form of an SQL statement.
The Server returns a status code, and maybe some data.
Suppose you want your Java programme to load some data to a MySQL database.
Your programme sends an SQL statement that includes the INSERT statement with the data values to be
inserted.
The DBMS executes the SQL statement, and returns a status code, like ―OK.‖
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 7
Java Programming : Unit 7
DriverManager: This class manages a list of registered JDBC drivers as well as matches
connection requests from the java application with the proper JDBC driver using the
communication subprotocol supplied as a substring of the database URL that is passed as an
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 8
Java Programming : Unit 7
argument of one of its getConnection() method. The first driver that recognizes a certain
subprotocol under JDBC will be used to establish a database connection.
Driver: This interface handles the communications with the database server. One will interact
directly with Driver objects very rarely. Instead, you use DriverManager objects, which are able
to manage objects of Driver. This will abstract the details associated with the working with
Driver objects.
Connection: An object of Connection interface represents the connection session between
Java application and database. All that happens within the context of a connection – e.g. the
creation of SQL statements, the submission of them to the database, the returning of the results
by the database - take place through the connection object. By using the
createStatement()method of this interface one can obtain a Statement object that can then
be used to execute SQL statements. It also provides many methods for transaction management
like commit(), rollback() etc., and of course the close() method to terminate the
connection.
Statement: The objects created from this interface are used to submit the database query to the
database through its executeQuery(String s)/executeUpdate(String s)
method that contains the SQL statement as its argument. Some of its derived interfaces accept
parameters or execute stored procedures. This interface has the following two useful sub-
interfaces:
PreparedStatement: It is used to execute parameterized query. An SQL statement is
precompiled and stored in a PreparedStatement object using the
prepareStatement(String s) method of the Connection interface. This object
can then be used to efficiently execute this statement multiple times using first its
setXXX() methods, and then its executeQuery(void)/executeUpdate(void)
method.
CallableStatement: This sub-interface of PreparedStatement is used to execute an
SQL stored procedure which is a parameterized block of code and is identified by a unique
name. A stored procedure can be created by the following SQL command:
CREATE PROCEDURE <sp_name>(<parameter_list>)
IS
BEGIN
//statements;
END;
A stored procedure can be executed by the SQL command as follows:
EXECUTE <sp_name>(<parameter_list>);
An SQL statement containing the stored procedure call is first stored in a CallableStatement
object using the prepareCall(String s) method of the Connection interface. This
object can then be used efficiently to execute this statement with the assistance of its
setXXX(), getXXX()and executeQuery(void)/executeUpdate(void)
methods.
ResultSet: The objects of this interface hold tabular data retrieved from a database after you
execute an SQL query using Statement objects. It contains many getXXX() methods to extract
specific data from the table returned by the database. It contains a method called next()that
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 9
Java Programming : Unit 7
allows the Java application to move down the rows of the table that it holds, starting from the top
row.
SQLException: This class handles any errors that occur in a database application.
Time
Fig: Hierarchy of the JDBC API components as per the sequence of their usage
JDBC drivers are basically client-side adapters (i.e. a piece of software), installed on the client machine,
not on the server, that convert requests from Java programs to a protocol that the DBMS it is catering
can understand.
JDBC drivers implement the defined interfaces in the JDBC API, for interacting with their respective
database servers.
JDBC drivers enable Java Applcations to connect to the database, to interact with it by sending SQL
statements and then to retrieve the result returned by the database.
The Java.sql package that ships with JDK, contains various classes and interfaces with their general
behaviours outlined, and their actual implementaions are done in third-party 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
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 10
Java Programming : Unit 7
native code or in Java code. Another difference lies in how the driver makes the actual connection to the
database.
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 11
Java Programming : Unit 7
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 12
Java Programming : Unit 7
Middleware application server can provide additional supports like caching, load balancing,
logging, auditing etc.
The middleware provides additional advantages of security
Cons:
Maintenance of the middleware incurs additional costing
May add extra latency as it goes through middleware server
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 13
Java Programming : Unit 7
Connects directly to database drivers without going through any other layer
Cons:
Database dependent
Example: MySQL's Connector/J driver
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 14
Java Programming : Unit 7
It was, in fact, the first strategic step to beckon the database application developers to migrate
from the old platform-dependent C/C++ world to the new much-alluring platform-independent
world of Java.
The JDBC-to-ODBC Bridge driver has always been a narrow-gauge driver, as it was conceived as a
transitional solution, and from the very beginning Sun warned that users should use this driver only for
experimental prototyping, or when they have no other pure JDBC driver available. With so many pure
JDBC drivers having already made it to the market, that is a decent justification of why Sun, to be exact
Oracle, removed it in Java 8.
Though, we must not forget that in those formative days in the mid ′90s of the last century, when the
JDBC drivers were at a premium, the Bridge was quite helpful as it provided the JAVA DB application
developers with an easy, almost panacea-like option. So, for one last time, here is to the Bridge!
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 15
Java Programming : Unit 7
try
{
//STEP 2: Register JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println(rs.getString(“Name”));
}
//STEP 7: Terminate the connection
con.close();
}
catch(Exception e)
{}
} // End of main()
} // End of class
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 16
Java Programming : Unit 7
Output:
Students having marks > 70 are:
Sonali
Priya
Tapan
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 17
Java Programming : Unit 7
References:
Books:
1. J2EE the Complete Reference by Jim Keogh
Websites:
1. https://docs.oracle.com
2. https://www.javatpoint.com
3. https://www.geeksforgeeks.org
©Prosenjit Roy (Lecturer in Computer Sc. & Technology, APC Ray Polytechnic) Page 18