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.

Computer Science & Information Technology

You might also like to view...

A security administrator is tasked with conducting an assessment made to establish the baseline security posture of the corporate IT infrastructure. The assessment must report actual flaws and weaknesses in the infrastructure. Due to the expense of hiring outside consultants, the testing must be performed using in-house or cheaply available resource. There cannot be a possibility of any requirement being damaged in the test.Which of the following has the administrator been tasked to perform?

A. Risk transference B. Penetration test C. Threat assessment D. Vulnerability assessment

Computer Science & Information Technology

A programmer, or software developer, is a person with the training and skills necessary to ____________ computer programs.

a. design b. create c. test d. All of the above

Computer Science & Information Technology