SlideShare a Scribd company logo
Oracle® Database
2 Day + Java Developer’s Guide
12c Release 1 (12.1)
E17944-10
February 2014
Oracle Database 2 Day + Java Developer's Guide, 12c Release 1 (12.1)
E17944-10
Copyright © 2007, 2014, Oracle and/or its affiliates. All rights reserved.
Primary Authors: Tulika Das, Maitreyee Chaliha, Deepa Aswani, Rosslynne Hefferan
Contributing Authors: Kathleen Heap, Simon Law
Contributor: The Oracle Database 12c documentation is dedicated to Mark Townsend, who was an
inspiration to all who worked on this release.
Contributors: Kuassi Mensah, Chris Schalk, Christian Bauwens, Mark Townsend, Paul Lo,
Venkatasubramaniam Iyer, Yuri Dolgov
This software and related documentation are provided under a license agreement containing restrictions on
use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your
license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license,
transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse
engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is
prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If
you find any errors, please report them to us in writing.
If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it
on behalf of the U.S. Government, the following notice is applicable:
U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software,
any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users
are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and
agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and
adaptation of the programs, including any operating system, integrated software, any programs installed on
the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to
the programs. No other rights are granted to the U.S. Government.
This software or hardware is developed for general use in a variety of information management
applications. It is not developed or intended for use in any inherently dangerous applications, including
applications that may create a risk of personal injury. If you use this software or hardware in dangerous
applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other
measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages
caused by use of this software or hardware in dangerous applications.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks
are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD,
Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced
Micro Devices. UNIX is a registered trademark of The Open Group.
This software or hardware and documentation may provide access to or information on content, products,
and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly
disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle
Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your
access to or use of third-party content, products, or services.
iii
Contents
Preface............................................................................................................................................................... xiii
Audience..................................................................................................................................................... xiii
Documentation Accessibility................................................................................................................... xiii
Related Documents ................................................................................................................................... xiii
Conventions ............................................................................................................................................... xiv
1 Using Java with Oracle Database
Using Java to Connect to Oracle Database 12c Release 1 (12.1)....................................................... 1-1
Oracle JDBC Thin Driver................................................................................................................... 1-2
Oracle JDBC Packages ....................................................................................................................... 1-2
Using JDeveloper to Create JDBC Applications................................................................................ 1-2
JDeveloper User Interface ................................................................................................................. 1-3
JDeveloper Tools ................................................................................................................................ 1-4
Overview of Sample Java Application................................................................................................. 1-4
Resources ................................................................................................................................................... 1-6
2 Getting Started with the Application
What You Need to Install........................................................................................................................ 2-1
Oracle Database 12c Release 1 (12.1) ............................................................................................... 2-1
Unlocking the HR Schema for the JDBC Application ........................................................... 2-1
J2SE or JDK.......................................................................................................................................... 2-2
Integrated Development Environment........................................................................................... 2-2
Web Server .......................................................................................................................................... 2-3
Verifying the Oracle Database 12c Release 1 (12.1) Installation..................................................... 2-3
Checking Installed Directories and Files ........................................................................................ 2-3
Checking the Environment Variables ............................................................................................. 2-4
Determining the JDBC Driver Version............................................................................................ 2-4
Installing Oracle JDeveloper ................................................................................................................. 2-5
3 Connecting to Oracle Database 12c Release 1 (12.1)
Connecting to Oracle Database from JDeveloper.............................................................................. 3-1
JDeveloper Database Navigator....................................................................................................... 3-1
Creating a Database Connection...................................................................................................... 3-2
Browsing the Data Using the Database Navigator ....................................................................... 3-3
Setting Up Applications and Projects in JDeveloper........................................................................ 3-4
iv
Using the JDeveloper Application Navigator................................................................................ 3-5
Creating an Application and a Project ............................................................................................ 3-5
Viewing the Javadoc and Source Code Available in the Project Scope .................................... 3-5
Connecting to Oracle Database from a Java Application................................................................. 3-6
Overview of Connecting to Oracle Database................................................................................. 3-6
Specifying Database URLs................................................................................................................ 3-7
Using the Default Service Feature of the Oracle Database ................................................... 3-8
Creating a Java Class in JDeveloper................................................................................................ 3-9
Java Libraries ................................................................................................................................... 3-10
Overview of the Oracle JDBC Library .................................................................................. 3-10
Overview of the JSP Runtime Library .................................................................................. 3-10
Adding JDBC and JSP Libraries.................................................................................................... 3-10
Importing JDBC Packages.............................................................................................................. 3-11
Declaring Connection-Related Variables..................................................................................... 3-11
Creating the Connection Method ................................................................................................. 3-12
4 Querying for and Displaying Data
Overview of Querying for Data in Oracle Database......................................................................... 4-1
SQL Statements................................................................................................................................... 4-1
Query Methods for the Statement Object ....................................................................................... 4-2
Result Sets ........................................................................................................................................... 4-2
Features of ResultSet Objects .................................................................................................... 4-3
Summary of Result Set Object Types ....................................................................................... 4-3
Querying Data from a Java Application .............................................................................................. 4-4
Creating a Method in JDeveloper to Query Data.......................................................................... 4-4
Testing the Connection and the Query Methods .......................................................................... 4-5
Creating JSP Pages ................................................................................................................................... 4-7
Overview of Page Presentation........................................................................................................ 4-7
JSP Tags ........................................................................................................................................ 4-8
Scriptlets....................................................................................................................................... 4-8
HTML Tags.................................................................................................................................. 4-8
HTML Forms ............................................................................................................................... 4-8
Creating a Simple JSP Page............................................................................................................... 4-9
Adding Static Content to a JSP Page ............................................................................................... 4-9
Adding a Style Sheet to a JSP Page............................................................................................... 4-11
Adding Dynamic Content to the JSP Page: Database Query Results ......................................... 4-13
Adding a JSP useBean Tag to Initialize the DataHandler Class............................................... 4-13
Creating a Result Set....................................................................................................................... 4-14
Adding a Table to the JSP Page to Display the Result Set......................................................... 4-16
Filtering a Query Result Set................................................................................................................ 4-18
Creating a Java Method for Filtering Results............................................................................. 4-18
Testing the Query Filter Method .................................................................................................. 4-19
Adding Filter Controls to the JSP Page........................................................................................ 4-20
Displaying Filtered Data in the JSP Page..................................................................................... 4-21
Adding Login Functionality to the Application.............................................................................. 4-22
Creating a Method to Authenticate Users................................................................................... 4-22
Creating a Login Page .................................................................................................................... 4-24
v
Preparing Error Reports for Failed Logins.................................................................................. 4-25
Creating the Login Interface.......................................................................................................... 4-25
Creating a JSP Page to Handle Login Action.............................................................................. 4-26
Testing the JSP Page ............................................................................................................................. 4-27
5 Updating Data
Creating a JavaBean ................................................................................................................................. 5-1
Creating a JavaBean in JDeveloper.................................................................................................. 5-1
Defining the JavaBean Properties and Methods............................................................................ 5-2
Updating Data from a Java Class .......................................................................................................... 5-4
Creating a Method to Identify an Employee Record .................................................................... 5-5
Creating a Method to Update Employee Data............................................................................... 5-5
Adding a Link to Navigate to an Update Page.............................................................................. 5-8
Creating a JSP Page to Edit Employee Data................................................................................ 5-10
Creating a JSP Page to Handle an Update Action...................................................................... 5-11
Inserting an Employee Record............................................................................................................ 5-12
Creating a Method to Insert Data ................................................................................................. 5-12
Adding a Link to Navigate to an Insert Page.............................................................................. 5-14
Creating a JSP Page to Enter New Data....................................................................................... 5-14
Creating a JSP Page to Handle an Insert Action......................................................................... 5-16
Deleting an Employee Record ............................................................................................................ 5-17
Creating a Method for Deleting Data........................................................................................... 5-17
Adding a Link to Delete an Employee......................................................................................... 5-18
Creating a JSP Page to Handle a Delete Action.......................................................................... 5-19
Exception Handling .............................................................................................................................. 5-19
Adding Exception Handling to Java Methods............................................................................ 5-20
Creating a Method for Handling Any SQLException ............................................................... 5-21
Navigation in the Sample Application ............................................................................................. 5-21
Creating a Starting Page for an Application ............................................................................... 5-22
6 Enhancing the Application: Advanced JDBC Features
Using Dynamic SQL ................................................................................................................................ 6-1
Using OraclePreparedStatement...................................................................................................... 6-1
Using OracleCallableStatement ....................................................................................................... 6-2
Using Bind Variables ......................................................................................................................... 6-2
Calling Stored Procedures ...................................................................................................................... 6-3
Creating a PL/SQL Stored Procedure in JDeveloper ................................................................... 6-4
Creating a Method to Use the Stored Procedure........................................................................... 6-5
Enabling Users to Choose the Stored Procedure........................................................................... 6-6
Calling the Stored Procedure from the Application ..................................................................... 6-8
Using Cursor Variables........................................................................................................................... 6-9
Oracle REF CURSOR Type Category........................................................................................... 6-10
Accessing REF CURSOR Data....................................................................................................... 6-10
Using REF CURSOR in the Sample Application ........................................................................ 6-11
Creating a Package in the Database ...................................................................................... 6-11
Creating a Database Function ................................................................................................ 6-11
vi
Calling the REF CURSOR from a Method............................................................................ 6-12
Displaying a Dynamically Generated List ........................................................................... 6-13
7 Getting Unconnected from Oracle Database 12c Release 1 (12.1)
Creating a Method to Close All Open Objects................................................................................... 7-1
Closing Open Objects in the Application ........................................................................................... 7-2
8 Creating a Universal Connection Pool
Setting JDeveloper Project Properties.................................................................................................. 8-1
Creating the Stock Ticker Application................................................................................................. 8-3
Observing the Output ............................................................................................................................. 8-6
9 Building Global Applications
Developing Locale Awareness............................................................................................................... 9-1
Mapping Between Oracle and Java Locales................................................................................... 9-2
Determining User Locales ...................................................................................................................... 9-3
Locale Awareness in Java Applications.......................................................................................... 9-3
Encoding HTML Pages............................................................................................................................ 9-3
Specifying the Page Encoding for HTML Pages............................................................................ 9-4
Specifying the Page Encoding in Java Servlets and JSP Pages .................................................... 9-4
Organizing the Content of HTML Pages for Translation ................................................................ 9-5
Strings in Java Servlets and JSP Pages ............................................................................................ 9-5
Static Files............................................................................................................................................ 9-6
Data from the Database..................................................................................................................... 9-6
Presenting Data by User Locale Convention ...................................................................................... 9-6
Oracle Date Formats .......................................................................................................................... 9-7
Oracle Number Formats.................................................................................................................... 9-7
Oracle Linguistic Sorts....................................................................................................................... 9-8
Oracle Error Messages....................................................................................................................... 9-9
Localizing Text on JSP Pages in JDeveloper....................................................................................... 9-9
Creating a Resource Bundle .......................................................................................................... 9-10
Using Resource Bundle Text on JSP Pages.................................................................................. 9-11
Index
vii
viii
List of Examples
2–1 Determining the JDBC Driver Version .................................................................................... 2-4
3–1 Specifying the url Property for the DataSource Object ......................................................... 3-8
3–2 Default Service Configuration in listener.ora ........................................................................ 3-8
3–3 Importing Packages in a Java Application........................................................................... 3-11
3–4 Declaring Connection Variables and the Connection Object ............................................ 3-12
3–5 Adding a Method to Connect to the Database .................................................................... 3-13
4–1 Creating a Statement Object ...................................................................................................... 4-2
4–2 Declaring a Scroll-Sensitive, Read-Only ResultSet Object.................................................... 4-3
4–3 Using the Connection, Statement, Query, and ResultSet Objects........................................ 4-5
4–4 Implementing User Validation .............................................................................................. 4-23
5–1 Skeleton Code for a Basic Java Bean with Accessor Methods.............................................. 5-3
5–2 Method for Updating a Database Record................................................................................ 5-7
5–3 Method for Adding a New Employee Record..................................................................... 5-13
5–4 Method for Deleting an Employee Record........................................................................... 5-18
5–5 Adding a Method to Handle Any SQLException in the Application .............................. 5-21
6–1 Creating a PreparedStatement .................................................................................................. 6-2
6–2 Creating a CallableStatement.................................................................................................... 6-2
6–3 Calling Stored Procedures ......................................................................................................... 6-3
6–4 Creating a Stored Function........................................................................................................ 6-3
6–5 Calling a Stored Function in Java ............................................................................................. 6-3
6–6 Creating a PL/SQL Stored Procedure to Insert Employee Data.......................................... 6-4
6–7 Using PL/SQL Stored Procedures in Java .............................................................................. 6-6
6–8 Declaring a REF CURSOR Type ............................................................................................ 6-10
6–9 Accessing REF Cursor Data in Java....................................................................................... 6-10
6–10 Creating a Package in the Database ...................................................................................... 6-11
6–11 Creating a Stored Function..................................................................................................... 6-12
7–1 Creating a Method to Close All Open Objects........................................................................ 7-2
9–1 Mapping from a Java Locale to an Oracle Language and Territory.................................... 9-2
9–2 Determining User Locale in Java Using the Accept-Language Header.............................. 9-3
9–3 Explicitly Specifying User Locale in Java ................................................................................ 9-3
9–4 Specifying Page Encoding in the HTTP Specification ........................................................... 9-4
9–5 Specifying Page Encoding on an HTML Page........................................................................ 9-4
9–6 Specifying Page Encoding in Servlets Using setContentType ............................................. 9-5
9–7 Difference Between Date Formats by Locale (United States and Germany)...................... 9-7
9–8 Difference Between Number Formats by Locale (United States and Germany)............... 9-8
9–9 Variations in Linguistic Sorting (Binary and Spanish).......................................................... 9-8
9–10 Creating a Resource Bundle Class......................................................................................... 9-11
ix
List of Figures
1–1 JDeveloper User Interface.......................................................................................................... 1-3
1–2 Web Pages in the Sample Application..................................................................................... 1-5
3–1 Specifying Connection Details .................................................................................................. 3-3
3–2 Viewing the Table Structure and Data..................................................................................... 3-4
3–3 Selecting the Class to View the Javadoc in JDeveloper......................................................... 3-6
3–4 Creating a Java Class .................................................................................................................. 3-9
3–5 Java Source Editor.................................................................................................................... 3-10
3–6 Importing Libraries.................................................................................................................. 3-11
3–7 Java Code Insight..................................................................................................................... 3-13
4–1 Test Output for Query Method in Log Window.................................................................... 4-7
4–2 Creating a JSP Page..................................................................................................................... 4-9
4–3 Adding Content to JSP Pages in the JDeveloper Visual Source Editor............................ 4-10
4–4 Formatting a JSP....................................................................................................................... 4-11
4–5 Selecting a CSS File for the JSP............................................................................................... 4-12
4–6 Adding Static Content to the JSP Page ................................................................................. 4-12
4–7 useBean Representation in the employees.jsp File.............................................................. 4-13
4–8 Preferences Window to Select JSP and HTML Visual Editor Options............................. 4-14
4–9 Scriptlet Representation in a JSP Page .................................................................................. 4-15
4–10 Viewing Errors in the Structure Window............................................................................. 4-15
4–11 Importing Packages in JDeveloper........................................................................................ 4-16
4–12 Common HTML Components in the Component Palette................................................. 4-17
4–13 Table in a JSP Page................................................................................................................... 4-18
4–14 HTML Form Components in the JSP Page........................................................................... 4-21
4–15 Using the Scriptlet Properties Dialog Box............................................................................ 4-22
4–16 Project CSS Files Panel ............................................................................................................ 4-24
4–17 Login Page................................................................................................................................. 4-26
4–18 Login Page for Sample Application in the Browser............................................................ 4-28
4–19 Unfiltered Employee Data in employee.jsp ......................................................................... 4-29
4–20 Filtered Employee Data in employee.jsp.............................................................................. 4-29
5–1 Creating a Java Bean................................................................................................................... 5-2
5–2 Generate Accessors Dialog Box ................................................................................................ 5-3
5–3 Link to Edit Employees in employees.jsp................................................................................ 5-9
5–4 Creating a JSP Page to Edit Employee Details..................................................................... 5-11
5–5 Editing Employee Data ........................................................................................................... 5-12
5–6 Form to Insert Employee Data ............................................................................................... 5-15
5–7 Inserting New Employee Data............................................................................................... 5-16
5–8 Inserting Employee Data ........................................................................................................ 5-17
5–9 Link for Deleting an Employee from employees.jsp .......................................................... 5-19
6–1 Adding a Link to Provide the Stored Procedure Option ...................................................... 6-8
6–2 Using Stored Procedures to Enter Records ............................................................................. 6-9
6–3 Structure View of ListBox Options........................................................................................ 6-14
6–4 Dynamically Generated List in Browser............................................................................... 6-15
8–1 Setting the Default Project Properties...................................................................................... 8-2
8–2 Setting the Libraries and Classpath.......................................................................................... 8-2
8–3 The Add Archive or Directory Screen ..................................................................................... 8-3
8–4 The Application Navigator Panel Options.............................................................................. 8-3
8–5 The DbConfig.properties File in the Code Editor .................................................................. 8-4
8–6 Adding a Source File to a Project.............................................................................................. 8-4
8–7 The Add to Project Content....................................................................................................... 8-4
8–8 The Run Menu Options.............................................................................................................. 8-5
8–9 The Project Properties Screen for the StockTickerProj Project ............................................. 8-5
8–10 The Edit Run Configuration Screen ......................................................................................... 8-6
8–11 Output of the Stock Ticker Application in JDeveloper Log.................................................. 8-6
8–12 Retrieving Information from the Universal Connection Pool Using the Browser ............ 8-7
x
8–13 The UCP Properties page........................................................................................................... 8-7
8–14 The Stock Ticker Page on the Browser..................................................................................... 8-8
8–15 The UCP Statistics Page on the Browser.................................................................................. 8-8
8–16 The UCP Properties Update Page on the Browser................................................................. 8-9
8–17 The Client Simulator Page on the Browser ............................................................................. 8-9
xi
xii
List of Tables
2–1 Directories and Files in the ORACLE_HOME Directory..................................................... 2-3
3–1 Standard Data Source Properties............................................................................................. 3-7
4–1 Key Query Execution Methods for java.sql.Statement......................................................... 4-2
9–1 Locale Representation in Java, SQL, and PL/SQL Programming Environments............ 9-2
xiii
Preface
This Preface introduces you to Oracle Database 2 Day + Java Developer's Guide, by
discussing the intended audience and conventions of this document. It also includes a
list of related Oracle documents that you can refer to for more information.
Audience
This guide is intended for application developers using Java to access and modify data
in Oracle Database. This guide illustrates how to perform these tasks using a simple
Java Database Connectivity (JDBC) application. This guide uses the Oracle JDeveloper
integrated development environment (IDE) to create the application. This guide can be
read by anyone with an interest in Java programming, but it assumes at least some
prior knowledge of the following:
■ Java
■ Oracle PL/SQL
■ Oracle databases
Documentation Accessibility
For information about Oracle's commitment to accessibility, visit the Oracle
Accessibility Program website at
http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc.
Access to Oracle Support
Oracle customers have access to electronic support through My Oracle Support. For
information, visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=info or
visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=trs if you are hearing
impaired.
Related Documents
For more information, see the following documents in the Oracle Database
documentation set:
■ Oracle® Fusion Middleware Installation Guide for Oracle JDeveloper, 11g Release 1
(11.1.1) and JDeveloper Online Documentation on Oracle Technology Network at
http://www.oracle.com/technology/documentation/jdev.html
■ Oracle Database JDBC Developer's Guide
■ Oracle Database Java Developer's Guide
xiv
■ Oracle Universal Connection Pool for JDBC Developer's Guide
Conventions
The following text conventions are used in this document:
Convention Meaning
boldface Boldface type indicates graphical user interface elements associated
with an action, or terms defined in text or the glossary.
italic Italic type indicates book titles, emphasis, or placeholder variables for
which you supply particular values.
monospace Monospace type indicates commands within a paragraph, URLs, code
in examples, text that appears on the screen, or text that you enter.
1
Using Java with Oracle Database 1-1
1 Using Java with Oracle Database
Oracle Database is a relational database that you can use to store, use, and modify
data. The Java Database Connectivity (JDBC) standard is used by Java applications to
access and manipulate data in relational databases.
JDBC is an industry-standard application programming interface (API) that lets you
access a RDBMS using SQL from Java. JDBC is based on the X/Open SQL Call Level
Interface (CLI) and complies with the Entry Level of the JDBC escape standard. Each
vendor implements the JDBC Specification with its own extensions.
This guide shows you how to use a simple Java application to connect to Oracle
Database 12c Release 1 (12.1) and access and modify data within the database.
This chapter introduces you to the Java application created in this guide, and to the
tools you can use to develop the Java application in the following topics:
■ Using Java to Connect to Oracle Database 12c Release 1 (12.1)
■ Using JDeveloper to Create JDBC Applications
■ Overview of Sample Java Application
Using Java to Connect to Oracle Database 12c Release 1 (12.1)
JDBC is a database access protocol that enables you connect to a database and run SQL
statements and queries on the database. The core Java class libraries provide the JDBC
APIs, java.sql and javax.sql. However, JDBC is designed to allow vendors to
supply drivers that offer the necessary specialization for a particular database.
The following sections describe Oracle support for the JDBC standard:
■ Oracle JDBC Thin Driver
■ Oracle JDBC Packages
See Also:
http://www.oracle.com/technetwork/java/overview-141217.html
Note: Oracle Database 12c Release 1 (12.1) supports JDK 6 and JDK
7.
Using JDeveloper to Create JDBC Applications
1-2 Oracle Database 2 Day + Java Developer's Guide
Oracle JDBC Thin Driver
Oracle recommends using the JDBC Thin Driver for most requirements. JDBC-OCI is
only needed for OCI-specific features. The Thin driver will work on any system that
has a suitable Java virtual machine (JVM).
The JDBC Thin Driver is a pure Java, Type IV driver. It supports the JavaTM
2 Platform
Standard Edition 5.0, also known as Java Development Kit (JDK) 5. It also includes
support for JDK 6. It is platform-independent and does not require any additional
Oracle software for client-side application development. The JDBC Thin Driver
communicates with the server using SQL*Net to access Oracle Database 12c Release 1
(12.1).
You can access the Oracle-specific JDBC features and the standard features by using
the oracle.jdbc package.
Oracle JDBC Packages
Oracle support for the JDBC API is provided through the oracle.jdbc and
oracle.sql packages. These packages support all Java Development Kit (JDK) releases
from 1.5 through 1.6.
oracle.sql
The oracle.sql package supports direct access to data in SQL format. This package
consists primarily of classes that provide Java mappings to SQL data types and their
support classes. Essentially, the classes act as Java wrappers for SQL data. The
characters are converted to Java chars and, then, to bytes in the UCS-2 character set.
Each of the oracle.sql.* data type classes extends oracle.sql.Datum, a superclass
that includes functions and features common to all the data types. Some of the classes
are for JDBC 2.0-compliant data types. In addition to data type classes, the oracle.sql
package supports classes and interfaces for use with objects and collections.
oracle.jdbc
The interfaces of the oracle.jdbc package define the Oracle extensions to the
interfaces in the java.sql package. These extensions provide access to Oracle
SQL-format data. They also provide access to other Oracle-specific features, including
Oracle performance enhancements.
The key classes and interfaces of this package provide methods that support standard
JDBC features and perform tasks such as:
■ Returning Oracle statement objects
■ Setting Oracle performance extensions for any statement
■ Binding oracle.sql.* types into prepared and callable statements
■ Retrieving data in oracle.sql format
■ Getting meta information about the database and result sets
■ Defining integer constants used to identify SQL types
Using JDeveloper to Create JDBC Applications
The Java application tutorial in this guide uses Oracle JDeveloper release 11.1.1 as the
integrated development environment (IDE) for developing the Java application and
creating Web pages for users to view and change the data.
Using JDeveloper to Create JDBC Applications
Using Java with Oracle Database 1-3
Oracle JDeveloper is an IDE with support for modeling, developing, debugging,
optimizing, and deploying Java applications and Web services.
JDeveloper provides features for you to write and test Java programs that access the
database with SQL statements embedded in Java programs. For the database,
JDeveloper provides functions and features to do the following:
■ Create a connection to a database
■ Browse database objects
■ Create, edit, or delete database objects
■ Create and edit PL/SQL functions, procedures, and packages
JDeveloper User Interface
Oracle JDeveloper is an IDE that uses windows for various application development
tools. You can display or hide any of the windows, and you can dock them or undock
them to create a desktop suited to your method of working.
In addition to these tools, JDeveloper provides a range of navigators to help you
organize and view the contents of your projects. Application and System navigators
show you the files in your projects, and a Structure window shows you the structure of
individual items.
You can arrange the windows as you choose, and can close and open them from the
View menu. Figure 1–1 shows the default layout of some of the available navigators,
palettes, and work areas in the JDeveloper user interface (GUI).
Figure 1–1 JDeveloper User Interface
See Also: Working with Windows in the IDE, in the JDeveloper online
Help
Java Source Editor/
Visual Editor
Application
Navigator
Connections
Navigator
Component
Palette
Property
Inspector
Runtime
Messages
Structure
Window
Overview of Sample Java Application
1-4 Oracle Database 2 Day + Java Developer's Guide
JDeveloper Tools
For creating a Java application, JDeveloper provides the following tools to simplify the
process:
■ Structure window, which provides a tree view of all of the elements in the
application currently being edited be it Java, XML, or JSP/HTML.
■ Java Visual Editor, which you can use to assemble the elements of a user interface
quickly and easily.
■ JSP/HTML Visual Editor, which you can use to visually edit HTML and JSP
pages.
■ Java Source Editor, which provides extensive features for helping in writing the
Java code, such as distinctive highlighting for syntax and semantic errors,
assistance for adding and sorting import statements, the Java Code Insight feature,
and code templates.
■ Component Palette, from which you select the user interface components, such as
buttons and text areas, that you want to display on your pages.
■ Property Inspector, which gives a simple way of setting properties of items such
as user interface components.
Refer to Figure 1–1 to get a better idea of these tools.
Overview of Sample Java Application
This guide shows you how to create an application using Java, JDBC and Oracle ADF.
In this application, you build in functions and features that:
1. Allow users to log in and validate the user name and password.
2. Establish a connection to the database.
3. Query the database for data and retrieve the data using a JavaBean.
4. Display the data using JavaServer Pages (JSP) technology.
5. Allow users to insert, update, or delete records.
6. Access and modify information from a master-detail application.
7. Handle exceptions.
Overview of Application Web Pages (JSP Pages)
Figure 1–2 shows the relationships among the pages developed for this application.
Note: The Java Code Insight feature is a facility that provides
context-specific, intelligent input when creating code in the Java
Source Editor. In this guide, you will see many instances of how you
can use Java Code Insight to insert code.
Note: The application connects to the HR schema that ships with
Oracle Database 12c Release 1 (12.1).
Overview of Sample Java Application
Using Java with Oracle Database 1-5
Figure 1–2 Web Pages in the Sample Application
A brief description of the Web pages in the sample application follows:
■ index.jsp
This is the starting page of the application. It automatically forwards the user to
the login page of the application, login.jsp.
■ login.jsp
This page allows users to log in to the application. The user name, password, and
host information are validated and used to create the connection descriptor to log
in to the database.
■ login_action.jsp
This is a nonviewable page that handles the authentication of the user-supplied
login details from login.jsp. If authentication is successful, the page forwards the
user to employees.jsp. Otherwise, it redisplays the login.jsp page including a
message.
■ employees.jsp
This is the main page of the application. It displays a list of all the employees in
the HR schema for AnyCo Corporation and allows the user to filter the list of
employees using any string. It also includes links to add, edit, and delete any user
data. These actions, however, are handled by other JSP pages that are created
specifically for each of these tasks.
■ insert.jsp
The link to insert employee data on the employees.jsp page redirects the user to
this page. This includes a form that accepts all the details for a new employee
record. The details entered on this form are processed by the insert_action.jsp
page.
■ insert_action.jsp
This is a nonviewable page that handles the insertion of data for a new employee
that is entered on the insert.jsp page.
■ edit.jsp
employees.jsp
Displays a table of employees.
Contains :
• a field for filtering the list of
employees
• a link to add an employee
• links to edit employee rows
• links to delete employee rows
index.jsp
login.jsp login_action.jsp
insert.jsp insert_action.jsp
edit_emp.jsp update_action.jsp
delete_action.jsp
Resources
1-6 Oracle Database 2 Day + Java Developer's Guide
The link to edit employee data on the employees.jsp page redirects the user to
this page. This form displays current data of a single employee in text fields, and
the user can edit this information.
■ update_action.jsp
The submit action on the edit.jsp page directs the data to this nonviewable page,
which inserts the edited data into the database.
■ delete_action.jsp
The link to delete an employee record on the employees.jsp page is handled by
this nonviewable page, which deletes the employee data and forwards the user
back to the employees.jsp page.
Classes
The sample application includes the following classes:
■ DataHandler.java
This class contains all the methods that are used to implement the important
functions of the sample application. It includes methods that validate user
credentials, connect to the database, retrieve employee data with and without
filters, insert data, update data, handle exceptions, and so on.
■ Employees.java
This class is a JavaBean that holds a single employee record. It contains accessor
methods to get and set the values of each of the record fields. It also contains
accessor methods to retrieve and modify employee records.
■ JavaClient.java
This class is used only for testing the DataHandler class.
Resources
For more information about Oracle Database 12c Release 1:
■ Visit the Oracle Technology Network page
http://www.oracle.com/technetwork/index.html
■ Visit the Oracle Database 12c Release 1 Online Documentation Library by
performing the following steps:
– Click Start and then Programs.
– Select Oracle Database 12c Release 1, then Get Help, and then Read
Documentation.
■ Visit the Oracle Database 12c Release 1 Online Discussion forum by performing
the following steps:
– Click Start and then Programs.
– Select Oracle Database 12c Release 1, then Get Help, and then Go To Online
Forum.
Note: This application is developed throughout this guide in the
form of a tutorial. It is recommended, therefore, that you read these
chapters in sequence.
2
Getting Started with the Application 2-1
2 Getting Started with the Application
To develop a Java application that connects to Oracle Database 12c Release 1 (12.1),
you must ensure that certain components are installed as required. This chapter covers
the following topics:
■ What You Need to Install
■ Verifying the Oracle Database 12c Release 1 (12.1) Installation
■ Installing Oracle JDeveloper
What You Need to Install
To be able to develop the sample application, you need to install the following
products and components:
■ Oracle Database 12c Release 1 (12.1)
■ J2SE or JDK
■ Integrated Development Environment
■ Web Server
The following subsections describe these requirements in detail.
Oracle Database 12c Release 1 (12.1)
To develop the Java application, you need a working installation of Oracle Database
12c Release 1 (12.1) Server with the HR schema, which comes with the database. The
installation creates an instance of Oracle Database 12c Release 1 (12.1) and provides
additional tools for managing this database. For more information, refer to the
following Oracle Database 12c Release 1 (12.1) installation guides and release notes:
■ Oracle Database Installation Guide for Linux
■ Oracle Database Installation Guide for Microsoft Windows
Unlocking the HR Schema for the JDBC Application
The HR user account, which owns the sample HR schema used for the Java application
in this guide, is initially locked. You must log in as a user with administrative
privileges (SYS) and unlock the account before you can log in as HR.
If the database is locally installed, use the Run SQL Command Line to unlock the
account as follows:
What You Need to Install
2-2 Oracle Database 2 Day + Java Developer's Guide
1. To access the Run SQL Command Line, from the Start menu, select Programs (or
All Programs), then Oracle Database 12c Release 1 (12.1), and then click Run SQL
Command Line. Log in as a user with DBA privileges, for example:
> CONNECT SYS AS SYSDBA;
Enter password: password
2. Run the following command:
> ALTER USER HR ACCOUNT UNLOCK;
or,
> ALTER USER HR IDENTIFIED BY HR;
3. Test the connection as follows:
> CONNECT HR
Enter password: password
You should see a message indicating that you have connected to the database.
In addition, some of the constraints and triggers present in the HR schema are not in
line with the scope of the Java application created in this guide. You must remove
these constraints and triggers as follows using the following SQL statements:
DROP TRIGGER HR.UPDATE_JOB_HISTORY;
DROP TRIGGER HR.SECURE_EMPLOYEES;
DELETE FROM JOB_HISTORY;
J2SE or JDK
To create and compile Java applications, you need the full Java 2 Platform, Standard
Edition, Software Development Kit (J2SE SDK), formerly known as the Java
Development Kit (JDK). You also need the Java Runtime Environment (JRE).
Integrated Development Environment
For ease in developing the application, you can choose to develop your application in
an integrated development environment (IDE). This guide uses Oracle JDeveloper to
create the files for this application. For more information about installing JDeveloper,
refer to Installing Oracle JDeveloper.
Note: For information about creating and using secure passwords
with Oracle Database 12c Release 1 (12.1), refer to Oracle Database
Security Guide.
Note: Oracle Database 12c Release 1 (12.1) supports JDK 6 and JDK
7.
See Also:
■ http://www.oracle.com/technetwork/java/javase/downloads/ind
ex.html for information about installing Java
■ http://www.oracle.com/technetwork/java/overview-141217.html
for information about the JDBC API
Verifying the Oracle Database 12c Release 1 (12.1) Installation
Getting Started with the Application 2-3
Web Server
The sample application developed in this guide uses JavaServer Pages (JSP)
technology to display information and accept input from users. To deploy these pages,
you need a Web server with a servlet and JSP container, such as the Apache Tomcat
application server.
This guide uses the embedded server called the Oracle WebLogic Server in JDeveloper
for deploying the JSP pages. If you choose not to install Oracle JDeveloper, then any
Web server that enables you to deploy JSP pages should suffice.
JDeveloper supports direct deployment to the following production application
servers:
■ Oracle WebLogic Server
■ Oracle Application Server
■ Apache Tomcat
■ IBM WebSphere
■ JBoss
For more information about these servers, please refer to vendor-specific
documentation.
Verifying the Oracle Database 12c Release 1 (12.1) Installation
Oracle Database 12c Release 1 (12.1) installation is platform-specific. You must verify
that the installation was successful before you proceed to create the sample
application. This section describes the steps for verifying an Oracle Database 12c
Release 1 (12.1) installation.
Verifying a installation involves the following tasks:
■ Checking Installed Directories and Files
■ Checking the Environment Variables
■ Determining the JDBC Driver Version
Checking Installed Directories and Files
Installing Oracle Java products creates the following directories:
■ ORACLE_HOME/jdbc
■ ORACLE_HOME /jlib
Check if the directories described in Table 2–1 have been created and populated in the
ORACLE_HOME directory.
Table 2–1 Directories and Files in the ORACLE_HOME Directory
Directory Description
$OH/jdbc/lib The lib directory contains the ojdbc6.jar and ojdbc7.jar
required Java classes. These contain the JDBC driver classes for
use with JDK 6 and JDK 7.
$OH/jdbc/Readme.txt This file contains late-breaking and release-specific information
about the drivers, which may not have been included in other
documentation on the product.
Verifying the Oracle Database 12c Release 1 (12.1) Installation
2-4 Oracle Database 2 Day + Java Developer's Guide
Checking the Environment Variables
This section describes the environment variables that must be set for the JDBC Thin
Driver. You must set the classpath for your installed JDBC Thin Driver. For JDK 6, you
must set the following values for the CLASSPATH variable:
ORACLE_HOME/jdbc/lib/ojdbc6.jar
ORACLE_HOME/jlib/orai18n.jar
For JDK 7, you must set the following values for the CLASSPATH variable:
ORACLE_HOME/jdbc/lib/ojdbc7.jar
ORACLE_HOME/jlib/orai18n.jar
Ensure that there is only one JDBC class file, such as ojdbc6.jar, and one
globalization classes file, orai18n.jar, in the CLASSPATH variable.
Determining the JDBC Driver Version
Starting from Oracle Database 12c Release 1 (12.1), you can get details about the JDBC
support in the database as follows:
> java -jar ojdbc6.jar
Oracle 12.1.0.0. JDBC 4.0 compiled with JDK6
In addition, you can determine the version of the JDBC driver that you installed by
calling the getDriverVersion method of the OracleDatabaseMetaData class.
Example 2–1 illustrates how to determine the driver version:
Example 2–1 Determining the JDBC Driver Version
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class JDBCVersion
{
public static void main (String args[]) throws SQLException
{
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:hr/hr@localhost:1521/oracle");
Connection conn = ods.getConnection();
$OH/jlib This directory contains the orai18n.jar file. This file contains
classes for globalization and multibyte character sets support.
Note: Use the ojdbcn.jar file (where ’n’ is the release number)
supplied with Oracle Database Installation Guide, or Oracle
Database Client Installation Guide.
Note: The JDBC Thin Driver requires a TCP/IP listener to be
running on the computer where the database is installed.
Table 2–1 (Cont.) Directories and Files in the ORACLE_HOME Directory
Directory Description
Installing Oracle JDeveloper
Getting Started with the Application 2-5
// Create Oracle DatabaseMetaData object
DatabaseMetaData meta = conn.getMetaData();
// gets driver info:
System.out.println("JDBC driver version is " + meta.getDriverVersion());
}
}
Installing Oracle JDeveloper
In this guide, the integrated development environment (IDE) that is used to create the
sample Java application using JDBC is Oracle JDeveloper release 11.1.1. This release of
JDeveloper is supported on the Microsoft Windows Vista, Windows XP, Windows
2003, Windows 2000, Linux, and Mac OS X operating systems. Installation of the latest
version of JDeveloper is described in detail in Installation Guide for Oracle JDeveloper,
which is available online on the Oracle Technology Network at
http://download.oracle.com/docs/cd/E12839_01/install.1111/e13666/toc.htm
Also read JDeveloper 11g Release Notes, which is available online on the Oracle
Technology Network at
http://www.oracle.com/technetwork/developer-tools/jdev/overview/index.html
Installing Oracle JDeveloper
2-6 Oracle Database 2 Day + Java Developer's Guide
3
Connecting to Oracle Database 12c Release 1 (12.1) 3-1
3 Connecting to Oracle Database 12c Release 1
(12.1)
This chapter is the first in a series of five chapters, each of which describes how to
create parts of a Java application that accesses Oracle Database 12c Release 1 (12.1) and
displays, modifies, deletes, and updates data on it. To be able to access the database
from a Java application, you must connect to the database using a
java.sql.Connection object.
This chapter includes the following sections:
■ Connecting to Oracle Database from JDeveloper
■ Setting Up Applications and Projects in JDeveloper
■ Connecting to Oracle Database from a Java Application
Connecting to Oracle Database from JDeveloper
You can set up and manage database connections in JDeveloper to enable your
application to communicate with external data sources, including Oracle Database 12c
Release 1 (12.1) and offline database objects. This is done using the Database
Navigator. The same navigator is also used to manage other connections your
application needs, such as connections to application servers. The following
subsections describe how you can use the Database Navigator to view the database
and its objects and to create a connection to the database:
■ JDeveloper Database Navigator
■ Creating a Database Connection
■ Browsing the Data Using the Database Navigator
JDeveloper Database Navigator
The Database Navigator displays all currently defined connections. To view the
Database Navigator, select the Database Navigator tab in the navigator panel on the
top left-hand side of the JDeveloper display, if it is displayed, or use the View menu.
For an illustration of the default layout of the JDeveloper IDE, see Figure 1–1.
You can use the Database Navigator to browse through the connections it displays. In
particular, for a database schema, you can also view database objects, tables, views,
and their contents.
Database connections are shown under the IDE Connections node. To view the objects
in the database, expand the connection. Expanding a schema displays nodes for the
object types in that schema. Expanding the node for an object type displays its
Connecting to Oracle Database from JDeveloper
3-2 Oracle Database 2 Day + Java Developer's Guide
individual objects. When you expand a table node, you can view the structure of the
table and the data within the table.
Creating a Database Connection
You can connect to any database for which you have connection details. When you
create a database connection, you must specify a user name and a password. By
default, the connection enables you to browse only the schema of the user that you
specify in the connection.
To create a connection, follow these steps:
1. Start JDeveloper.
2. From the View menu, go to Database and select Database Navigator. The
Database Navigator is displayed, showing you a list of available connections.
3. Right-click IDE Connection, and from the shortcut menu, select New Connection.
The Create Database Connection screen is displayed.
4. On the Create Database Connection screen, do not change the default values for
the connection name and type, Connection1 and Oracle (JDBC). Enter HR in both
the Username and Password fields. Do not enter a value for Role, and select
Deploy Password. You must provide information about the computer where your
database is located. Your database administrator should provide you with this
information.
Enter the following information:
– Driver: thin
– Host Name: Host name of the computer where Oracle Database 12c
Release 1 (12.1) is installed
If the database is on the same computer, then for the Host Name parameter,
enter localhost.
– JDBC Port: 1521
– SID: oracle
Click Test Connection. If the connection is successful, the word Success! is
displayed in the Status field.
Figure 3–1 shows the Connection screen where you enter these details.
Connecting to Oracle Database from JDeveloper
Connecting to Oracle Database 12c Release 1 (12.1) 3-3
Figure 3–1 Specifying Connection Details
5. Click Finish to create the connection and close the screen.
Disconnecting and Reconnecting from Oracle Database in JDeveloper
To disconnect from the database in JDeveloper, in the Database Navigator, right-click
the connection name and select Disconnect. The display in the Database Navigator
now shows only the name of the connection, without the plus (+) symbol for
expanding the node. To reconnect to the database, right-click the connection name and
select Connect.
Browsing the Data Using the Database Navigator
After you have successfully established a connection to the database, you can browse
its contents through the Database Navigator. The Database Navigator displays a
navigable, hierarchical tree structure for the database, its objects, their instances, and
the contents of each. To view the contents at each level of the hierarchy of the database
connection that you created, do the following:
1. The IDE Connections node in the Database Navigator now shows a node with the
name of your connection. Click the plus symbol (+) to the left of the connection
name to expand the navigation tree. To display a list of the instances of an object
type, for example Tables, expand the Table navigation tree.
2. The Structure window below the navigator shows the detailed structure of any
object selected in the navigator. Select a table in the navigator (for example
Employees) to see the columns of that table in the Structure window.
Setting Up Applications and Projects in JDeveloper
3-4 Oracle Database 2 Day + Java Developer's Guide
Figure 3–2 Viewing the Table Structure and Data
3. If you double-click a table in the navigator, the structure of that table is displayed
in the main editing area of the window. It includes details about all the columns,
such as Name, Type, and Size, so you can browse the table definition.
To view the data from a table, select the Data tab below the table structure. You
can now view and browse through the table data.
4. You can also edit the objects in the Database Navigator. To edit a table, right-click
the table and select Edit from the shortcut menu. A dialog box enables you to
make changes to the selected table.
Setting Up Applications and Projects in JDeveloper
In JDeveloper, you create your work in an application, within which you can organize
your work into a number of projects. JDeveloper provides a number of application
templates, to help you to create the project structure for standard types of application
relatively quickly and easily. At the time you create your application in JDeveloper,
Setting Up Applications and Projects in JDeveloper
Connecting to Oracle Database 12c Release 1 (12.1) 3-5
you can choose the application template that matches the type of application you will
be building.
The application template you select determines the initial project structure (the named
project folders within the application) and the application technologies that will be
included. You can then add any extra libraries or technologies you need for your
particular application, and create additional projects if you need them.
Using the JDeveloper Application Navigator
The Application Navigator displays all your applications and projects. When you first
start JDeveloper, the Application Navigator is displayed by default on the left side of
the JDeveloper IDE.
To view the Application Navigator when it is not displayed, you can click the
Applications tab in the navigator panel on the top left-hand side of the JDeveloper
display, or select Application Navigator from the View menu.
The Application Navigator shows a logical grouping of the items in your projects. To
see the structure of an individual item, you can select it and the structure is displayed
in the Structure window.
From the Application Navigator, you can display items in an appropriate default
editor. For example, if you double-click a Java file, the file opens in the Java Source
Editor, and if you double-click a JavaServer Pages (JSP) file, it opens in the JSP/HTML
Visual Editor.
Creating an Application and a Project
To get started with JDeveloper, you must create an application and at least one project
in which to store your work, as follows:
1. In the Application Navigator, click New Application.
2. The Create Generic Application wizard is displayed. In the Name your application
screen, enter HRApp in the Application Name field, and from the Application
Template list, select Generic Application. Click Next.
3. On the Name your project screen, enter View as the name of the project. Click
Finish.
4. The new HRApp application is displayed in the Application Navigator.
5. Save your application. To do this, from the File menu, select Save All.
Viewing the Javadoc and Source Code Available in the Project Scope
You can view the Javadoc or the code for any of the classes available in the project
technology scope within JDeveloper. In addition, you can view the details of all the
methods available for those classes.
For example, to see the code or Javadoc for the Connection class, do the following:
1. With your project selected in the Application Navigator, from the Navigate menu
select Go to Java Type. You can also do this for a specific file in your project.
2. In the Go to Java Type dialog box, type the name of the Java class.
3. Enter the name of the class you want to view in the Name field, or click Browse to
find the class. For the Connection class, start to enter Connection, and from the
displayed list select Connection (java.sql).
Connecting to Oracle Database from a Java Application
3-6 Oracle Database 2 Day + Java Developer's Guide
Figure 3–3 Selecting the Class to View the Javadoc in JDeveloper
4. Click OK.
Connecting to Oracle Database from a Java Application
So far, you have seen how to connect to the database from JDeveloper. To initiate a
connection from the Java application, you use the Connection object from the JDBC
application programming interface (API).
This section describes connecting to the database from the Java application in the
following subsections:
■ Overview of Connecting to Oracle Database
■ Specifying Database URLs
■ Creating a Java Class in JDeveloper
■ Java Libraries
■ Adding JDBC and JSP Libraries
■ Importing JDBC Packages
■ Declaring Connection-Related Variables
■ Creating the Connection Method
Overview of Connecting to Oracle Database
In Java, you use an instance of the DataSource object to get a connection to the
database. The DataSource interface provides a complete replacement for the previous
JDBC DriverManager class. Oracle implements the javax.sql.DataSource interface
with the OracleDataSource class in the oracle.jdbc.pool package. The overloaded
getConnection method returns a physical connection to the database.
You can either set properties using appropriate setxxx methods for the DataSource
object or use the getConnection method that accepts these properties as input
parameters.
Important DataSource Properties are listed in Table 3–1.
Note: The use of the DriverManager class to establish a connection to
a database is deprecated.
Connecting to Oracle Database from a Java Application
Connecting to Oracle Database 12c Release 1 (12.1) 3-7
If you choose to set the url property of the DataSource object with all necessary
parameters, then you can connect to the database without setting any other properties
or specifying any additional parameters with the getDBConnection method. For more
information about setting the database URL, refer to the Specifying Database URLs
section.
Specifying Database URLs
This release of Oracle JVM supports Internet Protocol Version 6 (IPv6) addresses in the
URL and system names of the Java code in the database, which resolve to IPv6
addresses.
Database URLs are strings that you specify for the value of the url property of the
DataSource object. The complete URL syntax is the following:
jdbc:oracle:driver_type:[username/password]@database_specifier
The first part of the URL specifies which JDBC driver is to be used. The supported
driver_type values for client-side applications are thin and oci. The brackets indicate
that the user name and password pair is optional. The database_specifier value
identifies the database to which the application is connected.
The following is the syntax for thin-style service names that are supported by the Thin
driver:
jdbc:oracle:driver_type:[username/password]@//host_name:port_number:SID
For the sample application created in this guide, if you include the user name and
password, and if the database is hosted locally, then the database connection URL is as
Table 3–1 Standard Data Source Properties
Name Type Description
databaseName String Name of the particular database on the server. Also known as the service name
(or SID) in Oracle terminology.
dataSourceName String Name of the underlying data source class.
description String Description of the data source.
networkProtocol String Network protocol for communicating with the server. For Oracle, this applies
only to the JDBC Oracle Call Interface (OCI) drivers and defaults to tcp.
password String Password for the connecting user.
portNumber int Number of the port where the server listens for requests
serverName String Name of the database server
user String User name to be used for login
driverType String Specifies the Oracle JDBC driver type. It can be either oci or thin.
This is an Oracle-specific property.
url String Specifies the URL of the database connect string.You can use this property in
place of the standard portNumber, networkProtocol, serverName, and
databaseName properties.
This is an Oracle-specific property.
Note: The parameters specified through the getConnection method
override all property and url parameter settings previously specified
in the application.
Connecting to Oracle Database from a Java Application
3-8 Oracle Database 2 Day + Java Developer's Guide
shown in Example 3–1.
Example 3–1 Specifying the url Property for the DataSource Object
jdbc:oracle:thin:hr/hr@localhost:1521:oracle
Using the Default Service Feature of the Oracle Database
If you have performed Oracle Database server installation in Typical mode, then the
default service name used by the Oracle instance is ORCL, and the following Easy
Connect syntax can be used to connect to that instance:
sqlplus /nolog
SQL> CONNECT username@"host/ORCL"
SQL> Enter password: password
The Easy Connect feature, which was introduced in Oracle Database 11g Release 1
(11.1), makes the following parts of the conventional JDBC connection URL syntax
optional:
jdbc:oracle:driver_type:[username/password]@[//]host_name[:port][:oracle]
In this URL:
■ // is optional.
■ :port is optional.
Specify a port only if the default Oracle Net listener port (1521) is not used.
■ :oracle (or the service name) is optional.
The connection adapter for the Oracle Database connects to the default service on
the host. On the host, this is set to ORACLE in the listener.ora file.
Example 3–2 shows a basic configuration of the listener.ora file, where the default
service is defined.
Example 3–2 Default Service Configuration in listener.ora
MYLISTENER = (ADDRESS_LIST=
(ADDRESS=(PROTOCOL=tcp)(HOST=test555)(PORT=1521))
)
DEFAULT_SERVICE_MYLISTENER=dbjf.regress.rdbms.dev.testserver.com
SID_LIST_MYLISTENER = (SID_LIST=
(SID_DESC=(SID_NAME=dbjf)(GLOBAL_
DBNAME=dbjf.regress.rdbms.dev.testserver.com)(ORACLE_HOME=/test/oracle))
)
After making changes to the listener.ora file, you must restart the listener with the
following command:
> lsnrctl start mylistener
The following URLs should work with this configuration:
jdbc:oracle:thin:@//test555.testserver.com
jdbc:oracle:thin:@//test555.testserver.com:1521
jdbc:oracle:thin:@test555.testserver.com
See Also: Oracle Database Net Services Administrator's Guide for more
information about the Easy Connect feature
Connecting to Oracle Database from a Java Application
Connecting to Oracle Database 12c Release 1 (12.1) 3-9
jdbc:oracle:thin:@test555.testserver.com:1521
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test555.testserver.com
)(PORT=1521)))
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test555.testserver.com
)))
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test555.testserver.com
)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=)))
Creating a Java Class in JDeveloper
The first step in building a Java application is to create a Java class. The following
instructions describe how you create a class called DataHandler, which will contain the
methods for querying the database and modifying the data in it.
1. In the Application Navigator, right-click the View project, and from the shortcut
menu, select New.
2. In the New Gallery dialog box, select General in the Categories list. In the Items
list, select Java Class, and click OK. The Create Java Class dialog box is displayed.
3. In the Create Java Class dialog box, enter DataHandler in the Name text box, and
hr in the Package text box. Do not change the default values of the Optional
Attributes, and click OK. The Create Java Class dialog box with the appropriate
values specified is shown in Figure 3–4.
Figure 3–4 Creating a Java Class
4. The skeleton DataHandler class is created and is displayed in the Java Source
Editor. The package declaration, the class declaration, and the default constructor
are created by default. Figure 3–5 shows the class displayed in the Java Source
Note: Default service is a new feature in Oracle Database 12c Release
1 (12.1). If you use any other version of the Oracle Database to connect
to the database, then you must specify the SID and port number.
Connecting to Oracle Database from a Java Application
3-10 Oracle Database 2 Day + Java Developer's Guide
Editor, ready for you to add your Java code:
Figure 3–5 Java Source Editor
Java Libraries
Oracle JDeveloper comes with standard libraries to help Java application
programming. These libraries include API support for Application Development
Framework (ADF), Oracle libraries for JDBC, JSP, and so on.
To use JDBC in your project, you import the Oracle JDBC library into the project.
Similarly, to use JSP technology, you import the JSP Runtime library.
Overview of the Oracle JDBC Library
Important packages of the Oracle JDBC library include the following:
■ oracle.jdbc: The interfaces of the oracle.jdbc package define the Oracle
extensions to the interfaces in the java.sql package. These extensions provide
access to Oracle SQL-format data and other Oracle-specific features, including
Oracle performance enhancements.
■ oracle.sql: The oracle.sql package supports direct access to data in SQL
format. This package consists primarily of classes that provide Java mappings to
SQL data types and their support classes.
■ oracle.jdbc.pool: This package includes the OracleDataSource class that is used
to get a connection to the database. The overloaded getConnection method
returns a physical connection to the database.
Overview of the JSP Runtime Library
This library includes the classes and tag libraries required to interpret and run JSP files
on the Oracle WebLogic Server that comes with JDeveloper.
Adding JDBC and JSP Libraries
To include libraries in your project, perform the following steps:
1. Double-click the View project in the Application Navigator to display the Project
Properties dialog box.
2. Click Libraries and Classpath, and then click Add Library.... The Add Library
dialog box is displayed with a list of the available libraries for the Java2 Platform,
Standard Edition (J2SE) version is displayed.
Connecting to Oracle Database from a Java Application
Connecting to Oracle Database 12c Release 1 (12.1) 3-11
3. In the Add Library dialog box, scroll through the list of libraries in the Extension
folder. Select JSP Runtime library and click OK to add it to the list of selected
libraries for your project. Similarly, add the Oracle JDBC library. Figure 3–6 shows
the Oracle JDBC library added to the View project.
Figure 3–6 Importing Libraries
4. Click OK.
Importing JDBC Packages
To use JDBC in the Java application, import the following JDBC packages:
1. If the DataHandler.java class is not already open in the Java Source Editor, in the
Application Navigator, expand the View project, Application Sources, and your
package (hr) and double-click DataHandler.java.
2. At the end of the generated package declaration, on a new line, enter the import
statements shown in Example 3–3.
Example 3–3 Importing Packages in a Java Application
package hr;
import java.sql.Connection;
import oracle.jdbc.pool.OracleDataSource;
Declaring Connection-Related Variables
Connection information is passed to the connection method by using the following
connection variables: the connection URL, a user name, and the corresponding
password.
Use the Java Source Editor of JDeveloper to edit the DataHandler.java class as
follows:
Connecting to Oracle Database from a Java Application
3-12 Oracle Database 2 Day + Java Developer's Guide
1. After the DataHandler constructor, on a new line, declare the three connection
variables as follows:
String jdbcUrl = null;
String userid = null;
String password = null;
These variables will be used in the application to contain values supplied by the
user at login to authenticate the user and to create a connection to the database.
The jdbcUrl variable is used to hold the URL of the database that you will connect
to. The userid and password variables are used to authenticate the user and
identify the schema to be used for the session.
2. On a new line, declare a connection instance as follows:
Connection conn;
Your Java class should now contain the code in Example 3–4.
Example 3–4 Declaring Connection Variables and the Connection Object
package hr;
import java.sql.Connection;
import oracle.jdbc.pool.OracleDataSource;
public class DataHandler {
public DataHandler() {
}
String jdbcUrl = null;
String userid = null;
String password = null;
Connection conn;
}
Creating the Connection Method
To connect to the database, you must create a method as follows:
Note: The login variables have been set to null to secure the
application. At this point in the guide, application login functionality
is yet to be built into the application. Therefore, to test the application
until login functionality is built in, you can set values in the login
variables as follows:
Set the jdbcUrl variable to the connect string for your database.
String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORACLE";
Set the variables userid and password to hr as follows:
String userid = "hr";
String password = "hr";
Make sure you reset these to null as soon as you finish testing.
For more information about security features and practices, refer to
Oracle Database Security Guide and the vendor-specific documentation
for your development environment.
Connecting to Oracle Database from a Java Application
Connecting to Oracle Database 12c Release 1 (12.1) 3-13
1. Add the following method declaration after the connection declaration:
public void getDBConnection() throws SQLException
The Java Code Insight feature displays a message reminding you to import the
SQLException error handling package. Press the Alt+Enter keys to import it. The
import java.sql.SQLException statement is added to the list of import packages.
2. At the end of the same line, add an open brace ({) and then press the Enter key.
JDeveloper automatically creates the closing brace, and positions the cursor in a
new empty line between the braces.
3. On a new line, declare an OracleDataSource instance as follows:
OracleDataSource ds;
4. Enter the following to create a new OracleDataSource object:
ds = new OracleDataSource();
5. Start to enter the following to set the URL for the DataSource object:
ds.setURL(jdbcUrl);
Java Code Insight prompts you by providing you with a list of available
OracleDataSource methods. Scroll through the list to select the setURL(String)
method, and press the Enter key to select it into your code. In the parentheses for
this function, enter jdbcUrl in place of arg0.
Figure 3–7 shows how the Java Code Insight feature in JDeveloper helps you with
inserting code.
Figure 3–7 Java Code Insight
6. On the next line, enter the following:
conn = ds.getConnection(userid,password);
As usual, Java Code Insight will prompt you with a list of methods for ds. This
time, select getConnection(String,String). In the parentheses, enter
userid,password. End the line with a semicolon (;).
Your code should look similar to the code in Example 3–5.
Example 3–5 Adding a Method to Connect to the Database
package hr;
import java.sql.Connection;
import java.sql.SQLException;
Connecting to Oracle Database from a Java Application
3-14 Oracle Database 2 Day + Java Developer's Guide
import oracle.jdbc.pool.OracleDataSource;
public class DataHandler {
public DataHandler() {
}
String jdbcUrl = null;
String userid = null;
String password = null;
Connection conn;
public void getDBConnection() throws SQLException{
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn=ds.getConnection(userid,password);
}
}
7. Compile your class to ensure that there are no syntax errors. To do this, right-click
in the Java Source Editor, and select Make from the shortcut menu. A Successful
compilation message is displayed in the Log window below the Java Source
Editor window.
4
Querying for and Displaying Data 4-1
4 Querying for and Displaying Data
This chapter adds functions and code to the DataHandler.java file for querying the
database. This chapter has the following sections:
■ Overview of Querying for Data in Oracle Database
■ Querying Data from a Java Application
■ Creating JSP Pages
■ Adding Dynamic Content to the JSP Page: Database Query Results
■ Filtering a Query Result Set
■ Adding Login Functionality to the Application
■ Testing the JSP Page
Overview of Querying for Data in Oracle Database
In outline, to query Oracle Database 12c Release 1 (12.1) from a Java class to retrieve
data, you must do the following:
1. Create a connection by using the OracleDataSource.getConnection method. This
is covered in Chapter 3, "Connecting to Oracle Database 12c Release 1 (12.1)".
2. Define your SQL statements with the methods available for the connection object.
The createStatement method is used to define a SQL query statement.
3. Using the methods available for the statement, run your queries. You use the
executeQuery method to run queries on the database and produce a set of rows
that match the query conditions. These results are contained in a ResultSet object.
4. You use a ResultSet object to display the data in the application pages.
The following sections describe important Java Database Connectivity (JDBC)
concepts related to querying the database from a Java application:
■ SQL Statements
■ Query Methods for the Statement Object
■ Result Sets
SQL Statements
Once you connect to the database and, in the process, create a Connection object, the
next step is to create a Statement object. The createStatement method of the JDBC
Connection object returns an object of the JDBC Statement type. Example 4–1 shows
how to create a Statement object.
Overview of Querying for Data in Oracle Database
4-2 Oracle Database 2 Day + Java Developer's Guide
Example 4–1 Creating a Statement Object
Statement stmt = conn.createStatement();
The Statement object is used to run static SQL queries that can be coded into the
application.
In addition, for scenarios where many similar queries with differing update values
must be run on the database, you use the OraclePreparedStatement object, which
extends the Statement object. To access stored procedures on Oracle Database 12c
Release 1 (12.1), you use the OracleCallableStatement object.
Query Methods for the Statement Object
To run a query embedded in a Statement object, you use variants of the execute
method. Important variants of this method are listed in Table 4–1.
Result Sets
A ResultSet object contains a table of data representing a database result set, which is
generated by executing a statement that queries the database.
A cursor points to the current row of data in a ResultSet object. Initially, it is
positioned before the first row. Use the next method of the ResultSet object to move
the cursor to the next row in the result set. It returns false when there are no more
rows in the ResultSet object. Typically, the contents of a ResultSet object are read by
using the next method within a loop until it returns false.
See Also:
■ Using OraclePreparedStatement
■ Using OracleCallableStatement
Table 4–1 Key Query Execution Methods for java.sql.Statement
Method Name Return Type Description
execute(String sql) Boolean Runs the given SQL statement, which
returns a Boolean response: true if the
query runs successfully and false if it
does not.
addBatch() void Adds a set of parameters to a
PreparedStatement object batch of
commands.
executeBatch() int[] Submits a batch of commands to the
database for running, and returns an
array of update counts if all commands
run successfully.
executeQuery(String sql) ResultSet Runs the given SQL statement, which
returns a single ResultSet object.
executeUpdate(String sql) int Runs the given SQL statement, which
may be an INSERT, UPDATE, or DELETE
statement or a SQL statement that returns
nothing, such as a SQL DDL statement.
See Also:
http://www.oracle.com/technetwork/java/javase/documentation/
api-jsp-136079.html
Overview of Querying for Data in Oracle Database
Querying for and Displaying Data 4-3
The ResultSet interface provides accessor methods (getBoolean, getLong, getInt,
and so on) for retrieving column values from the current row. Values can be retrieved
by using either the index number of the column or the name of the column.
By default, only one ResultSet object per Statement object can be open at the same
time. Therefore, to read data from multiple ResultSet objects, you must use multiple
Statement objects. A ResultSet object is automatically closed when the Statement
object that generated it is closed, rerun, or used to retrieve the next result from a
sequence of multiple results.
Features of ResultSet Objects
Scrollability refers to the ability to move backward as well as forward through a result
set. You can also move to any particular position in the result set, through either
relative positioning or absolute positioning. Relative positioning lets you move a
specified number of rows forward or backward from the current row. Absolute
positioning lets you move to a specified row number, counting from either the
beginning or the end of the result set.
When creating a scrollable or positionable result set, you must also specify sensitivity.
This refers to the ability of a result set to detect and reveal changes made to the
underlying database from outside the result set. A sensitive result set can see changes
made to the database while the result set is open, providing a dynamic view of the
underlying data. Changes made to the underlying column values of rows in the result
set are visible. Updatability refers to the ability to update data in a result set and then
copy the changes to the database. This includes inserting new rows into the result set
or deleting existing rows. A result set may be updatable or read-only.
Summary of Result Set Object Types
Scrollability and sensitivity are independent of updatability, and the three result set
types and two concurrency types combine for the following six result set categories:
■ Forward-only/read-only
■ Forward-only/updatable
■ Scroll-sensitive/read-only
■ Scroll-sensitive/updatable
■ Scroll-insensitive/read-only
■ Scroll-insensitive/updatable
Example 4–2 demonstrates how to declare a scroll-sensitive and read-only ResultSet
object.
Example 4–2 Declaring a Scroll-Sensitive, Read-Only ResultSet Object
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_
READ_ONLY);
See Also:
■ Oracle Database JDBC Developer's Guide for more information about
result sets and their features
Querying Data from a Java Application
4-4 Oracle Database 2 Day + Java Developer's Guide
Querying Data from a Java Application
This section discusses how you can use JDeveloper to create a Java class that queries
data in Oracle Database 12c Release 1 (12.1) in the following sections:
■ Creating a Method in JDeveloper to Query Data
■ Testing the Connection and the Query Methods
Creating a Method in JDeveloper to Query Data
The following steps show you how to add a simple query method to your
DataHandler.java class. If DataHandler.java is not open in the JDeveloper integrated
development environment (IDE), double-click the DataHandler.java file in the
Application Navigator to display it in the Java Source Editor.
1. In the DataHandler class, add the following import statements after the existing
import statements to use the Statement and ResultSet JDBC classes:
import java.sql.Statement;
import java.sql.ResultSet;
2. After the connection declaration, declare variables for Statement, ResultSet, and
String objects as follows:
Statement stmt;
ResultSet rset;
String query;
String sqlString;
3. Create a method called getAllEmployees, which will be used to retrieve employee
information from the database. Enter the signature for the method:
public ResultSet getAllEmployees() throws SQLException{
4. Press the Enter key to include the closing brace for the method and a new line to
start entering the method code.
5. Call the getDBConnection method created earlier:
getDBConnection();
6. After calling the getDBConnection method, use the createStatement method of
the Connection instance to provide context for executing the SQL statement and
define the ResultSet type. Specify a read-only, scroll-sensitive ResultSet type as
stated in the following code:
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_
READ_ONLY);
The Java Code Insight feature can help you ensure that the statement syntax is
correct.
Note: A forward-only updatable result set has no provision for
positioning at a particular row within the ResultSet object. You can
update rows only as you iterate through them using the next
method.
Querying Data from a Java Application
Querying for and Displaying Data 4-5
7. Define the query and print a trace message. The following code uses a simple
query to return all the rows and columns in the Employees table, where the data is
ordered by the Employee ID:
query = "SELECT * FROM Employees ORDER BY employee_id";
System.out.println("nExecuting query: " + query);
8. Run the query and retrieve the results in the ResultSet instance as follows:
rset = stmt.executeQuery(query);
9. Return the ResultSet object:
return rset;
10. Save your work. From the File menu, select Save All.
The code for the getAllEmployees method should be as shown in Example 4–3.
Example 4–3 Using the Connection, Statement, Query, and ResultSet Objects
public ResultSet getAllEmployees() throws SQLException{
getDBConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
query = "SELECT * FROM Employees ORDER BY employee_id";
System.out.println("nExecuting query: " + query);
rset = stmt.executeQuery(query);
return rset;
}
Testing the Connection and the Query Methods
In the following steps, you create a simple Java class to test the methods in the
DataHandler.java class. To test your application at this stage, you can temporarily set
the value of the jdbcUrl variable to the connection string for your database and set the
values of the userid and password variables to the values required to access the HR
schema.
1. Open the DataHandler.java class in the Java Visual Editor from the Application
Navigator.
2. Change the jdbcUrl, userid and password variables to contain the values required
for the HR schema as follows:
String jdbcUrl = "connect-string";
String userid = "HR";
String password = "hr";
where connect-string is, for example:
jdbc:oracle:thin:@localhost:1521:ORACLE
3. Create a new Java class named JavaClient in the hr package. Make it a public
class and generate a default constructor and a main method. Note that you must
select the Main Method check box from the Optional Attributes panel to generate
the main method.
The skeleton JavaClient.java class is created and displayed in the Java Source
Editor.
See Also: Declaring Connection-Related Variables in Chapter 3
Querying Data from a Java Application
4-6 Oracle Database 2 Day + Java Developer's Guide
4. Import the ResultSet package:
import java.sql.ResultSet;
5. In the main method declaration, add exception handling as follows:
public static void main(String[] args) throws Exception{
6. Replace the JavaClient object created by default with a DataHandler object.
Locate the following line in the main method:
JavaClient javaClient = new JavaClient();
Replace this with:
DataHandler datahandler = new DataHandler();
7. Define a ResultSet object to hold the results of the getAllEmployees query, and
iterate through the rows of the result set, displaying the first four columns,
Employee Id, First Name, Last Name, and Email. To do this, add the following
code to the main method:
ResultSet rset = datahandler.getAllEmployees();
while (rset.next()) {
System.out.println(rset.getInt(1) + " " +
rset.getString(2) + " " +
rset.getString(3) + " " +
rset.getString(4));
}
8. Compile the JavaClient.java file to check for compilation errors. To do this,
right-click in the Java Source Editor, and select Make from the shortcut menu.
If there are no errors in compilation, you should see the following message in the
Log window:
Successful compilation: 0 errors, 0 warnings
9. Run the JavaClient.java file. To do this, right-click in the Java Source Editor
window and select Run from the shortcut menu.
10. Examine the output in the Log window. Notice the trace message, followed by the
four columns from the Employees table as shown in Figure 4–1.
See Also: Creating a Java Class in JDeveloper in Chapter 3 for
information about creating a Java class file
Creating JSP Pages
Querying for and Displaying Data 4-7
Figure 4–1 Test Output for Query Method in Log Window
11. When you finish testing the application, set the jdbcUrl, userid and password
variables in DataHandler.java back to null.
Creating JSP Pages
The HRApp application uses JavaServer Pages (JSP) technology to display data. JSP
technology provides a simple, fast way to create server-independent and
platform-independent dynamic Web content. A JSP page has the .jsp extension. This
extension notifies the Web server that the page should be processed by a JSP container.
The JSP container interprets the JSP tags and scriptlets, generates the content required,
and sends the results back to the client as an HTML or XML page.
To develop JSP pages, you use some or all of the following:
■ HTML tags to design and format the dynamically generated Web page
■ Standard JSP tags or Java-based scriptlets to call other components that generate
the dynamic content on the page
■ JSP tags from custom tag libraries that generate the dynamic content on the page
In this section, you will see how you can create JSP pages for the application in this
guide in the following sections:
■ Overview of Page Presentation
■ Creating a Simple JSP Page
■ Adding Static Content to a JSP Page
■ Adding a Style Sheet to a JSP Page
Overview of Page Presentation
JSP pages can do the following:
■ Display data
■ Hold input data entered by users adding employees and editing employee data
See Also: Declaring Connection-Related Variables
See Also:
http://www.oracle.com/technetwork/java/javaee/jsp/index.html
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide
Oracle database 12c 2 day + java developer's guide

More Related Content

What's hot

Oracle database 12c application express end user's guide
Oracle database 12c application express end user's guideOracle database 12c application express end user's guide
Oracle database 12c application express end user's guide
bupbechanhgmail
 
Javaee tutorial
Javaee tutorialJavaee tutorial
Javaee tutorial
Luis Miguel
 
E49462 01
E49462 01E49462 01
High availability overview: Oracle Database 12c
High availability overview: Oracle Database 12cHigh availability overview: Oracle Database 12c
High availability overview: Oracle Database 12c
Femi Adeyemi
 
Oracle coher
Oracle coherOracle coher
Oracle coher
raj1290
 
Oracle timesten
Oracle timestenOracle timesten
Oracle timesten
vn_nguyenquang
 
Oracle database 12c 2 day developer's guide 123
Oracle database 12c 2 day developer's guide 123Oracle database 12c 2 day developer's guide 123
Oracle database 12c 2 day developer's guide 123
bupbechanhgmail
 
Oracle® database 2 days security guide e10575
Oracle® database 2 days security guide e10575Oracle® database 2 days security guide e10575
Oracle® database 2 days security guide e10575
imranshahid7861
 
Oracle database 12c advanced security guide
Oracle database 12c advanced security guideOracle database 12c advanced security guide
Oracle database 12c advanced security guide
bupbechanhgmail
 
Oracle database 12c client installation overview
Oracle database 12c client installation overviewOracle database 12c client installation overview
Oracle database 12c client installation overview
bupbechanhgmail
 
Oracle database 12c application express administration guide
Oracle database 12c application express administration guideOracle database 12c application express administration guide
Oracle database 12c application express administration guide
bupbechanhgmail
 
Oracle database 12c sql tuning
Oracle database 12c sql tuningOracle database 12c sql tuning
Oracle database 12c sql tuning
Femi Adeyemi
 
Developer’s guide for oracle data integrator
Developer’s guide for oracle data integratorDeveloper’s guide for oracle data integrator
Developer’s guide for oracle data integrator
Abhishek Srivastava
 
Oracle Incentive User Guide 122cnug
Oracle Incentive User Guide 122cnugOracle Incentive User Guide 122cnug
Oracle Incentive User Guide 122cnug
cuong vu
 
Xavier technology adapters
Xavier technology adaptersXavier technology adapters
Xavier technology adaptersprathap kumar
 
E13882== ORACLE SOA COOK BOOK
E13882== ORACLE SOA COOK BOOKE13882== ORACLE SOA COOK BOOK
E13882== ORACLE SOA COOK BOOK
prathap kumar
 
Oracle Enterprise Scheduler(ESS Job Scheduling)
Oracle Enterprise Scheduler(ESS Job Scheduling)Oracle Enterprise Scheduler(ESS Job Scheduling)
Oracle Enterprise Scheduler(ESS Job Scheduling)
TUSHAR VARSHNEY
 
Oracle@cloud adapter(SFDC integration with SOA Suites12c)
Oracle@cloud adapter(SFDC integration with SOA Suites12c)Oracle@cloud adapter(SFDC integration with SOA Suites12c)
Oracle@cloud adapter(SFDC integration with SOA Suites12c)
TUSHAR VARSHNEY
 

What's hot (20)

Oracle database 12c application express end user's guide
Oracle database 12c application express end user's guideOracle database 12c application express end user's guide
Oracle database 12c application express end user's guide
 
Javaee tutorial
Javaee tutorialJavaee tutorial
Javaee tutorial
 
E49462 01
E49462 01E49462 01
E49462 01
 
High availability overview: Oracle Database 12c
High availability overview: Oracle Database 12cHigh availability overview: Oracle Database 12c
High availability overview: Oracle Database 12c
 
120faug
120faug120faug
120faug
 
Oracle coher
Oracle coherOracle coher
Oracle coher
 
Oracle timesten
Oracle timestenOracle timesten
Oracle timesten
 
Oracle database 12c 2 day developer's guide 123
Oracle database 12c 2 day developer's guide 123Oracle database 12c 2 day developer's guide 123
Oracle database 12c 2 day developer's guide 123
 
Oracle® database 2 days security guide e10575
Oracle® database 2 days security guide e10575Oracle® database 2 days security guide e10575
Oracle® database 2 days security guide e10575
 
Oracle9
Oracle9Oracle9
Oracle9
 
Oracle database 12c advanced security guide
Oracle database 12c advanced security guideOracle database 12c advanced security guide
Oracle database 12c advanced security guide
 
Oracle database 12c client installation overview
Oracle database 12c client installation overviewOracle database 12c client installation overview
Oracle database 12c client installation overview
 
Oracle database 12c application express administration guide
Oracle database 12c application express administration guideOracle database 12c application express administration guide
Oracle database 12c application express administration guide
 
Oracle database 12c sql tuning
Oracle database 12c sql tuningOracle database 12c sql tuning
Oracle database 12c sql tuning
 
Developer’s guide for oracle data integrator
Developer’s guide for oracle data integratorDeveloper’s guide for oracle data integrator
Developer’s guide for oracle data integrator
 
Oracle Incentive User Guide 122cnug
Oracle Incentive User Guide 122cnugOracle Incentive User Guide 122cnug
Oracle Incentive User Guide 122cnug
 
Xavier technology adapters
Xavier technology adaptersXavier technology adapters
Xavier technology adapters
 
E13882== ORACLE SOA COOK BOOK
E13882== ORACLE SOA COOK BOOKE13882== ORACLE SOA COOK BOOK
E13882== ORACLE SOA COOK BOOK
 
Oracle Enterprise Scheduler(ESS Job Scheduling)
Oracle Enterprise Scheduler(ESS Job Scheduling)Oracle Enterprise Scheduler(ESS Job Scheduling)
Oracle Enterprise Scheduler(ESS Job Scheduling)
 
Oracle@cloud adapter(SFDC integration with SOA Suites12c)
Oracle@cloud adapter(SFDC integration with SOA Suites12c)Oracle@cloud adapter(SFDC integration with SOA Suites12c)
Oracle@cloud adapter(SFDC integration with SOA Suites12c)
 

Viewers also liked

Introduction to Java Cloud Service
Introduction to Java Cloud ServiceIntroduction to Java Cloud Service
Introduction to Java Cloud Service
Perficient, Inc.
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
Ranjan Kumar
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming LanguageLaxman Puri
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite
 

Viewers also liked (6)

Introduction to Java Cloud Service
Introduction to Java Cloud ServiceIntroduction to Java Cloud Service
Introduction to Java Cloud Service
 
Python - basics
Python - basicsPython - basics
Python - basics
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 

Similar to Oracle database 12c 2 day + java developer's guide

Platform Guide.pdf
Platform Guide.pdfPlatform Guide.pdf
Platform Guide.pdf
MartinCarrozzo
 
Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250
Udaykumar Sarana
 
Ora db 2 day+ app express dev guide
Ora db 2 day+ app express dev guideOra db 2 day+ app express dev guide
Ora db 2 day+ app express dev guide
Darnette A
 
PL_SQL Oracle Reference.pdf
PL_SQL Oracle Reference.pdfPL_SQL Oracle Reference.pdf
PL_SQL Oracle Reference.pdf
ARJUNMUKHERJEE27
 
Orac appl express guide release 4
Orac appl express guide release 4Orac appl express guide release 4
Orac appl express guide release 4Darnette A
 
Guia implementacion seguridad oracle 12c
Guia implementacion seguridad oracle 12cGuia implementacion seguridad oracle 12c
Guia implementacion seguridad oracle 12c
Otto Paiz
 
Oracle database 12c data masking and subsetting guide
Oracle database 12c data masking and subsetting guideOracle database 12c data masking and subsetting guide
Oracle database 12c data masking and subsetting guide
bupbechanhgmail
 
toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...
toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...
toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...
Chandan Bose
 
Getting Started on PeopleSoft InstallationJuly 2014.docx
Getting Started on PeopleSoft InstallationJuly 2014.docxGetting Started on PeopleSoft InstallationJuly 2014.docx
Getting Started on PeopleSoft InstallationJuly 2014.docx
gilbertkpeters11344
 
Odiun understanding oracle data integrator
Odiun understanding oracle data integratorOdiun understanding oracle data integrator
Odiun understanding oracle data integrator
gutiejun
 
WebLogic Scripting Tool
WebLogic Scripting ToolWebLogic Scripting Tool
WebLogic Scripting Tool
ALI ANWAR, OCP®
 
Receivables User Guide.pdf
Receivables User Guide.pdfReceivables User Guide.pdf
Receivables User Guide.pdf
Avijit Banerjee
 
e3222
e3222e3222
Oracle timesten
Oracle timestenOracle timesten
Oracle timesten
vn_nguyenquang
 
120finig
120finig120finig
120finig
Nimit Jain
 

Similar to Oracle database 12c 2 day + java developer's guide (15)

Platform Guide.pdf
Platform Guide.pdfPlatform Guide.pdf
Platform Guide.pdf
 
Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250Odi 12c-getting-started-guide-2032250
Odi 12c-getting-started-guide-2032250
 
Ora db 2 day+ app express dev guide
Ora db 2 day+ app express dev guideOra db 2 day+ app express dev guide
Ora db 2 day+ app express dev guide
 
PL_SQL Oracle Reference.pdf
PL_SQL Oracle Reference.pdfPL_SQL Oracle Reference.pdf
PL_SQL Oracle Reference.pdf
 
Orac appl express guide release 4
Orac appl express guide release 4Orac appl express guide release 4
Orac appl express guide release 4
 
Guia implementacion seguridad oracle 12c
Guia implementacion seguridad oracle 12cGuia implementacion seguridad oracle 12c
Guia implementacion seguridad oracle 12c
 
Oracle database 12c data masking and subsetting guide
Oracle database 12c data masking and subsetting guideOracle database 12c data masking and subsetting guide
Oracle database 12c data masking and subsetting guide
 
toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...
toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...
toaz.info-oracle-enterprise-manager-13cpdf-pr_d27a8dfef1d5b8f2ea644bed2462172...
 
Getting Started on PeopleSoft InstallationJuly 2014.docx
Getting Started on PeopleSoft InstallationJuly 2014.docxGetting Started on PeopleSoft InstallationJuly 2014.docx
Getting Started on PeopleSoft InstallationJuly 2014.docx
 
Odiun understanding oracle data integrator
Odiun understanding oracle data integratorOdiun understanding oracle data integrator
Odiun understanding oracle data integrator
 
WebLogic Scripting Tool
WebLogic Scripting ToolWebLogic Scripting Tool
WebLogic Scripting Tool
 
Receivables User Guide.pdf
Receivables User Guide.pdfReceivables User Guide.pdf
Receivables User Guide.pdf
 
e3222
e3222e3222
e3222
 
Oracle timesten
Oracle timestenOracle timesten
Oracle timesten
 
120finig
120finig120finig
120finig
 

More from bupbechanhgmail

Ibm db2 10.5 for linux, unix, and windows x query reference
Ibm db2 10.5 for linux, unix, and windows   x query referenceIbm db2 10.5 for linux, unix, and windows   x query reference
Ibm db2 10.5 for linux, unix, and windows x query reference
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows what's new for db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows   what's new for db2 version 10.5Ibm db2 10.5 for linux, unix, and windows   what's new for db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows what's new for db2 version 10.5
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows upgrading to db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows   upgrading to db2 version 10.5Ibm db2 10.5 for linux, unix, and windows   upgrading to db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows upgrading to db2 version 10.5
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows text search guide
Ibm db2 10.5 for linux, unix, and windows   text search guideIbm db2 10.5 for linux, unix, and windows   text search guide
Ibm db2 10.5 for linux, unix, and windows text search guide
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows installing ibm data server clients
Ibm db2 10.5 for linux, unix, and windows   installing ibm data server clientsIbm db2 10.5 for linux, unix, and windows   installing ibm data server clients
Ibm db2 10.5 for linux, unix, and windows installing ibm data server clients
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows getting started with db2 installa...
Ibm db2 10.5 for linux, unix, and windows   getting started with db2 installa...Ibm db2 10.5 for linux, unix, and windows   getting started with db2 installa...
Ibm db2 10.5 for linux, unix, and windows getting started with db2 installa...
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows getting started with database app...
Ibm db2 10.5 for linux, unix, and windows   getting started with database app...Ibm db2 10.5 for linux, unix, and windows   getting started with database app...
Ibm db2 10.5 for linux, unix, and windows getting started with database app...
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows developing rdf applications for i...
Ibm db2 10.5 for linux, unix, and windows   developing rdf applications for i...Ibm db2 10.5 for linux, unix, and windows   developing rdf applications for i...
Ibm db2 10.5 for linux, unix, and windows developing rdf applications for i...
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows developing perl, php, python, and...
Ibm db2 10.5 for linux, unix, and windows   developing perl, php, python, and...Ibm db2 10.5 for linux, unix, and windows   developing perl, php, python, and...
Ibm db2 10.5 for linux, unix, and windows developing perl, php, python, and...
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows developing embedded sql applications
Ibm db2 10.5 for linux, unix, and windows   developing embedded sql applicationsIbm db2 10.5 for linux, unix, and windows   developing embedded sql applications
Ibm db2 10.5 for linux, unix, and windows developing embedded sql applications
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows developing ado.net and ole db app...
Ibm db2 10.5 for linux, unix, and windows   developing ado.net and ole db app...Ibm db2 10.5 for linux, unix, and windows   developing ado.net and ole db app...
Ibm db2 10.5 for linux, unix, and windows developing ado.net and ole db app...
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows db2 connect user's guide
Ibm db2 10.5 for linux, unix, and windows   db2 connect user's guideIbm db2 10.5 for linux, unix, and windows   db2 connect user's guide
Ibm db2 10.5 for linux, unix, and windows db2 connect user's guide
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
bupbechanhgmail
 
Ibm db2 10.5 for linux, unix, and windows data movement utilities guide and...
Ibm db2 10.5 for linux, unix, and windows   data movement utilities guide and...Ibm db2 10.5 for linux, unix, and windows   data movement utilities guide and...
Ibm db2 10.5 for linux, unix, and windows data movement utilities guide and...
bupbechanhgmail
 
Db2 version 9 for linux, unix, and windows
Db2 version 9 for linux, unix, and windowsDb2 version 9 for linux, unix, and windows
Db2 version 9 for linux, unix, and windows
bupbechanhgmail
 
Reliability and performance with ibm db2 analytics accelerator
Reliability and performance with ibm db2 analytics acceleratorReliability and performance with ibm db2 analytics accelerator
Reliability and performance with ibm db2 analytics accelerator
bupbechanhgmail
 
Ibm db2 analytics accelerator high availability and disaster recovery
Ibm db2 analytics accelerator  high availability and disaster recoveryIbm db2 analytics accelerator  high availability and disaster recovery
Ibm db2 analytics accelerator high availability and disaster recovery
bupbechanhgmail
 
Db2 virtualization
Db2 virtualizationDb2 virtualization
Db2 virtualization
bupbechanhgmail
 
Db2 udb backup and recovery with ess copy services
Db2 udb backup and recovery with ess copy servicesDb2 udb backup and recovery with ess copy services
Db2 udb backup and recovery with ess copy services
bupbechanhgmail
 

More from bupbechanhgmail (20)

Ibm db2 10.5 for linux, unix, and windows x query reference
Ibm db2 10.5 for linux, unix, and windows   x query referenceIbm db2 10.5 for linux, unix, and windows   x query reference
Ibm db2 10.5 for linux, unix, and windows x query reference
 
Ibm db2 10.5 for linux, unix, and windows what's new for db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows   what's new for db2 version 10.5Ibm db2 10.5 for linux, unix, and windows   what's new for db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows what's new for db2 version 10.5
 
Ibm db2 10.5 for linux, unix, and windows upgrading to db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows   upgrading to db2 version 10.5Ibm db2 10.5 for linux, unix, and windows   upgrading to db2 version 10.5
Ibm db2 10.5 for linux, unix, and windows upgrading to db2 version 10.5
 
Ibm db2 10.5 for linux, unix, and windows text search guide
Ibm db2 10.5 for linux, unix, and windows   text search guideIbm db2 10.5 for linux, unix, and windows   text search guide
Ibm db2 10.5 for linux, unix, and windows text search guide
 
Ibm db2 10.5 for linux, unix, and windows installing ibm data server clients
Ibm db2 10.5 for linux, unix, and windows   installing ibm data server clientsIbm db2 10.5 for linux, unix, and windows   installing ibm data server clients
Ibm db2 10.5 for linux, unix, and windows installing ibm data server clients
 
Ibm db2 10.5 for linux, unix, and windows getting started with db2 installa...
Ibm db2 10.5 for linux, unix, and windows   getting started with db2 installa...Ibm db2 10.5 for linux, unix, and windows   getting started with db2 installa...
Ibm db2 10.5 for linux, unix, and windows getting started with db2 installa...
 
Ibm db2 10.5 for linux, unix, and windows getting started with database app...
Ibm db2 10.5 for linux, unix, and windows   getting started with database app...Ibm db2 10.5 for linux, unix, and windows   getting started with database app...
Ibm db2 10.5 for linux, unix, and windows getting started with database app...
 
Ibm db2 10.5 for linux, unix, and windows developing rdf applications for i...
Ibm db2 10.5 for linux, unix, and windows   developing rdf applications for i...Ibm db2 10.5 for linux, unix, and windows   developing rdf applications for i...
Ibm db2 10.5 for linux, unix, and windows developing rdf applications for i...
 
Ibm db2 10.5 for linux, unix, and windows developing perl, php, python, and...
Ibm db2 10.5 for linux, unix, and windows   developing perl, php, python, and...Ibm db2 10.5 for linux, unix, and windows   developing perl, php, python, and...
Ibm db2 10.5 for linux, unix, and windows developing perl, php, python, and...
 
Ibm db2 10.5 for linux, unix, and windows developing embedded sql applications
Ibm db2 10.5 for linux, unix, and windows   developing embedded sql applicationsIbm db2 10.5 for linux, unix, and windows   developing embedded sql applications
Ibm db2 10.5 for linux, unix, and windows developing embedded sql applications
 
Ibm db2 10.5 for linux, unix, and windows developing ado.net and ole db app...
Ibm db2 10.5 for linux, unix, and windows   developing ado.net and ole db app...Ibm db2 10.5 for linux, unix, and windows   developing ado.net and ole db app...
Ibm db2 10.5 for linux, unix, and windows developing ado.net and ole db app...
 
Ibm db2 10.5 for linux, unix, and windows db2 connect user's guide
Ibm db2 10.5 for linux, unix, and windows   db2 connect user's guideIbm db2 10.5 for linux, unix, and windows   db2 connect user's guide
Ibm db2 10.5 for linux, unix, and windows db2 connect user's guide
 
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
 
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...Ibm db2 10.5 for linux, unix, and windows   db2 connect installing and config...
Ibm db2 10.5 for linux, unix, and windows db2 connect installing and config...
 
Ibm db2 10.5 for linux, unix, and windows data movement utilities guide and...
Ibm db2 10.5 for linux, unix, and windows   data movement utilities guide and...Ibm db2 10.5 for linux, unix, and windows   data movement utilities guide and...
Ibm db2 10.5 for linux, unix, and windows data movement utilities guide and...
 
Db2 version 9 for linux, unix, and windows
Db2 version 9 for linux, unix, and windowsDb2 version 9 for linux, unix, and windows
Db2 version 9 for linux, unix, and windows
 
Reliability and performance with ibm db2 analytics accelerator
Reliability and performance with ibm db2 analytics acceleratorReliability and performance with ibm db2 analytics accelerator
Reliability and performance with ibm db2 analytics accelerator
 
Ibm db2 analytics accelerator high availability and disaster recovery
Ibm db2 analytics accelerator  high availability and disaster recoveryIbm db2 analytics accelerator  high availability and disaster recovery
Ibm db2 analytics accelerator high availability and disaster recovery
 
Db2 virtualization
Db2 virtualizationDb2 virtualization
Db2 virtualization
 
Db2 udb backup and recovery with ess copy services
Db2 udb backup and recovery with ess copy servicesDb2 udb backup and recovery with ess copy services
Db2 udb backup and recovery with ess copy services
 

Recently uploaded

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 

Oracle database 12c 2 day + java developer's guide

  • 1. Oracle® Database 2 Day + Java Developer’s Guide 12c Release 1 (12.1) E17944-10 February 2014
  • 2. Oracle Database 2 Day + Java Developer's Guide, 12c Release 1 (12.1) E17944-10 Copyright © 2007, 2014, Oracle and/or its affiliates. All rights reserved. Primary Authors: Tulika Das, Maitreyee Chaliha, Deepa Aswani, Rosslynne Hefferan Contributing Authors: Kathleen Heap, Simon Law Contributor: The Oracle Database 12c documentation is dedicated to Mark Townsend, who was an inspiration to all who worked on this release. Contributors: Kuassi Mensah, Chris Schalk, Christian Bauwens, Mark Townsend, Paul Lo, Venkatasubramaniam Iyer, Yuri Dolgov This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government. This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group. This software or hardware and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services.
  • 3. iii Contents Preface............................................................................................................................................................... xiii Audience..................................................................................................................................................... xiii Documentation Accessibility................................................................................................................... xiii Related Documents ................................................................................................................................... xiii Conventions ............................................................................................................................................... xiv 1 Using Java with Oracle Database Using Java to Connect to Oracle Database 12c Release 1 (12.1)....................................................... 1-1 Oracle JDBC Thin Driver................................................................................................................... 1-2 Oracle JDBC Packages ....................................................................................................................... 1-2 Using JDeveloper to Create JDBC Applications................................................................................ 1-2 JDeveloper User Interface ................................................................................................................. 1-3 JDeveloper Tools ................................................................................................................................ 1-4 Overview of Sample Java Application................................................................................................. 1-4 Resources ................................................................................................................................................... 1-6 2 Getting Started with the Application What You Need to Install........................................................................................................................ 2-1 Oracle Database 12c Release 1 (12.1) ............................................................................................... 2-1 Unlocking the HR Schema for the JDBC Application ........................................................... 2-1 J2SE or JDK.......................................................................................................................................... 2-2 Integrated Development Environment........................................................................................... 2-2 Web Server .......................................................................................................................................... 2-3 Verifying the Oracle Database 12c Release 1 (12.1) Installation..................................................... 2-3 Checking Installed Directories and Files ........................................................................................ 2-3 Checking the Environment Variables ............................................................................................. 2-4 Determining the JDBC Driver Version............................................................................................ 2-4 Installing Oracle JDeveloper ................................................................................................................. 2-5 3 Connecting to Oracle Database 12c Release 1 (12.1) Connecting to Oracle Database from JDeveloper.............................................................................. 3-1 JDeveloper Database Navigator....................................................................................................... 3-1 Creating a Database Connection...................................................................................................... 3-2 Browsing the Data Using the Database Navigator ....................................................................... 3-3 Setting Up Applications and Projects in JDeveloper........................................................................ 3-4
  • 4. iv Using the JDeveloper Application Navigator................................................................................ 3-5 Creating an Application and a Project ............................................................................................ 3-5 Viewing the Javadoc and Source Code Available in the Project Scope .................................... 3-5 Connecting to Oracle Database from a Java Application................................................................. 3-6 Overview of Connecting to Oracle Database................................................................................. 3-6 Specifying Database URLs................................................................................................................ 3-7 Using the Default Service Feature of the Oracle Database ................................................... 3-8 Creating a Java Class in JDeveloper................................................................................................ 3-9 Java Libraries ................................................................................................................................... 3-10 Overview of the Oracle JDBC Library .................................................................................. 3-10 Overview of the JSP Runtime Library .................................................................................. 3-10 Adding JDBC and JSP Libraries.................................................................................................... 3-10 Importing JDBC Packages.............................................................................................................. 3-11 Declaring Connection-Related Variables..................................................................................... 3-11 Creating the Connection Method ................................................................................................. 3-12 4 Querying for and Displaying Data Overview of Querying for Data in Oracle Database......................................................................... 4-1 SQL Statements................................................................................................................................... 4-1 Query Methods for the Statement Object ....................................................................................... 4-2 Result Sets ........................................................................................................................................... 4-2 Features of ResultSet Objects .................................................................................................... 4-3 Summary of Result Set Object Types ....................................................................................... 4-3 Querying Data from a Java Application .............................................................................................. 4-4 Creating a Method in JDeveloper to Query Data.......................................................................... 4-4 Testing the Connection and the Query Methods .......................................................................... 4-5 Creating JSP Pages ................................................................................................................................... 4-7 Overview of Page Presentation........................................................................................................ 4-7 JSP Tags ........................................................................................................................................ 4-8 Scriptlets....................................................................................................................................... 4-8 HTML Tags.................................................................................................................................. 4-8 HTML Forms ............................................................................................................................... 4-8 Creating a Simple JSP Page............................................................................................................... 4-9 Adding Static Content to a JSP Page ............................................................................................... 4-9 Adding a Style Sheet to a JSP Page............................................................................................... 4-11 Adding Dynamic Content to the JSP Page: Database Query Results ......................................... 4-13 Adding a JSP useBean Tag to Initialize the DataHandler Class............................................... 4-13 Creating a Result Set....................................................................................................................... 4-14 Adding a Table to the JSP Page to Display the Result Set......................................................... 4-16 Filtering a Query Result Set................................................................................................................ 4-18 Creating a Java Method for Filtering Results............................................................................. 4-18 Testing the Query Filter Method .................................................................................................. 4-19 Adding Filter Controls to the JSP Page........................................................................................ 4-20 Displaying Filtered Data in the JSP Page..................................................................................... 4-21 Adding Login Functionality to the Application.............................................................................. 4-22 Creating a Method to Authenticate Users................................................................................... 4-22 Creating a Login Page .................................................................................................................... 4-24
  • 5. v Preparing Error Reports for Failed Logins.................................................................................. 4-25 Creating the Login Interface.......................................................................................................... 4-25 Creating a JSP Page to Handle Login Action.............................................................................. 4-26 Testing the JSP Page ............................................................................................................................. 4-27 5 Updating Data Creating a JavaBean ................................................................................................................................. 5-1 Creating a JavaBean in JDeveloper.................................................................................................. 5-1 Defining the JavaBean Properties and Methods............................................................................ 5-2 Updating Data from a Java Class .......................................................................................................... 5-4 Creating a Method to Identify an Employee Record .................................................................... 5-5 Creating a Method to Update Employee Data............................................................................... 5-5 Adding a Link to Navigate to an Update Page.............................................................................. 5-8 Creating a JSP Page to Edit Employee Data................................................................................ 5-10 Creating a JSP Page to Handle an Update Action...................................................................... 5-11 Inserting an Employee Record............................................................................................................ 5-12 Creating a Method to Insert Data ................................................................................................. 5-12 Adding a Link to Navigate to an Insert Page.............................................................................. 5-14 Creating a JSP Page to Enter New Data....................................................................................... 5-14 Creating a JSP Page to Handle an Insert Action......................................................................... 5-16 Deleting an Employee Record ............................................................................................................ 5-17 Creating a Method for Deleting Data........................................................................................... 5-17 Adding a Link to Delete an Employee......................................................................................... 5-18 Creating a JSP Page to Handle a Delete Action.......................................................................... 5-19 Exception Handling .............................................................................................................................. 5-19 Adding Exception Handling to Java Methods............................................................................ 5-20 Creating a Method for Handling Any SQLException ............................................................... 5-21 Navigation in the Sample Application ............................................................................................. 5-21 Creating a Starting Page for an Application ............................................................................... 5-22 6 Enhancing the Application: Advanced JDBC Features Using Dynamic SQL ................................................................................................................................ 6-1 Using OraclePreparedStatement...................................................................................................... 6-1 Using OracleCallableStatement ....................................................................................................... 6-2 Using Bind Variables ......................................................................................................................... 6-2 Calling Stored Procedures ...................................................................................................................... 6-3 Creating a PL/SQL Stored Procedure in JDeveloper ................................................................... 6-4 Creating a Method to Use the Stored Procedure........................................................................... 6-5 Enabling Users to Choose the Stored Procedure........................................................................... 6-6 Calling the Stored Procedure from the Application ..................................................................... 6-8 Using Cursor Variables........................................................................................................................... 6-9 Oracle REF CURSOR Type Category........................................................................................... 6-10 Accessing REF CURSOR Data....................................................................................................... 6-10 Using REF CURSOR in the Sample Application ........................................................................ 6-11 Creating a Package in the Database ...................................................................................... 6-11 Creating a Database Function ................................................................................................ 6-11
  • 6. vi Calling the REF CURSOR from a Method............................................................................ 6-12 Displaying a Dynamically Generated List ........................................................................... 6-13 7 Getting Unconnected from Oracle Database 12c Release 1 (12.1) Creating a Method to Close All Open Objects................................................................................... 7-1 Closing Open Objects in the Application ........................................................................................... 7-2 8 Creating a Universal Connection Pool Setting JDeveloper Project Properties.................................................................................................. 8-1 Creating the Stock Ticker Application................................................................................................. 8-3 Observing the Output ............................................................................................................................. 8-6 9 Building Global Applications Developing Locale Awareness............................................................................................................... 9-1 Mapping Between Oracle and Java Locales................................................................................... 9-2 Determining User Locales ...................................................................................................................... 9-3 Locale Awareness in Java Applications.......................................................................................... 9-3 Encoding HTML Pages............................................................................................................................ 9-3 Specifying the Page Encoding for HTML Pages............................................................................ 9-4 Specifying the Page Encoding in Java Servlets and JSP Pages .................................................... 9-4 Organizing the Content of HTML Pages for Translation ................................................................ 9-5 Strings in Java Servlets and JSP Pages ............................................................................................ 9-5 Static Files............................................................................................................................................ 9-6 Data from the Database..................................................................................................................... 9-6 Presenting Data by User Locale Convention ...................................................................................... 9-6 Oracle Date Formats .......................................................................................................................... 9-7 Oracle Number Formats.................................................................................................................... 9-7 Oracle Linguistic Sorts....................................................................................................................... 9-8 Oracle Error Messages....................................................................................................................... 9-9 Localizing Text on JSP Pages in JDeveloper....................................................................................... 9-9 Creating a Resource Bundle .......................................................................................................... 9-10 Using Resource Bundle Text on JSP Pages.................................................................................. 9-11 Index
  • 7. vii
  • 8. viii List of Examples 2–1 Determining the JDBC Driver Version .................................................................................... 2-4 3–1 Specifying the url Property for the DataSource Object ......................................................... 3-8 3–2 Default Service Configuration in listener.ora ........................................................................ 3-8 3–3 Importing Packages in a Java Application........................................................................... 3-11 3–4 Declaring Connection Variables and the Connection Object ............................................ 3-12 3–5 Adding a Method to Connect to the Database .................................................................... 3-13 4–1 Creating a Statement Object ...................................................................................................... 4-2 4–2 Declaring a Scroll-Sensitive, Read-Only ResultSet Object.................................................... 4-3 4–3 Using the Connection, Statement, Query, and ResultSet Objects........................................ 4-5 4–4 Implementing User Validation .............................................................................................. 4-23 5–1 Skeleton Code for a Basic Java Bean with Accessor Methods.............................................. 5-3 5–2 Method for Updating a Database Record................................................................................ 5-7 5–3 Method for Adding a New Employee Record..................................................................... 5-13 5–4 Method for Deleting an Employee Record........................................................................... 5-18 5–5 Adding a Method to Handle Any SQLException in the Application .............................. 5-21 6–1 Creating a PreparedStatement .................................................................................................. 6-2 6–2 Creating a CallableStatement.................................................................................................... 6-2 6–3 Calling Stored Procedures ......................................................................................................... 6-3 6–4 Creating a Stored Function........................................................................................................ 6-3 6–5 Calling a Stored Function in Java ............................................................................................. 6-3 6–6 Creating a PL/SQL Stored Procedure to Insert Employee Data.......................................... 6-4 6–7 Using PL/SQL Stored Procedures in Java .............................................................................. 6-6 6–8 Declaring a REF CURSOR Type ............................................................................................ 6-10 6–9 Accessing REF Cursor Data in Java....................................................................................... 6-10 6–10 Creating a Package in the Database ...................................................................................... 6-11 6–11 Creating a Stored Function..................................................................................................... 6-12 7–1 Creating a Method to Close All Open Objects........................................................................ 7-2 9–1 Mapping from a Java Locale to an Oracle Language and Territory.................................... 9-2 9–2 Determining User Locale in Java Using the Accept-Language Header.............................. 9-3 9–3 Explicitly Specifying User Locale in Java ................................................................................ 9-3 9–4 Specifying Page Encoding in the HTTP Specification ........................................................... 9-4 9–5 Specifying Page Encoding on an HTML Page........................................................................ 9-4 9–6 Specifying Page Encoding in Servlets Using setContentType ............................................. 9-5 9–7 Difference Between Date Formats by Locale (United States and Germany)...................... 9-7 9–8 Difference Between Number Formats by Locale (United States and Germany)............... 9-8 9–9 Variations in Linguistic Sorting (Binary and Spanish).......................................................... 9-8 9–10 Creating a Resource Bundle Class......................................................................................... 9-11
  • 9. ix List of Figures 1–1 JDeveloper User Interface.......................................................................................................... 1-3 1–2 Web Pages in the Sample Application..................................................................................... 1-5 3–1 Specifying Connection Details .................................................................................................. 3-3 3–2 Viewing the Table Structure and Data..................................................................................... 3-4 3–3 Selecting the Class to View the Javadoc in JDeveloper......................................................... 3-6 3–4 Creating a Java Class .................................................................................................................. 3-9 3–5 Java Source Editor.................................................................................................................... 3-10 3–6 Importing Libraries.................................................................................................................. 3-11 3–7 Java Code Insight..................................................................................................................... 3-13 4–1 Test Output for Query Method in Log Window.................................................................... 4-7 4–2 Creating a JSP Page..................................................................................................................... 4-9 4–3 Adding Content to JSP Pages in the JDeveloper Visual Source Editor............................ 4-10 4–4 Formatting a JSP....................................................................................................................... 4-11 4–5 Selecting a CSS File for the JSP............................................................................................... 4-12 4–6 Adding Static Content to the JSP Page ................................................................................. 4-12 4–7 useBean Representation in the employees.jsp File.............................................................. 4-13 4–8 Preferences Window to Select JSP and HTML Visual Editor Options............................. 4-14 4–9 Scriptlet Representation in a JSP Page .................................................................................. 4-15 4–10 Viewing Errors in the Structure Window............................................................................. 4-15 4–11 Importing Packages in JDeveloper........................................................................................ 4-16 4–12 Common HTML Components in the Component Palette................................................. 4-17 4–13 Table in a JSP Page................................................................................................................... 4-18 4–14 HTML Form Components in the JSP Page........................................................................... 4-21 4–15 Using the Scriptlet Properties Dialog Box............................................................................ 4-22 4–16 Project CSS Files Panel ............................................................................................................ 4-24 4–17 Login Page................................................................................................................................. 4-26 4–18 Login Page for Sample Application in the Browser............................................................ 4-28 4–19 Unfiltered Employee Data in employee.jsp ......................................................................... 4-29 4–20 Filtered Employee Data in employee.jsp.............................................................................. 4-29 5–1 Creating a Java Bean................................................................................................................... 5-2 5–2 Generate Accessors Dialog Box ................................................................................................ 5-3 5–3 Link to Edit Employees in employees.jsp................................................................................ 5-9 5–4 Creating a JSP Page to Edit Employee Details..................................................................... 5-11 5–5 Editing Employee Data ........................................................................................................... 5-12 5–6 Form to Insert Employee Data ............................................................................................... 5-15 5–7 Inserting New Employee Data............................................................................................... 5-16 5–8 Inserting Employee Data ........................................................................................................ 5-17 5–9 Link for Deleting an Employee from employees.jsp .......................................................... 5-19 6–1 Adding a Link to Provide the Stored Procedure Option ...................................................... 6-8 6–2 Using Stored Procedures to Enter Records ............................................................................. 6-9 6–3 Structure View of ListBox Options........................................................................................ 6-14 6–4 Dynamically Generated List in Browser............................................................................... 6-15 8–1 Setting the Default Project Properties...................................................................................... 8-2 8–2 Setting the Libraries and Classpath.......................................................................................... 8-2 8–3 The Add Archive or Directory Screen ..................................................................................... 8-3 8–4 The Application Navigator Panel Options.............................................................................. 8-3 8–5 The DbConfig.properties File in the Code Editor .................................................................. 8-4 8–6 Adding a Source File to a Project.............................................................................................. 8-4 8–7 The Add to Project Content....................................................................................................... 8-4 8–8 The Run Menu Options.............................................................................................................. 8-5 8–9 The Project Properties Screen for the StockTickerProj Project ............................................. 8-5 8–10 The Edit Run Configuration Screen ......................................................................................... 8-6 8–11 Output of the Stock Ticker Application in JDeveloper Log.................................................. 8-6 8–12 Retrieving Information from the Universal Connection Pool Using the Browser ............ 8-7
  • 10. x 8–13 The UCP Properties page........................................................................................................... 8-7 8–14 The Stock Ticker Page on the Browser..................................................................................... 8-8 8–15 The UCP Statistics Page on the Browser.................................................................................. 8-8 8–16 The UCP Properties Update Page on the Browser................................................................. 8-9 8–17 The Client Simulator Page on the Browser ............................................................................. 8-9
  • 11. xi
  • 12. xii List of Tables 2–1 Directories and Files in the ORACLE_HOME Directory..................................................... 2-3 3–1 Standard Data Source Properties............................................................................................. 3-7 4–1 Key Query Execution Methods for java.sql.Statement......................................................... 4-2 9–1 Locale Representation in Java, SQL, and PL/SQL Programming Environments............ 9-2
  • 13. xiii Preface This Preface introduces you to Oracle Database 2 Day + Java Developer's Guide, by discussing the intended audience and conventions of this document. It also includes a list of related Oracle documents that you can refer to for more information. Audience This guide is intended for application developers using Java to access and modify data in Oracle Database. This guide illustrates how to perform these tasks using a simple Java Database Connectivity (JDBC) application. This guide uses the Oracle JDeveloper integrated development environment (IDE) to create the application. This guide can be read by anyone with an interest in Java programming, but it assumes at least some prior knowledge of the following: ■ Java ■ Oracle PL/SQL ■ Oracle databases Documentation Accessibility For information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc. Access to Oracle Support Oracle customers have access to electronic support through My Oracle Support. For information, visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=info or visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=trs if you are hearing impaired. Related Documents For more information, see the following documents in the Oracle Database documentation set: ■ Oracle® Fusion Middleware Installation Guide for Oracle JDeveloper, 11g Release 1 (11.1.1) and JDeveloper Online Documentation on Oracle Technology Network at http://www.oracle.com/technology/documentation/jdev.html ■ Oracle Database JDBC Developer's Guide ■ Oracle Database Java Developer's Guide
  • 14. xiv ■ Oracle Universal Connection Pool for JDBC Developer's Guide Conventions The following text conventions are used in this document: Convention Meaning boldface Boldface type indicates graphical user interface elements associated with an action, or terms defined in text or the glossary. italic Italic type indicates book titles, emphasis, or placeholder variables for which you supply particular values. monospace Monospace type indicates commands within a paragraph, URLs, code in examples, text that appears on the screen, or text that you enter.
  • 15. 1 Using Java with Oracle Database 1-1 1 Using Java with Oracle Database Oracle Database is a relational database that you can use to store, use, and modify data. The Java Database Connectivity (JDBC) standard is used by Java applications to access and manipulate data in relational databases. JDBC is an industry-standard application programming interface (API) that lets you access a RDBMS using SQL from Java. JDBC is based on the X/Open SQL Call Level Interface (CLI) and complies with the Entry Level of the JDBC escape standard. Each vendor implements the JDBC Specification with its own extensions. This guide shows you how to use a simple Java application to connect to Oracle Database 12c Release 1 (12.1) and access and modify data within the database. This chapter introduces you to the Java application created in this guide, and to the tools you can use to develop the Java application in the following topics: ■ Using Java to Connect to Oracle Database 12c Release 1 (12.1) ■ Using JDeveloper to Create JDBC Applications ■ Overview of Sample Java Application Using Java to Connect to Oracle Database 12c Release 1 (12.1) JDBC is a database access protocol that enables you connect to a database and run SQL statements and queries on the database. The core Java class libraries provide the JDBC APIs, java.sql and javax.sql. However, JDBC is designed to allow vendors to supply drivers that offer the necessary specialization for a particular database. The following sections describe Oracle support for the JDBC standard: ■ Oracle JDBC Thin Driver ■ Oracle JDBC Packages See Also: http://www.oracle.com/technetwork/java/overview-141217.html Note: Oracle Database 12c Release 1 (12.1) supports JDK 6 and JDK 7.
  • 16. Using JDeveloper to Create JDBC Applications 1-2 Oracle Database 2 Day + Java Developer's Guide Oracle JDBC Thin Driver Oracle recommends using the JDBC Thin Driver for most requirements. JDBC-OCI is only needed for OCI-specific features. The Thin driver will work on any system that has a suitable Java virtual machine (JVM). The JDBC Thin Driver is a pure Java, Type IV driver. It supports the JavaTM 2 Platform Standard Edition 5.0, also known as Java Development Kit (JDK) 5. It also includes support for JDK 6. It is platform-independent and does not require any additional Oracle software for client-side application development. The JDBC Thin Driver communicates with the server using SQL*Net to access Oracle Database 12c Release 1 (12.1). You can access the Oracle-specific JDBC features and the standard features by using the oracle.jdbc package. Oracle JDBC Packages Oracle support for the JDBC API is provided through the oracle.jdbc and oracle.sql packages. These packages support all Java Development Kit (JDK) releases from 1.5 through 1.6. oracle.sql The oracle.sql package supports direct access to data in SQL format. This package consists primarily of classes that provide Java mappings to SQL data types and their support classes. Essentially, the classes act as Java wrappers for SQL data. The characters are converted to Java chars and, then, to bytes in the UCS-2 character set. Each of the oracle.sql.* data type classes extends oracle.sql.Datum, a superclass that includes functions and features common to all the data types. Some of the classes are for JDBC 2.0-compliant data types. In addition to data type classes, the oracle.sql package supports classes and interfaces for use with objects and collections. oracle.jdbc The interfaces of the oracle.jdbc package define the Oracle extensions to the interfaces in the java.sql package. These extensions provide access to Oracle SQL-format data. They also provide access to other Oracle-specific features, including Oracle performance enhancements. The key classes and interfaces of this package provide methods that support standard JDBC features and perform tasks such as: ■ Returning Oracle statement objects ■ Setting Oracle performance extensions for any statement ■ Binding oracle.sql.* types into prepared and callable statements ■ Retrieving data in oracle.sql format ■ Getting meta information about the database and result sets ■ Defining integer constants used to identify SQL types Using JDeveloper to Create JDBC Applications The Java application tutorial in this guide uses Oracle JDeveloper release 11.1.1 as the integrated development environment (IDE) for developing the Java application and creating Web pages for users to view and change the data.
  • 17. Using JDeveloper to Create JDBC Applications Using Java with Oracle Database 1-3 Oracle JDeveloper is an IDE with support for modeling, developing, debugging, optimizing, and deploying Java applications and Web services. JDeveloper provides features for you to write and test Java programs that access the database with SQL statements embedded in Java programs. For the database, JDeveloper provides functions and features to do the following: ■ Create a connection to a database ■ Browse database objects ■ Create, edit, or delete database objects ■ Create and edit PL/SQL functions, procedures, and packages JDeveloper User Interface Oracle JDeveloper is an IDE that uses windows for various application development tools. You can display or hide any of the windows, and you can dock them or undock them to create a desktop suited to your method of working. In addition to these tools, JDeveloper provides a range of navigators to help you organize and view the contents of your projects. Application and System navigators show you the files in your projects, and a Structure window shows you the structure of individual items. You can arrange the windows as you choose, and can close and open them from the View menu. Figure 1–1 shows the default layout of some of the available navigators, palettes, and work areas in the JDeveloper user interface (GUI). Figure 1–1 JDeveloper User Interface See Also: Working with Windows in the IDE, in the JDeveloper online Help Java Source Editor/ Visual Editor Application Navigator Connections Navigator Component Palette Property Inspector Runtime Messages Structure Window
  • 18. Overview of Sample Java Application 1-4 Oracle Database 2 Day + Java Developer's Guide JDeveloper Tools For creating a Java application, JDeveloper provides the following tools to simplify the process: ■ Structure window, which provides a tree view of all of the elements in the application currently being edited be it Java, XML, or JSP/HTML. ■ Java Visual Editor, which you can use to assemble the elements of a user interface quickly and easily. ■ JSP/HTML Visual Editor, which you can use to visually edit HTML and JSP pages. ■ Java Source Editor, which provides extensive features for helping in writing the Java code, such as distinctive highlighting for syntax and semantic errors, assistance for adding and sorting import statements, the Java Code Insight feature, and code templates. ■ Component Palette, from which you select the user interface components, such as buttons and text areas, that you want to display on your pages. ■ Property Inspector, which gives a simple way of setting properties of items such as user interface components. Refer to Figure 1–1 to get a better idea of these tools. Overview of Sample Java Application This guide shows you how to create an application using Java, JDBC and Oracle ADF. In this application, you build in functions and features that: 1. Allow users to log in and validate the user name and password. 2. Establish a connection to the database. 3. Query the database for data and retrieve the data using a JavaBean. 4. Display the data using JavaServer Pages (JSP) technology. 5. Allow users to insert, update, or delete records. 6. Access and modify information from a master-detail application. 7. Handle exceptions. Overview of Application Web Pages (JSP Pages) Figure 1–2 shows the relationships among the pages developed for this application. Note: The Java Code Insight feature is a facility that provides context-specific, intelligent input when creating code in the Java Source Editor. In this guide, you will see many instances of how you can use Java Code Insight to insert code. Note: The application connects to the HR schema that ships with Oracle Database 12c Release 1 (12.1).
  • 19. Overview of Sample Java Application Using Java with Oracle Database 1-5 Figure 1–2 Web Pages in the Sample Application A brief description of the Web pages in the sample application follows: ■ index.jsp This is the starting page of the application. It automatically forwards the user to the login page of the application, login.jsp. ■ login.jsp This page allows users to log in to the application. The user name, password, and host information are validated and used to create the connection descriptor to log in to the database. ■ login_action.jsp This is a nonviewable page that handles the authentication of the user-supplied login details from login.jsp. If authentication is successful, the page forwards the user to employees.jsp. Otherwise, it redisplays the login.jsp page including a message. ■ employees.jsp This is the main page of the application. It displays a list of all the employees in the HR schema for AnyCo Corporation and allows the user to filter the list of employees using any string. It also includes links to add, edit, and delete any user data. These actions, however, are handled by other JSP pages that are created specifically for each of these tasks. ■ insert.jsp The link to insert employee data on the employees.jsp page redirects the user to this page. This includes a form that accepts all the details for a new employee record. The details entered on this form are processed by the insert_action.jsp page. ■ insert_action.jsp This is a nonviewable page that handles the insertion of data for a new employee that is entered on the insert.jsp page. ■ edit.jsp employees.jsp Displays a table of employees. Contains : • a field for filtering the list of employees • a link to add an employee • links to edit employee rows • links to delete employee rows index.jsp login.jsp login_action.jsp insert.jsp insert_action.jsp edit_emp.jsp update_action.jsp delete_action.jsp
  • 20. Resources 1-6 Oracle Database 2 Day + Java Developer's Guide The link to edit employee data on the employees.jsp page redirects the user to this page. This form displays current data of a single employee in text fields, and the user can edit this information. ■ update_action.jsp The submit action on the edit.jsp page directs the data to this nonviewable page, which inserts the edited data into the database. ■ delete_action.jsp The link to delete an employee record on the employees.jsp page is handled by this nonviewable page, which deletes the employee data and forwards the user back to the employees.jsp page. Classes The sample application includes the following classes: ■ DataHandler.java This class contains all the methods that are used to implement the important functions of the sample application. It includes methods that validate user credentials, connect to the database, retrieve employee data with and without filters, insert data, update data, handle exceptions, and so on. ■ Employees.java This class is a JavaBean that holds a single employee record. It contains accessor methods to get and set the values of each of the record fields. It also contains accessor methods to retrieve and modify employee records. ■ JavaClient.java This class is used only for testing the DataHandler class. Resources For more information about Oracle Database 12c Release 1: ■ Visit the Oracle Technology Network page http://www.oracle.com/technetwork/index.html ■ Visit the Oracle Database 12c Release 1 Online Documentation Library by performing the following steps: – Click Start and then Programs. – Select Oracle Database 12c Release 1, then Get Help, and then Read Documentation. ■ Visit the Oracle Database 12c Release 1 Online Discussion forum by performing the following steps: – Click Start and then Programs. – Select Oracle Database 12c Release 1, then Get Help, and then Go To Online Forum. Note: This application is developed throughout this guide in the form of a tutorial. It is recommended, therefore, that you read these chapters in sequence.
  • 21. 2 Getting Started with the Application 2-1 2 Getting Started with the Application To develop a Java application that connects to Oracle Database 12c Release 1 (12.1), you must ensure that certain components are installed as required. This chapter covers the following topics: ■ What You Need to Install ■ Verifying the Oracle Database 12c Release 1 (12.1) Installation ■ Installing Oracle JDeveloper What You Need to Install To be able to develop the sample application, you need to install the following products and components: ■ Oracle Database 12c Release 1 (12.1) ■ J2SE or JDK ■ Integrated Development Environment ■ Web Server The following subsections describe these requirements in detail. Oracle Database 12c Release 1 (12.1) To develop the Java application, you need a working installation of Oracle Database 12c Release 1 (12.1) Server with the HR schema, which comes with the database. The installation creates an instance of Oracle Database 12c Release 1 (12.1) and provides additional tools for managing this database. For more information, refer to the following Oracle Database 12c Release 1 (12.1) installation guides and release notes: ■ Oracle Database Installation Guide for Linux ■ Oracle Database Installation Guide for Microsoft Windows Unlocking the HR Schema for the JDBC Application The HR user account, which owns the sample HR schema used for the Java application in this guide, is initially locked. You must log in as a user with administrative privileges (SYS) and unlock the account before you can log in as HR. If the database is locally installed, use the Run SQL Command Line to unlock the account as follows:
  • 22. What You Need to Install 2-2 Oracle Database 2 Day + Java Developer's Guide 1. To access the Run SQL Command Line, from the Start menu, select Programs (or All Programs), then Oracle Database 12c Release 1 (12.1), and then click Run SQL Command Line. Log in as a user with DBA privileges, for example: > CONNECT SYS AS SYSDBA; Enter password: password 2. Run the following command: > ALTER USER HR ACCOUNT UNLOCK; or, > ALTER USER HR IDENTIFIED BY HR; 3. Test the connection as follows: > CONNECT HR Enter password: password You should see a message indicating that you have connected to the database. In addition, some of the constraints and triggers present in the HR schema are not in line with the scope of the Java application created in this guide. You must remove these constraints and triggers as follows using the following SQL statements: DROP TRIGGER HR.UPDATE_JOB_HISTORY; DROP TRIGGER HR.SECURE_EMPLOYEES; DELETE FROM JOB_HISTORY; J2SE or JDK To create and compile Java applications, you need the full Java 2 Platform, Standard Edition, Software Development Kit (J2SE SDK), formerly known as the Java Development Kit (JDK). You also need the Java Runtime Environment (JRE). Integrated Development Environment For ease in developing the application, you can choose to develop your application in an integrated development environment (IDE). This guide uses Oracle JDeveloper to create the files for this application. For more information about installing JDeveloper, refer to Installing Oracle JDeveloper. Note: For information about creating and using secure passwords with Oracle Database 12c Release 1 (12.1), refer to Oracle Database Security Guide. Note: Oracle Database 12c Release 1 (12.1) supports JDK 6 and JDK 7. See Also: ■ http://www.oracle.com/technetwork/java/javase/downloads/ind ex.html for information about installing Java ■ http://www.oracle.com/technetwork/java/overview-141217.html for information about the JDBC API
  • 23. Verifying the Oracle Database 12c Release 1 (12.1) Installation Getting Started with the Application 2-3 Web Server The sample application developed in this guide uses JavaServer Pages (JSP) technology to display information and accept input from users. To deploy these pages, you need a Web server with a servlet and JSP container, such as the Apache Tomcat application server. This guide uses the embedded server called the Oracle WebLogic Server in JDeveloper for deploying the JSP pages. If you choose not to install Oracle JDeveloper, then any Web server that enables you to deploy JSP pages should suffice. JDeveloper supports direct deployment to the following production application servers: ■ Oracle WebLogic Server ■ Oracle Application Server ■ Apache Tomcat ■ IBM WebSphere ■ JBoss For more information about these servers, please refer to vendor-specific documentation. Verifying the Oracle Database 12c Release 1 (12.1) Installation Oracle Database 12c Release 1 (12.1) installation is platform-specific. You must verify that the installation was successful before you proceed to create the sample application. This section describes the steps for verifying an Oracle Database 12c Release 1 (12.1) installation. Verifying a installation involves the following tasks: ■ Checking Installed Directories and Files ■ Checking the Environment Variables ■ Determining the JDBC Driver Version Checking Installed Directories and Files Installing Oracle Java products creates the following directories: ■ ORACLE_HOME/jdbc ■ ORACLE_HOME /jlib Check if the directories described in Table 2–1 have been created and populated in the ORACLE_HOME directory. Table 2–1 Directories and Files in the ORACLE_HOME Directory Directory Description $OH/jdbc/lib The lib directory contains the ojdbc6.jar and ojdbc7.jar required Java classes. These contain the JDBC driver classes for use with JDK 6 and JDK 7. $OH/jdbc/Readme.txt This file contains late-breaking and release-specific information about the drivers, which may not have been included in other documentation on the product.
  • 24. Verifying the Oracle Database 12c Release 1 (12.1) Installation 2-4 Oracle Database 2 Day + Java Developer's Guide Checking the Environment Variables This section describes the environment variables that must be set for the JDBC Thin Driver. You must set the classpath for your installed JDBC Thin Driver. For JDK 6, you must set the following values for the CLASSPATH variable: ORACLE_HOME/jdbc/lib/ojdbc6.jar ORACLE_HOME/jlib/orai18n.jar For JDK 7, you must set the following values for the CLASSPATH variable: ORACLE_HOME/jdbc/lib/ojdbc7.jar ORACLE_HOME/jlib/orai18n.jar Ensure that there is only one JDBC class file, such as ojdbc6.jar, and one globalization classes file, orai18n.jar, in the CLASSPATH variable. Determining the JDBC Driver Version Starting from Oracle Database 12c Release 1 (12.1), you can get details about the JDBC support in the database as follows: > java -jar ojdbc6.jar Oracle 12.1.0.0. JDBC 4.0 compiled with JDK6 In addition, you can determine the version of the JDBC driver that you installed by calling the getDriverVersion method of the OracleDatabaseMetaData class. Example 2–1 illustrates how to determine the driver version: Example 2–1 Determining the JDBC Driver Version import java.sql.*; import oracle.jdbc.*; import oracle.jdbc.pool.OracleDataSource; class JDBCVersion { public static void main (String args[]) throws SQLException { OracleDataSource ods = new OracleDataSource(); ods.setURL("jdbc:oracle:thin:hr/hr@localhost:1521/oracle"); Connection conn = ods.getConnection(); $OH/jlib This directory contains the orai18n.jar file. This file contains classes for globalization and multibyte character sets support. Note: Use the ojdbcn.jar file (where ’n’ is the release number) supplied with Oracle Database Installation Guide, or Oracle Database Client Installation Guide. Note: The JDBC Thin Driver requires a TCP/IP listener to be running on the computer where the database is installed. Table 2–1 (Cont.) Directories and Files in the ORACLE_HOME Directory Directory Description
  • 25. Installing Oracle JDeveloper Getting Started with the Application 2-5 // Create Oracle DatabaseMetaData object DatabaseMetaData meta = conn.getMetaData(); // gets driver info: System.out.println("JDBC driver version is " + meta.getDriverVersion()); } } Installing Oracle JDeveloper In this guide, the integrated development environment (IDE) that is used to create the sample Java application using JDBC is Oracle JDeveloper release 11.1.1. This release of JDeveloper is supported on the Microsoft Windows Vista, Windows XP, Windows 2003, Windows 2000, Linux, and Mac OS X operating systems. Installation of the latest version of JDeveloper is described in detail in Installation Guide for Oracle JDeveloper, which is available online on the Oracle Technology Network at http://download.oracle.com/docs/cd/E12839_01/install.1111/e13666/toc.htm Also read JDeveloper 11g Release Notes, which is available online on the Oracle Technology Network at http://www.oracle.com/technetwork/developer-tools/jdev/overview/index.html
  • 26. Installing Oracle JDeveloper 2-6 Oracle Database 2 Day + Java Developer's Guide
  • 27. 3 Connecting to Oracle Database 12c Release 1 (12.1) 3-1 3 Connecting to Oracle Database 12c Release 1 (12.1) This chapter is the first in a series of five chapters, each of which describes how to create parts of a Java application that accesses Oracle Database 12c Release 1 (12.1) and displays, modifies, deletes, and updates data on it. To be able to access the database from a Java application, you must connect to the database using a java.sql.Connection object. This chapter includes the following sections: ■ Connecting to Oracle Database from JDeveloper ■ Setting Up Applications and Projects in JDeveloper ■ Connecting to Oracle Database from a Java Application Connecting to Oracle Database from JDeveloper You can set up and manage database connections in JDeveloper to enable your application to communicate with external data sources, including Oracle Database 12c Release 1 (12.1) and offline database objects. This is done using the Database Navigator. The same navigator is also used to manage other connections your application needs, such as connections to application servers. The following subsections describe how you can use the Database Navigator to view the database and its objects and to create a connection to the database: ■ JDeveloper Database Navigator ■ Creating a Database Connection ■ Browsing the Data Using the Database Navigator JDeveloper Database Navigator The Database Navigator displays all currently defined connections. To view the Database Navigator, select the Database Navigator tab in the navigator panel on the top left-hand side of the JDeveloper display, if it is displayed, or use the View menu. For an illustration of the default layout of the JDeveloper IDE, see Figure 1–1. You can use the Database Navigator to browse through the connections it displays. In particular, for a database schema, you can also view database objects, tables, views, and their contents. Database connections are shown under the IDE Connections node. To view the objects in the database, expand the connection. Expanding a schema displays nodes for the object types in that schema. Expanding the node for an object type displays its
  • 28. Connecting to Oracle Database from JDeveloper 3-2 Oracle Database 2 Day + Java Developer's Guide individual objects. When you expand a table node, you can view the structure of the table and the data within the table. Creating a Database Connection You can connect to any database for which you have connection details. When you create a database connection, you must specify a user name and a password. By default, the connection enables you to browse only the schema of the user that you specify in the connection. To create a connection, follow these steps: 1. Start JDeveloper. 2. From the View menu, go to Database and select Database Navigator. The Database Navigator is displayed, showing you a list of available connections. 3. Right-click IDE Connection, and from the shortcut menu, select New Connection. The Create Database Connection screen is displayed. 4. On the Create Database Connection screen, do not change the default values for the connection name and type, Connection1 and Oracle (JDBC). Enter HR in both the Username and Password fields. Do not enter a value for Role, and select Deploy Password. You must provide information about the computer where your database is located. Your database administrator should provide you with this information. Enter the following information: – Driver: thin – Host Name: Host name of the computer where Oracle Database 12c Release 1 (12.1) is installed If the database is on the same computer, then for the Host Name parameter, enter localhost. – JDBC Port: 1521 – SID: oracle Click Test Connection. If the connection is successful, the word Success! is displayed in the Status field. Figure 3–1 shows the Connection screen where you enter these details.
  • 29. Connecting to Oracle Database from JDeveloper Connecting to Oracle Database 12c Release 1 (12.1) 3-3 Figure 3–1 Specifying Connection Details 5. Click Finish to create the connection and close the screen. Disconnecting and Reconnecting from Oracle Database in JDeveloper To disconnect from the database in JDeveloper, in the Database Navigator, right-click the connection name and select Disconnect. The display in the Database Navigator now shows only the name of the connection, without the plus (+) symbol for expanding the node. To reconnect to the database, right-click the connection name and select Connect. Browsing the Data Using the Database Navigator After you have successfully established a connection to the database, you can browse its contents through the Database Navigator. The Database Navigator displays a navigable, hierarchical tree structure for the database, its objects, their instances, and the contents of each. To view the contents at each level of the hierarchy of the database connection that you created, do the following: 1. The IDE Connections node in the Database Navigator now shows a node with the name of your connection. Click the plus symbol (+) to the left of the connection name to expand the navigation tree. To display a list of the instances of an object type, for example Tables, expand the Table navigation tree. 2. The Structure window below the navigator shows the detailed structure of any object selected in the navigator. Select a table in the navigator (for example Employees) to see the columns of that table in the Structure window.
  • 30. Setting Up Applications and Projects in JDeveloper 3-4 Oracle Database 2 Day + Java Developer's Guide Figure 3–2 Viewing the Table Structure and Data 3. If you double-click a table in the navigator, the structure of that table is displayed in the main editing area of the window. It includes details about all the columns, such as Name, Type, and Size, so you can browse the table definition. To view the data from a table, select the Data tab below the table structure. You can now view and browse through the table data. 4. You can also edit the objects in the Database Navigator. To edit a table, right-click the table and select Edit from the shortcut menu. A dialog box enables you to make changes to the selected table. Setting Up Applications and Projects in JDeveloper In JDeveloper, you create your work in an application, within which you can organize your work into a number of projects. JDeveloper provides a number of application templates, to help you to create the project structure for standard types of application relatively quickly and easily. At the time you create your application in JDeveloper,
  • 31. Setting Up Applications and Projects in JDeveloper Connecting to Oracle Database 12c Release 1 (12.1) 3-5 you can choose the application template that matches the type of application you will be building. The application template you select determines the initial project structure (the named project folders within the application) and the application technologies that will be included. You can then add any extra libraries or technologies you need for your particular application, and create additional projects if you need them. Using the JDeveloper Application Navigator The Application Navigator displays all your applications and projects. When you first start JDeveloper, the Application Navigator is displayed by default on the left side of the JDeveloper IDE. To view the Application Navigator when it is not displayed, you can click the Applications tab in the navigator panel on the top left-hand side of the JDeveloper display, or select Application Navigator from the View menu. The Application Navigator shows a logical grouping of the items in your projects. To see the structure of an individual item, you can select it and the structure is displayed in the Structure window. From the Application Navigator, you can display items in an appropriate default editor. For example, if you double-click a Java file, the file opens in the Java Source Editor, and if you double-click a JavaServer Pages (JSP) file, it opens in the JSP/HTML Visual Editor. Creating an Application and a Project To get started with JDeveloper, you must create an application and at least one project in which to store your work, as follows: 1. In the Application Navigator, click New Application. 2. The Create Generic Application wizard is displayed. In the Name your application screen, enter HRApp in the Application Name field, and from the Application Template list, select Generic Application. Click Next. 3. On the Name your project screen, enter View as the name of the project. Click Finish. 4. The new HRApp application is displayed in the Application Navigator. 5. Save your application. To do this, from the File menu, select Save All. Viewing the Javadoc and Source Code Available in the Project Scope You can view the Javadoc or the code for any of the classes available in the project technology scope within JDeveloper. In addition, you can view the details of all the methods available for those classes. For example, to see the code or Javadoc for the Connection class, do the following: 1. With your project selected in the Application Navigator, from the Navigate menu select Go to Java Type. You can also do this for a specific file in your project. 2. In the Go to Java Type dialog box, type the name of the Java class. 3. Enter the name of the class you want to view in the Name field, or click Browse to find the class. For the Connection class, start to enter Connection, and from the displayed list select Connection (java.sql).
  • 32. Connecting to Oracle Database from a Java Application 3-6 Oracle Database 2 Day + Java Developer's Guide Figure 3–3 Selecting the Class to View the Javadoc in JDeveloper 4. Click OK. Connecting to Oracle Database from a Java Application So far, you have seen how to connect to the database from JDeveloper. To initiate a connection from the Java application, you use the Connection object from the JDBC application programming interface (API). This section describes connecting to the database from the Java application in the following subsections: ■ Overview of Connecting to Oracle Database ■ Specifying Database URLs ■ Creating a Java Class in JDeveloper ■ Java Libraries ■ Adding JDBC and JSP Libraries ■ Importing JDBC Packages ■ Declaring Connection-Related Variables ■ Creating the Connection Method Overview of Connecting to Oracle Database In Java, you use an instance of the DataSource object to get a connection to the database. The DataSource interface provides a complete replacement for the previous JDBC DriverManager class. Oracle implements the javax.sql.DataSource interface with the OracleDataSource class in the oracle.jdbc.pool package. The overloaded getConnection method returns a physical connection to the database. You can either set properties using appropriate setxxx methods for the DataSource object or use the getConnection method that accepts these properties as input parameters. Important DataSource Properties are listed in Table 3–1. Note: The use of the DriverManager class to establish a connection to a database is deprecated.
  • 33. Connecting to Oracle Database from a Java Application Connecting to Oracle Database 12c Release 1 (12.1) 3-7 If you choose to set the url property of the DataSource object with all necessary parameters, then you can connect to the database without setting any other properties or specifying any additional parameters with the getDBConnection method. For more information about setting the database URL, refer to the Specifying Database URLs section. Specifying Database URLs This release of Oracle JVM supports Internet Protocol Version 6 (IPv6) addresses in the URL and system names of the Java code in the database, which resolve to IPv6 addresses. Database URLs are strings that you specify for the value of the url property of the DataSource object. The complete URL syntax is the following: jdbc:oracle:driver_type:[username/password]@database_specifier The first part of the URL specifies which JDBC driver is to be used. The supported driver_type values for client-side applications are thin and oci. The brackets indicate that the user name and password pair is optional. The database_specifier value identifies the database to which the application is connected. The following is the syntax for thin-style service names that are supported by the Thin driver: jdbc:oracle:driver_type:[username/password]@//host_name:port_number:SID For the sample application created in this guide, if you include the user name and password, and if the database is hosted locally, then the database connection URL is as Table 3–1 Standard Data Source Properties Name Type Description databaseName String Name of the particular database on the server. Also known as the service name (or SID) in Oracle terminology. dataSourceName String Name of the underlying data source class. description String Description of the data source. networkProtocol String Network protocol for communicating with the server. For Oracle, this applies only to the JDBC Oracle Call Interface (OCI) drivers and defaults to tcp. password String Password for the connecting user. portNumber int Number of the port where the server listens for requests serverName String Name of the database server user String User name to be used for login driverType String Specifies the Oracle JDBC driver type. It can be either oci or thin. This is an Oracle-specific property. url String Specifies the URL of the database connect string.You can use this property in place of the standard portNumber, networkProtocol, serverName, and databaseName properties. This is an Oracle-specific property. Note: The parameters specified through the getConnection method override all property and url parameter settings previously specified in the application.
  • 34. Connecting to Oracle Database from a Java Application 3-8 Oracle Database 2 Day + Java Developer's Guide shown in Example 3–1. Example 3–1 Specifying the url Property for the DataSource Object jdbc:oracle:thin:hr/hr@localhost:1521:oracle Using the Default Service Feature of the Oracle Database If you have performed Oracle Database server installation in Typical mode, then the default service name used by the Oracle instance is ORCL, and the following Easy Connect syntax can be used to connect to that instance: sqlplus /nolog SQL> CONNECT username@"host/ORCL" SQL> Enter password: password The Easy Connect feature, which was introduced in Oracle Database 11g Release 1 (11.1), makes the following parts of the conventional JDBC connection URL syntax optional: jdbc:oracle:driver_type:[username/password]@[//]host_name[:port][:oracle] In this URL: ■ // is optional. ■ :port is optional. Specify a port only if the default Oracle Net listener port (1521) is not used. ■ :oracle (or the service name) is optional. The connection adapter for the Oracle Database connects to the default service on the host. On the host, this is set to ORACLE in the listener.ora file. Example 3–2 shows a basic configuration of the listener.ora file, where the default service is defined. Example 3–2 Default Service Configuration in listener.ora MYLISTENER = (ADDRESS_LIST= (ADDRESS=(PROTOCOL=tcp)(HOST=test555)(PORT=1521)) ) DEFAULT_SERVICE_MYLISTENER=dbjf.regress.rdbms.dev.testserver.com SID_LIST_MYLISTENER = (SID_LIST= (SID_DESC=(SID_NAME=dbjf)(GLOBAL_ DBNAME=dbjf.regress.rdbms.dev.testserver.com)(ORACLE_HOME=/test/oracle)) ) After making changes to the listener.ora file, you must restart the listener with the following command: > lsnrctl start mylistener The following URLs should work with this configuration: jdbc:oracle:thin:@//test555.testserver.com jdbc:oracle:thin:@//test555.testserver.com:1521 jdbc:oracle:thin:@test555.testserver.com See Also: Oracle Database Net Services Administrator's Guide for more information about the Easy Connect feature
  • 35. Connecting to Oracle Database from a Java Application Connecting to Oracle Database 12c Release 1 (12.1) 3-9 jdbc:oracle:thin:@test555.testserver.com:1521 jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test555.testserver.com )(PORT=1521))) jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test555.testserver.com ))) jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test555.testserver.com )(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=))) Creating a Java Class in JDeveloper The first step in building a Java application is to create a Java class. The following instructions describe how you create a class called DataHandler, which will contain the methods for querying the database and modifying the data in it. 1. In the Application Navigator, right-click the View project, and from the shortcut menu, select New. 2. In the New Gallery dialog box, select General in the Categories list. In the Items list, select Java Class, and click OK. The Create Java Class dialog box is displayed. 3. In the Create Java Class dialog box, enter DataHandler in the Name text box, and hr in the Package text box. Do not change the default values of the Optional Attributes, and click OK. The Create Java Class dialog box with the appropriate values specified is shown in Figure 3–4. Figure 3–4 Creating a Java Class 4. The skeleton DataHandler class is created and is displayed in the Java Source Editor. The package declaration, the class declaration, and the default constructor are created by default. Figure 3–5 shows the class displayed in the Java Source Note: Default service is a new feature in Oracle Database 12c Release 1 (12.1). If you use any other version of the Oracle Database to connect to the database, then you must specify the SID and port number.
  • 36. Connecting to Oracle Database from a Java Application 3-10 Oracle Database 2 Day + Java Developer's Guide Editor, ready for you to add your Java code: Figure 3–5 Java Source Editor Java Libraries Oracle JDeveloper comes with standard libraries to help Java application programming. These libraries include API support for Application Development Framework (ADF), Oracle libraries for JDBC, JSP, and so on. To use JDBC in your project, you import the Oracle JDBC library into the project. Similarly, to use JSP technology, you import the JSP Runtime library. Overview of the Oracle JDBC Library Important packages of the Oracle JDBC library include the following: ■ oracle.jdbc: The interfaces of the oracle.jdbc package define the Oracle extensions to the interfaces in the java.sql package. These extensions provide access to Oracle SQL-format data and other Oracle-specific features, including Oracle performance enhancements. ■ oracle.sql: The oracle.sql package supports direct access to data in SQL format. This package consists primarily of classes that provide Java mappings to SQL data types and their support classes. ■ oracle.jdbc.pool: This package includes the OracleDataSource class that is used to get a connection to the database. The overloaded getConnection method returns a physical connection to the database. Overview of the JSP Runtime Library This library includes the classes and tag libraries required to interpret and run JSP files on the Oracle WebLogic Server that comes with JDeveloper. Adding JDBC and JSP Libraries To include libraries in your project, perform the following steps: 1. Double-click the View project in the Application Navigator to display the Project Properties dialog box. 2. Click Libraries and Classpath, and then click Add Library.... The Add Library dialog box is displayed with a list of the available libraries for the Java2 Platform, Standard Edition (J2SE) version is displayed.
  • 37. Connecting to Oracle Database from a Java Application Connecting to Oracle Database 12c Release 1 (12.1) 3-11 3. In the Add Library dialog box, scroll through the list of libraries in the Extension folder. Select JSP Runtime library and click OK to add it to the list of selected libraries for your project. Similarly, add the Oracle JDBC library. Figure 3–6 shows the Oracle JDBC library added to the View project. Figure 3–6 Importing Libraries 4. Click OK. Importing JDBC Packages To use JDBC in the Java application, import the following JDBC packages: 1. If the DataHandler.java class is not already open in the Java Source Editor, in the Application Navigator, expand the View project, Application Sources, and your package (hr) and double-click DataHandler.java. 2. At the end of the generated package declaration, on a new line, enter the import statements shown in Example 3–3. Example 3–3 Importing Packages in a Java Application package hr; import java.sql.Connection; import oracle.jdbc.pool.OracleDataSource; Declaring Connection-Related Variables Connection information is passed to the connection method by using the following connection variables: the connection URL, a user name, and the corresponding password. Use the Java Source Editor of JDeveloper to edit the DataHandler.java class as follows:
  • 38. Connecting to Oracle Database from a Java Application 3-12 Oracle Database 2 Day + Java Developer's Guide 1. After the DataHandler constructor, on a new line, declare the three connection variables as follows: String jdbcUrl = null; String userid = null; String password = null; These variables will be used in the application to contain values supplied by the user at login to authenticate the user and to create a connection to the database. The jdbcUrl variable is used to hold the URL of the database that you will connect to. The userid and password variables are used to authenticate the user and identify the schema to be used for the session. 2. On a new line, declare a connection instance as follows: Connection conn; Your Java class should now contain the code in Example 3–4. Example 3–4 Declaring Connection Variables and the Connection Object package hr; import java.sql.Connection; import oracle.jdbc.pool.OracleDataSource; public class DataHandler { public DataHandler() { } String jdbcUrl = null; String userid = null; String password = null; Connection conn; } Creating the Connection Method To connect to the database, you must create a method as follows: Note: The login variables have been set to null to secure the application. At this point in the guide, application login functionality is yet to be built into the application. Therefore, to test the application until login functionality is built in, you can set values in the login variables as follows: Set the jdbcUrl variable to the connect string for your database. String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORACLE"; Set the variables userid and password to hr as follows: String userid = "hr"; String password = "hr"; Make sure you reset these to null as soon as you finish testing. For more information about security features and practices, refer to Oracle Database Security Guide and the vendor-specific documentation for your development environment.
  • 39. Connecting to Oracle Database from a Java Application Connecting to Oracle Database 12c Release 1 (12.1) 3-13 1. Add the following method declaration after the connection declaration: public void getDBConnection() throws SQLException The Java Code Insight feature displays a message reminding you to import the SQLException error handling package. Press the Alt+Enter keys to import it. The import java.sql.SQLException statement is added to the list of import packages. 2. At the end of the same line, add an open brace ({) and then press the Enter key. JDeveloper automatically creates the closing brace, and positions the cursor in a new empty line between the braces. 3. On a new line, declare an OracleDataSource instance as follows: OracleDataSource ds; 4. Enter the following to create a new OracleDataSource object: ds = new OracleDataSource(); 5. Start to enter the following to set the URL for the DataSource object: ds.setURL(jdbcUrl); Java Code Insight prompts you by providing you with a list of available OracleDataSource methods. Scroll through the list to select the setURL(String) method, and press the Enter key to select it into your code. In the parentheses for this function, enter jdbcUrl in place of arg0. Figure 3–7 shows how the Java Code Insight feature in JDeveloper helps you with inserting code. Figure 3–7 Java Code Insight 6. On the next line, enter the following: conn = ds.getConnection(userid,password); As usual, Java Code Insight will prompt you with a list of methods for ds. This time, select getConnection(String,String). In the parentheses, enter userid,password. End the line with a semicolon (;). Your code should look similar to the code in Example 3–5. Example 3–5 Adding a Method to Connect to the Database package hr; import java.sql.Connection; import java.sql.SQLException;
  • 40. Connecting to Oracle Database from a Java Application 3-14 Oracle Database 2 Day + Java Developer's Guide import oracle.jdbc.pool.OracleDataSource; public class DataHandler { public DataHandler() { } String jdbcUrl = null; String userid = null; String password = null; Connection conn; public void getDBConnection() throws SQLException{ OracleDataSource ds; ds = new OracleDataSource(); ds.setURL(jdbcUrl); conn=ds.getConnection(userid,password); } } 7. Compile your class to ensure that there are no syntax errors. To do this, right-click in the Java Source Editor, and select Make from the shortcut menu. A Successful compilation message is displayed in the Log window below the Java Source Editor window.
  • 41. 4 Querying for and Displaying Data 4-1 4 Querying for and Displaying Data This chapter adds functions and code to the DataHandler.java file for querying the database. This chapter has the following sections: ■ Overview of Querying for Data in Oracle Database ■ Querying Data from a Java Application ■ Creating JSP Pages ■ Adding Dynamic Content to the JSP Page: Database Query Results ■ Filtering a Query Result Set ■ Adding Login Functionality to the Application ■ Testing the JSP Page Overview of Querying for Data in Oracle Database In outline, to query Oracle Database 12c Release 1 (12.1) from a Java class to retrieve data, you must do the following: 1. Create a connection by using the OracleDataSource.getConnection method. This is covered in Chapter 3, "Connecting to Oracle Database 12c Release 1 (12.1)". 2. Define your SQL statements with the methods available for the connection object. The createStatement method is used to define a SQL query statement. 3. Using the methods available for the statement, run your queries. You use the executeQuery method to run queries on the database and produce a set of rows that match the query conditions. These results are contained in a ResultSet object. 4. You use a ResultSet object to display the data in the application pages. The following sections describe important Java Database Connectivity (JDBC) concepts related to querying the database from a Java application: ■ SQL Statements ■ Query Methods for the Statement Object ■ Result Sets SQL Statements Once you connect to the database and, in the process, create a Connection object, the next step is to create a Statement object. The createStatement method of the JDBC Connection object returns an object of the JDBC Statement type. Example 4–1 shows how to create a Statement object.
  • 42. Overview of Querying for Data in Oracle Database 4-2 Oracle Database 2 Day + Java Developer's Guide Example 4–1 Creating a Statement Object Statement stmt = conn.createStatement(); The Statement object is used to run static SQL queries that can be coded into the application. In addition, for scenarios where many similar queries with differing update values must be run on the database, you use the OraclePreparedStatement object, which extends the Statement object. To access stored procedures on Oracle Database 12c Release 1 (12.1), you use the OracleCallableStatement object. Query Methods for the Statement Object To run a query embedded in a Statement object, you use variants of the execute method. Important variants of this method are listed in Table 4–1. Result Sets A ResultSet object contains a table of data representing a database result set, which is generated by executing a statement that queries the database. A cursor points to the current row of data in a ResultSet object. Initially, it is positioned before the first row. Use the next method of the ResultSet object to move the cursor to the next row in the result set. It returns false when there are no more rows in the ResultSet object. Typically, the contents of a ResultSet object are read by using the next method within a loop until it returns false. See Also: ■ Using OraclePreparedStatement ■ Using OracleCallableStatement Table 4–1 Key Query Execution Methods for java.sql.Statement Method Name Return Type Description execute(String sql) Boolean Runs the given SQL statement, which returns a Boolean response: true if the query runs successfully and false if it does not. addBatch() void Adds a set of parameters to a PreparedStatement object batch of commands. executeBatch() int[] Submits a batch of commands to the database for running, and returns an array of update counts if all commands run successfully. executeQuery(String sql) ResultSet Runs the given SQL statement, which returns a single ResultSet object. executeUpdate(String sql) int Runs the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or a SQL statement that returns nothing, such as a SQL DDL statement. See Also: http://www.oracle.com/technetwork/java/javase/documentation/ api-jsp-136079.html
  • 43. Overview of Querying for Data in Oracle Database Querying for and Displaying Data 4-3 The ResultSet interface provides accessor methods (getBoolean, getLong, getInt, and so on) for retrieving column values from the current row. Values can be retrieved by using either the index number of the column or the name of the column. By default, only one ResultSet object per Statement object can be open at the same time. Therefore, to read data from multiple ResultSet objects, you must use multiple Statement objects. A ResultSet object is automatically closed when the Statement object that generated it is closed, rerun, or used to retrieve the next result from a sequence of multiple results. Features of ResultSet Objects Scrollability refers to the ability to move backward as well as forward through a result set. You can also move to any particular position in the result set, through either relative positioning or absolute positioning. Relative positioning lets you move a specified number of rows forward or backward from the current row. Absolute positioning lets you move to a specified row number, counting from either the beginning or the end of the result set. When creating a scrollable or positionable result set, you must also specify sensitivity. This refers to the ability of a result set to detect and reveal changes made to the underlying database from outside the result set. A sensitive result set can see changes made to the database while the result set is open, providing a dynamic view of the underlying data. Changes made to the underlying column values of rows in the result set are visible. Updatability refers to the ability to update data in a result set and then copy the changes to the database. This includes inserting new rows into the result set or deleting existing rows. A result set may be updatable or read-only. Summary of Result Set Object Types Scrollability and sensitivity are independent of updatability, and the three result set types and two concurrency types combine for the following six result set categories: ■ Forward-only/read-only ■ Forward-only/updatable ■ Scroll-sensitive/read-only ■ Scroll-sensitive/updatable ■ Scroll-insensitive/read-only ■ Scroll-insensitive/updatable Example 4–2 demonstrates how to declare a scroll-sensitive and read-only ResultSet object. Example 4–2 Declaring a Scroll-Sensitive, Read-Only ResultSet Object stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_ READ_ONLY); See Also: ■ Oracle Database JDBC Developer's Guide for more information about result sets and their features
  • 44. Querying Data from a Java Application 4-4 Oracle Database 2 Day + Java Developer's Guide Querying Data from a Java Application This section discusses how you can use JDeveloper to create a Java class that queries data in Oracle Database 12c Release 1 (12.1) in the following sections: ■ Creating a Method in JDeveloper to Query Data ■ Testing the Connection and the Query Methods Creating a Method in JDeveloper to Query Data The following steps show you how to add a simple query method to your DataHandler.java class. If DataHandler.java is not open in the JDeveloper integrated development environment (IDE), double-click the DataHandler.java file in the Application Navigator to display it in the Java Source Editor. 1. In the DataHandler class, add the following import statements after the existing import statements to use the Statement and ResultSet JDBC classes: import java.sql.Statement; import java.sql.ResultSet; 2. After the connection declaration, declare variables for Statement, ResultSet, and String objects as follows: Statement stmt; ResultSet rset; String query; String sqlString; 3. Create a method called getAllEmployees, which will be used to retrieve employee information from the database. Enter the signature for the method: public ResultSet getAllEmployees() throws SQLException{ 4. Press the Enter key to include the closing brace for the method and a new line to start entering the method code. 5. Call the getDBConnection method created earlier: getDBConnection(); 6. After calling the getDBConnection method, use the createStatement method of the Connection instance to provide context for executing the SQL statement and define the ResultSet type. Specify a read-only, scroll-sensitive ResultSet type as stated in the following code: stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_ READ_ONLY); The Java Code Insight feature can help you ensure that the statement syntax is correct. Note: A forward-only updatable result set has no provision for positioning at a particular row within the ResultSet object. You can update rows only as you iterate through them using the next method.
  • 45. Querying Data from a Java Application Querying for and Displaying Data 4-5 7. Define the query and print a trace message. The following code uses a simple query to return all the rows and columns in the Employees table, where the data is ordered by the Employee ID: query = "SELECT * FROM Employees ORDER BY employee_id"; System.out.println("nExecuting query: " + query); 8. Run the query and retrieve the results in the ResultSet instance as follows: rset = stmt.executeQuery(query); 9. Return the ResultSet object: return rset; 10. Save your work. From the File menu, select Save All. The code for the getAllEmployees method should be as shown in Example 4–3. Example 4–3 Using the Connection, Statement, Query, and ResultSet Objects public ResultSet getAllEmployees() throws SQLException{ getDBConnection(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); query = "SELECT * FROM Employees ORDER BY employee_id"; System.out.println("nExecuting query: " + query); rset = stmt.executeQuery(query); return rset; } Testing the Connection and the Query Methods In the following steps, you create a simple Java class to test the methods in the DataHandler.java class. To test your application at this stage, you can temporarily set the value of the jdbcUrl variable to the connection string for your database and set the values of the userid and password variables to the values required to access the HR schema. 1. Open the DataHandler.java class in the Java Visual Editor from the Application Navigator. 2. Change the jdbcUrl, userid and password variables to contain the values required for the HR schema as follows: String jdbcUrl = "connect-string"; String userid = "HR"; String password = "hr"; where connect-string is, for example: jdbc:oracle:thin:@localhost:1521:ORACLE 3. Create a new Java class named JavaClient in the hr package. Make it a public class and generate a default constructor and a main method. Note that you must select the Main Method check box from the Optional Attributes panel to generate the main method. The skeleton JavaClient.java class is created and displayed in the Java Source Editor. See Also: Declaring Connection-Related Variables in Chapter 3
  • 46. Querying Data from a Java Application 4-6 Oracle Database 2 Day + Java Developer's Guide 4. Import the ResultSet package: import java.sql.ResultSet; 5. In the main method declaration, add exception handling as follows: public static void main(String[] args) throws Exception{ 6. Replace the JavaClient object created by default with a DataHandler object. Locate the following line in the main method: JavaClient javaClient = new JavaClient(); Replace this with: DataHandler datahandler = new DataHandler(); 7. Define a ResultSet object to hold the results of the getAllEmployees query, and iterate through the rows of the result set, displaying the first four columns, Employee Id, First Name, Last Name, and Email. To do this, add the following code to the main method: ResultSet rset = datahandler.getAllEmployees(); while (rset.next()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getString(3) + " " + rset.getString(4)); } 8. Compile the JavaClient.java file to check for compilation errors. To do this, right-click in the Java Source Editor, and select Make from the shortcut menu. If there are no errors in compilation, you should see the following message in the Log window: Successful compilation: 0 errors, 0 warnings 9. Run the JavaClient.java file. To do this, right-click in the Java Source Editor window and select Run from the shortcut menu. 10. Examine the output in the Log window. Notice the trace message, followed by the four columns from the Employees table as shown in Figure 4–1. See Also: Creating a Java Class in JDeveloper in Chapter 3 for information about creating a Java class file
  • 47. Creating JSP Pages Querying for and Displaying Data 4-7 Figure 4–1 Test Output for Query Method in Log Window 11. When you finish testing the application, set the jdbcUrl, userid and password variables in DataHandler.java back to null. Creating JSP Pages The HRApp application uses JavaServer Pages (JSP) technology to display data. JSP technology provides a simple, fast way to create server-independent and platform-independent dynamic Web content. A JSP page has the .jsp extension. This extension notifies the Web server that the page should be processed by a JSP container. The JSP container interprets the JSP tags and scriptlets, generates the content required, and sends the results back to the client as an HTML or XML page. To develop JSP pages, you use some or all of the following: ■ HTML tags to design and format the dynamically generated Web page ■ Standard JSP tags or Java-based scriptlets to call other components that generate the dynamic content on the page ■ JSP tags from custom tag libraries that generate the dynamic content on the page In this section, you will see how you can create JSP pages for the application in this guide in the following sections: ■ Overview of Page Presentation ■ Creating a Simple JSP Page ■ Adding Static Content to a JSP Page ■ Adding a Style Sheet to a JSP Page Overview of Page Presentation JSP pages can do the following: ■ Display data ■ Hold input data entered by users adding employees and editing employee data See Also: Declaring Connection-Related Variables See Also: http://www.oracle.com/technetwork/java/javaee/jsp/index.html