State and briefly explain steps used in creating a JDBC application.
What will be an ideal response?
JDBC is an API (Application Programming Interface) for database access in Java. Using
the JDBC 3.0 API, you can access virtually any data source, from relational databases to
spreadsheets to flat files. JDBC technology also provides a common base on which tools
and alternate interfaces can be built. The JDBC 3.0 API is comprised of two packages:
java.sql package and javax.sql package
The JDBC-ODBC Bridge allows applications written in the Java programming language
to use the JDBC API with many existing ODBC drivers. The Bridge is itself a driver
based on JDBC technology ("JDBC driver") that is defined in the class
sun.jdbc.odbc.JdbcOdbcDriver. The Bridge defines the JDBC sub protocol of
ODBC. There are five steps in creating a JDBC application.
? Import JDBC classes or packages with JDBC classes
? Load JDBC drivers.
? Establish connection with the database.
? Execute SQL statements / interact with the database.
? Close connection.
Importing package or JDBC classes
In Java, all classes from java.lang package are readily available. All other packages and
their classes must be imported to make them available to the program. For example,
To import the entire java.sql package and all its classes, you would type:
import java.sql.*;
Loading JDBC Drivers
A Java program may load many JDBC drivers to connect to different database servers.
For example,
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
or Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connecting to the Oracle Database
The DriverManager class manages JDBC drivers. After a JDBC driver is loaded,
getConnection( ) classwide method of DriverManager class is used for establishing
connection to the database.
Connection conn =
DriverManager.getConnection("jdbc:odbc:shah_ora", userName, passWord);
Another example (using Oracle’s driver),
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@nshah-monroe:1521:oracle","nshah","india_usa");
When getConnection( ) method is called with a url, DriverManager locates a suitable
driver. If it does not find a suitable driver, it throws an exception. If it finds a suitable
driver, the Connection object (referenced by conn) is returned and connection is
established. The interaction with the database is possible through this connection object.
One Java application may have multiple connections with the same database, and it may
have connection with different databases.
Interacting with The Oracle database
The Connection object conn is used to interact with the database using SQL statements.
There are three classes for sending SQL statements to the server:
? Statement class for SQL statements without parameters
? PreparedStatement class for SQL statements with different parameters
? CallableStatement class for executing stored procedures
Statement class. This class is used for SQL statements without parameters., such as
SELECT, INSERT, DELETE, UPDATE or CREATE TABLE. First a Statement object
is created with Connection class’s instance method createStatement( ).
For example,
Statement stmt = conn.createStatement( );
Then , executeQuery( ) method is used with Statement object stmt for SELECT query,
and executeUpdate( ) method is used for INSERT, DELETE, UPDATE or CREATE
TABLE statement. For example,
int n = stmt.executeUpdate(query);
Closing connection
The JDBC session ends with closing database connection. Before disconnecting from
database, stmt.close( ) method is used to close ResultSet generated by that statement. At
last, conn.close( ) method is used to close connection and release JDBC resources.
You might also like to view...
When you assign a nonrecurring task, you cannot retain a copy, but can request a status report when the task is completed.
Answer the following statement true (T) or false (F)
A composite key is a primary key that is made up of one field
Indicate whether the statement is true or false
Storage pool capacity can be expanded at any time by adding _______disks
Fill in the blank(s) with correct word
How would you declare a named instance of a Font class, and what arguments can be used within the constructor? Show the declaration for a font named fancyFont that is size 24, bold, underlined, and Helvetica.
What will be an ideal response?