0% found this document useful (0 votes)
158 views

Fundamental Steps in JDBC - Using Java Database Connectivity (JDBC) With Oracle - InformIT

The document summarizes the fundamental steps in using JDBC to connect to an Oracle database and execute queries: 1. Import the necessary JDBC packages. 2. Load and register the JDBC driver to establish communication between the JDBC program and the Oracle database. 3. Open a connection to the database by calling the getConnection() method and passing the required parameters like the URL, username, and password. 4. Create a statement object to define the query and execute it, which returns a result set.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

Fundamental Steps in JDBC - Using Java Database Connectivity (JDBC) With Oracle - InformIT

The document summarizes the fundamental steps in using JDBC to connect to an Oracle database and execute queries: 1. Import the necessary JDBC packages. 2. Load and register the JDBC driver to establish communication between the JDBC program and the Oracle database. 3. Open a connection to the database by calling the getConnection() method and passing the required parameters like the URL, username, and password. 4. Create a statement object to define the query and execute it, which returns a result set.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT

Pearson

EVERYDAY DISCOUNT OFFER Buy 2 or more eligible tles and save 35%*—use code BUY2. Shop now.

books, eBooks, and digital learning

Home > Ar cles > Programming > Java

Related Resources
Using Java Database Connec vity (JDBC) with Oracle Store Ar cles Blogs
By Bulusu Lakshman
Apr 5, 2002 Core Java, Volume II--
Advanced Features, 11th
Edi on
📄 Contents ⎙ Print + Share This < Back Page 2 of 7 Next >
By Cay S. Horstmann
Book $51.99

This chapter is from the book


Hands-On Guide to Spring
Oracle and Java Development Cloud Contract LiveLessons:
Crea ng Consumer-Driven
Learn More  Buy Contracts to Leverage
Contract Tests and Improve
Your Code
By Marcin Grzejszczak
Online Video $239.99

Fundamental Steps in JDBC


The fundamental steps involved in the process of connec ng to a database and execu ng a query
Python for Programmers
consist of the following: By Harvey Deitel, Paul J. Deitel
Book $47.99
Import JDBC packages.

Load and register the JDBC driver.


See All Related Store Items
Open a connec on to the database.

Create a statement object to perform a query.

Execute the statement object and return a query resultset.

Process the resultset.

Close the resultset and statement objects.

Close the connec on.


This chapter is from the book
These steps are described in detail in the sec ons that follow.

Import JDBC Packages Oracle and Java Development

This is for making the JDBC API classes immediately available to the applica on program. The following
Learn More  Buy
import statement should be included in the program irrespec ve of the JDBC driver being used:

import java.sql.*;

www.informit.com/articles/article.aspx?p=26251&seqNum=3 1/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT
Addi onally, depending on the features being used, Oracle-supplied JDBC packages might need to be
imported. For example, the following packages might need to be imported while using the Oracle
extensions to JDBC such as using advanced data types such as BLOB, and so on.

import oracle.jdbc.driver.*;
import oracle.sql.*;

Load and Register the JDBC Driver


This is for establishing a communica on between the JDBC program and the Oracle database. This is
done by using the sta c registerDriver() method of the DriverManager class of the JDBC API.
The following line of code does this job:

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

JDBC Driver Registra on

For the en re Java applica on, the JDBC driver is registered only once per each database that needs
to be accessed. This is true even when there are mul ple database connec ons to the same data
server.

Alterna vely, the forName() method of the java.lang.Class class can be used to load and
register the JDBC driver:

Class.forName("oracle.jdbc.driver.OracleDriver");

However, the forName() method is valid for only JDK-compliant Java Virtual Machines and implicitly
creates an instance of the Oracle driver, whereas the registerDriver() method does this explicitly.

Connec ng to a Database
Once the required packages have been imported and the Oracle JDBC driver has been loaded and
registered, a database connec on must be established. This is done by using the getConnection()
method of the DriverManager class. A call to this method creates an object instance of the
java.sql.Connection class. The getConnection() requires three input parameters, namely, a
connect string, a username, and a password. The connect string should specify the JDBC driver to be
yes and the database instance to connect to.

The getConnection() method is an overloaded method that takes

Three parameters, one each for the URL, username, and password.

Only one parameter for the database URL. In this case, the URL contains the username and
password.

The following lines of code illustrate using the getConnection() method:

Connection conn = DriverManager.getConnection(URL, username, passwd);


Connection conn = DriverManager.getConnection(URL);

where URL, username, and passwd are of String data types.

We will discuss the methods of opening a connec on using the Oracle JDBC OCI and thin _drivers. This chapter is from the book
When using the OCI driver, the database can be specified using the TNSNAMES entry in the
tnsnames.ora file. For example, to connect to a database on a par cular host as user oratest and Oracle and Java Development
password oratest that has a TNSNAMES entry of oracle.world, use the following code:
Learn More  Buy

Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:


@oracle.world", "oratest", "oratest");

www.informit.com/articles/article.aspx?p=26251&seqNum=3 2/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT
Both the ":" and "@" are mandatory.

When using the JDBC thin driver, the TNSNAMES entry cannot be used to iden fy the database. There
are two ways of specifying the connect string in this case, namely,

Explicitly specifying the hostname, the TCP/IP port number, and the Oracle SID of the
database to connect to. This is for thin driver only.

Specify a Net8 keyword-value pair list.

For example, for the explicit method, use the following code to connect to a database on host
training where the TCP/IP listener is on port 1521, the SID for the database instance is Oracle, the
username and password are both oratest:

Connection conn = DriverManager.getConnection


("jdbc:oracle:thin:@training:1521:Oracle",
"oratest", "oratest");

For the Net8 keyword-value pair list, use the following:

Connection conn = DriverManager.getConnection


("jdbc:oracle:thin@(description=(address=
(host=training)(protocol=tcp)(port=1521))
(connect_data=(sid=Oracle))) ", _"oratest", "oratest");

This method can also be used for the JDBC OCI driver. Just specify oci8 instead of thin in the above
keyword-value pair list.

Querying the Database


Querying the database involves two steps: first, crea ng a statement object to perform a query, and
second, execu ng the query and returning a resultset.

Crea ng a Statement Object


This is to instan ate objects that run the query against the database connected to. This is done by the
createStatement() method of the conn Connection object created above. A call to this method
creates an object instance of the Statement class. The following line of code illustrates this:

Statement sql_stmt = conn.createStatement();

Execu ng the Query and Returning a ResultSet


Once a Statement object has been constructed, the next step is to execute the query. This is done by
using the executeQuery() method of the Statement object. A call to this method takes as
parameter a SQL SELECT statement and returns a JDBC ResultSet object. The following line of code
illustrates this using the sql_stmt object created above:

ResultSet rset = sql_stmt.executeQuery


("SELECT empno, ename, sal, deptno FROM emp ORDER BY ename");

Alterna vely, the SQL statement can be placed in a string and then this string passed to the
executeQuery() func on. This is shown below.
This chapter is from the book
String sql = "SELECT empno, ename, sal, deptno FROM emp ORDER BY ename";
Oracle and Java Development
ResultSet rset = sql_stmt.executeQuery(sql);

Learn More  Buy

Statement and ResultSet Objects

Statement and ResultSet objects open a corresponding cursor in the database for SELECT and other
DML statements.
www.informit.com/articles/article.aspx?p=26251&seqNum=3 3/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT
The above statement executes the SELECT statement specified in between the double quotes and
stores the resul ng rows in an instance of the ResultSet object named rset.

Processing the Results of a Database Query That Returns Mul ple Rows
Once the query has been executed, there are two steps to be carried out:

Processing the output resultset to fetch the rows

Retrieving the column values of the current row

The first step is done using the next() method of the ResultSet object. A call to next() is
executed in a loop to fetch the rows one row at a me, with each call to next() advancing the control
to the next available row. The next() method returns the Boolean value true while rows are s ll
available for fetching and returns false when all the rows have been fetched.

The second step is done by using the getXXX() methods of the JDBC rset object. Here getXXX()
corresponds to the getInt(), getString() etc with XXX being replaced by a Java datatype.

The following code demonstrates the above steps:

String str;
while (rset.next())
{
str = rset.getInt(1)+ " "+ rset.getString(2)+ "
"+rset.getFloat(3)+ " "rset.getInt(4)+ "\n";
}
byte buf[] = str.getBytes();
OutputStream fp = new FileOutputStream("query1.lst");
fp.write(buf);
fp.close();

Here the 1, 2, 3, and 4 in rset.getInt(), rset.getString(), getFloat(), and getInt()


respec vely denote the posi on of the columns in the SELECT statement, that is, the first column
empno, second column ename, third column sal, and fourth column deptno of the SELECT statement
respec vely.

Specifying get() Parameters

The parameters for the getXXX() methods can be specified by posi on of the corresponding
columns as numbers 1, 2, and so on, or by directly specifying the column names enclosed in double
quotes, as getString("ename") and so on, or a combina on of both.

Closing the ResultSet and Statement


Once the ResultSet and Statement objects have been used, they must be closed explicitly. This is
done by calls to the close() method of the ResultSet and Statement classes. The following code
illustrates this:

rset.close();
sql_stmt.close();

If not closed explicitly, there are two disadvantages:

Memory leaks can occur

Maximum Open cursors can be exceeded

Closing the ResultSet and Statement objects frees the corresponding cursor in the database.
This chapter is from the book
Closing the Connec on
The last step is to close the database connec on opened in the beginning a er impor ng the packages
Oracle and Java Development
and loading the JDBC drivers. This is done by a call to the close() method of the Connec on class.
Learn More  Buy
The following line of code does this:

conn.close();

www.informit.com/articles/article.aspx?p=26251&seqNum=3 4/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT
Explicitly Close your Connec on

Closing the ResultSet and Statement objects does not close the connec on. The connec on
should be closed by explicitly invoking the close() method of the Connection class.

A complete example of the above procedures using a JDBC thin driver is given below. This program
queries the emp table and writes the output rows to an opera ng system file.

//Import JDBC package


import java.sql.*;
// Import Java package for File I/O
import java.io.*;
public class QueryExample {
public static void main (String[] args) throws SQLException, IOException
{
//Load and register Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Establish a connection
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:
@training:1521:Oracle", "oratest", "oratest");
//Create a Statement object
Statement sql_stmt = conn.createStatement();
//Create a ResultSet object, execute the query and return a
// resultset
ResultSet rset = sql_stmt.executeQuery("SELECT empno, ename, sal,
deptno FROM emp ORDER BY ename");
//Process the resultset, retrieve data in each row, column by column
//and write to an operating system file
String str = "";
while (rset.next())
{
str += rset.getInt(1)+" "+ rset.getString(2)+" "+
rset.getFloat(3)+" "+rset.getInt(4)+"\n";
}
byte buf[] = str.getBytes();
OutputStream fp = new FileOutputStream("query1.lst");
fp.write(buf);
fp.close();
//Close the ResultSet and Statement
rset.close();
sql_stmt.close();
//Close the database connection
conn.close();
}
}

Processing the Results of a Database Query That Returns a Single Row


The above sec ons and the complete example explained the processing of a query that returned
mul ple rows. This sec on highlights the processing of a single-row query and explains how to write
code that is the analogue of the PL/SQL excep on NO_DATA_FOUND.

NO DATA FOUND Excep on

NO_DATA_FOUND excep on in PL/SQL is simulated in JDBC by using the return value of the next()
method of the ResultSet object. A value of false returned by the next() method iden fies a
NO_DATA_FOUND excep on.

Consider the following code (this uses the ResultSet object rset defined in the above sec ons):

if (rset.next())
// Process the row returned
else
System.out.println("The Employee with Empno "+ args[1] + This chapter is from the book
"does not exist");

Oracle and Java Development

Instead of the while loop used earlier, an if statement is used to determine whether the SELECT
Learn More  Buy
statement returned a row or not.

Datatype Mappings

www.informit.com/articles/article.aspx?p=26251&seqNum=3 5/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT
Corresponding to each SQL data type, there exist mappings to the corresponding JDBC Types, standard
Java types, and the Java types provided by Oracle extensions. These are required to be used in JDBC
programs that manipulate data and data structures based on these types.

There are four categories of Data types any of which can be mapped to the others. These are:

SQL Data types—These are Oracle SQL data types that exist in the database.

JDBC Typecodes—These are the data typecodes supported by JDBC as defined in the
java.sql.Types class or defined by Oracle in oracle.jdbc.driver.OracleTypes
class.

Java Types—These are the standard types defined in the Java language.

Oracle Extension Java Types—These are the Oracle extensions to the SQL data types and
are defined in the oracle.sql.* class. Mapping SQL data types to the oracle.sql.*
Java types enables storage and retrieval of SQL data without first conver ng into Java
format thus preven ng any loss of informa on.

Table 3.1 lists the default mappings exis ng between these four different types.

Table 3.1 Standard and Oracle-specific SQL-Java Data Type Mappings

SQL Data JDBC Type codes Standard Java Types Oracle Extension Java _
types Types

Standard JDBC 1.0 Types

CHAR java.sql.Types.CHAR java.lang.String oracle.sql.CHAR

VARCHAR2 java.sql.Types.VARCHAR java.lang.String oracle.sql.CHAR

LONG java.sql.Types. LONGVARCHAR java.lang.String oracle.sql.CHAR_

NUMBER java.sql.Types.NUMERIC java.math.BigDecimal oracle.sql.NUMBER

NUMBER java.sql.Types.DECIMAL java.math.BigDecimal oracle.sql.NUMBER

NUMBER java.sql.Types.BIT Boolean oracle.sql.NUMBER

NUMBER java.sql.Types.TINYINT byte oracle.sql.NUMBER

NUMBER java.sql.Types.SMALLINT short oracle.sql.NUMBER

NUMBER java.sql.Types.INTEGER int oracle.sql.NUMBER

NUMBER java.sql.Types.BIGINT long oracle.sql.NUMBER

NUMBER java.sql.Types.REAL float oracle.sql.NUMBER

NUMBER java.sql.Types.FLOAT double oracle.sql.NUMBER


This chapter is from the book
NUMBER java.sql.Types.DOUBLE double oracle.sql.NUMBER
Oracle and Java Development
RAW java.sql.Types.BINARY byte[] oracle.sql.RAW
Learn More  Buy

RAW java.sql.Types.VARBINARY byte[] oracle.sql.RAW

www.informit.com/articles/article.aspx?p=26251&seqNum=3 6/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT

LONGRAW java.sql.Types.LONGVARBINARY byte[] oracle.sql.RAW

DATE java.sql.Types.DATE java.sql.Date oracle.sql.DATE

DATE java.sql.Types.TIME java.sql.Time oracle.sql.DATE

DATE java.sql.Types.TIMESTAMP javal.sql.Timestamp oracle.sql.DATE

Standard JDBC 2.0 Types

BLOB java.sql.Types.BLOB java.sql.Blob Oracle.sql.BLOB

CLOB Java.sql.Types.CLOB java.sql.Clob oracle.sql.CLOB

user- java.sql.Types.STRUCT java.sql.Struct oracle.sql.STRUCT_object


defined

user- java.sql.Types.REF java.sql.Ref oracle.sql.REF_reference


defined

user- java.sql.Types.ARRAY java.sql.Array oracle.sql.ARRAY_collec on


defined

Oracle Extensions

BFILE oracle.jdbc.driver. n/a OracleTypes.BFILE

oracle.sql.BFILE_

ROWID oracle.jdbc.driver. n/a OracleTypes.ROWID


oracle.sql.ROWID_

REFCURSOR oracle.jdbc.driver. java.sql.ResultSet oracle.jdbc.driver._


type OracleTypes.CURSOR
OracleResultSet

Excep on Handling in JDBC


Like in PL/SQL programs, excep ons do occur in JDBC programs. No ce how the NO_DATA_FOUND
excep on was simulated in the earlier sec on "Processing the Results of a Database Query That
Returns a Single Row."

Excep ons in JDBC are usually of two types:

Excep ons occurring in the JDBC driver

Excep ons occurring in the Oracle 8i database itself

Just as PL/SQL provides for an implicit or explicit RAISE statement for an excep on, Oracle JDBC
programs have a throw statement that is used to inform that JDBC calls throw the SQL excep ons.This
This chapter is from the book
is shown below.

Oracle and Java Development

throws SQLException
Learn More  Buy

This creates instances of the class java.sql.SQLException or a subclass of it.

www.informit.com/articles/article.aspx?p=26251&seqNum=3 7/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT
And, like in PL/SQL, SQL excep ons in JDBC have to be handled explicitly. Similar to PL/SQL excep on
handling sec ons, Java provides a try..catch sec on that can handle all excep ons including SQL
excep ons. Handling an excep on can basically include retrieving the error code, error text, the SQL
state, and/or prin ng the error stack trace. The SQLException class provides methods for obtaining
all of this informa on in case of error condi ons.

Retrieving Error Code, Error Text, and SQL State


There are the methods getErrorCode() and getMessage() similar to the func ons SQLCODE and
SQLERRM in PL/SQL. To retrieve the SQL state, there is the method getSQLState(). A brief
descrip on of these methods is given below:

getErrorCode()

This func on returns the five-digit ORA number of the error in case of excep ons occurring
in the JDBC driver as well as in the database.

getMessage()

This func on returns the error message text in case of excep ons occurring in the JDBC
driver. For excep ons occurring in the database, this func on returns the error message
text prefixed with the ORA number.

getSQLState()

This func on returns the five digit code indica ng the SQL state only for excep ons
occurring in the database.

The following code illustrates the use of excep on handlers in JDBC:

try { <JDBC code> }


catch (SQLException e) { System.out.println("ERR: "+ e.getMessage())}

We now show the QueryExample class of the earlier sec on with complete excep on handlers built
in it. The code is as follows:

//Import JDBC package


import java.sql.*;
// Import Java package for File I/O
import java.io.*;
public class QueryExample {
public static void main (String[] args) {
int ret_code;
try {
//Load and register Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Establish a connection
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:
@training:1521:Oracle", "oratest", "oratest");
//Create a Statement object
Statement sql_stmt = conn.createStatement();
//Create a ResultSet object, execute the query and return a
// resultset
ResultSet rset = sql_stmt.executeQuery("SELECT empno, ename, sal,
deptno FROM emp ORDER BY ename");
//Process the resultset, retrieve data in each row, column by column
// and write to an operating system file
String str = "";
while (rset.next())
{
str += rset.getInt(1)+" "+ rset.getString(2)+" "+rset.getFloat(3)+
" "+rset.getInt(4)+"\n"; This chapter is from the book
}
byte buf[] = str.getBytes();
OutputStream fp = new FileOutputStream("query1.lst"); Oracle and Java Development
fp.write(buf);
fp.close();
Learn More  Buy
//Close the ResultSet and Statement
rset.close();
sql_stmt.close();
//Close the database connection
conn.close();
} catch (SQLException e) {ret_code = e.getErrorCode();
www.informit.com/articles/article.aspx?p=26251&seqNum=3 8/9
12/11/2019 Fundamental Steps in JDBC | Using Java Database Connectivity (JDBC) with Oracle | InformIT
System.err.println("Oracle Error: "+ ret_code + e.getMessage());}
catch (IOException e) {System.out.println("Java Error: "+
e.getMessage()); }
}
}

Prin ng Error Stack Trace


The SQLExcep on has the method printStackTrace() for prin ng an error stack trace. This
method prints the stack trace of the throwable object to the standard error stream.

The following code illustrates this:

catch (SQLException e) { e.printStackTrace(); }

+ Share This 🔖 Save To Your Account < Back Page 2 of 7 Next >

This chapter is from the book

Oracle and Java Development

Learn More  Buy

www.informit.com/articles/article.aspx?p=26251&seqNum=3 9/9

You might also like