Using Java To Connect To Oracle Database XE
Using Java To Connect To Oracle Database XE
Using Java To Connect To Oracle Database XE
Oracle Database Express Edition (Oracle Database XE) 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.
See Also:
The Sun Microsystems documentation site for JDBC at
http://java.sun.com/javase/technologies/database.jsp
This guide shows you how to use a simple Java application to connect to Oracle
Database XE 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 this application in the following topics:
See Also:
You can use the Oracle JDBC Thin Driver to write Java applications and applets that
access Oracle SQL data. The JDBC Thin Driver is especially well-suited for Web-based
applications and applets, because you can dynamically download it from a Web page,
like a Java applet.
The JDBC Thin Driver is a pure Java, Type IV driver. It supports the Java Development
Kit (JDK) releases 1.4.x and 1.5.x. 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 XE.
The JDBC Thin Driver allows a direct connection to the database by providing a pure
Java implementation of Oracle network protocols (TTC, SQL*Net). The driver supports
the TCP/IP protocol and requires a TNS listener on the TCP/IP sockets on the database
server. The Thin driver will work on any machine that has a suitable Java virtual
machine (JVM).
The JDBC Thin Driver is fully compatible with JDBC 3.0 specifications. You can access
the Oracle-specific JDBC features and the standard features by using the
oracle.jdbc package.
The JDBC OCI driver is a Type II driver used with Java applications. It requires an
Oracle client installation. It supports all installed Oracle Net adapters, including IPC,
named pipes, TCP/IP, and InternetworkPacket Exchange/Sequenced Packet Exchange
(IPX/SPX).
OCI is an API that enables you to create applications that use the native procedures or
function calls of a third-generation language to access Oracle Database and control all
phases of the SQL statement processing.
The JDBC OCI driver, written in a combination of Java and C, converts JDBC calls to
calls to OCI. It does this by using native methods to call C-entry points. These calls
communicate with the database using SQL*Net.
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.4 through 1.5.
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 UCS2 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 the 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:
See Also:
For more information on Oracle support for JDBC and other features provided by Oracle
that extend the JDBC API, refer to Oracle Database JDBC Developer's Guide and Reference.
JDeveloper provides features for you to write and run 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:
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.
Note:
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.
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).
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. Handle exceptions.
The application connects to the HR schema that ships with Oracle Database XE.
Although the Oracle Database XE client installation comes with both the Thin and OCI
drivers, the sample application will use only the JDBC Thin Driver.
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
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 nonvisual page that handles the insertion of data for a new employee
that is entered on the insert.jsp page.
edit_emp.jsp
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_emp.jsp page directs the data to this nonvisual
page, which inserts the edited data into the database.
delete_action.jsp
Figure 1-2 shows the relationships among the pages developed for this application.
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
JavaClient.java
Note:
This application is developed throughout this book in the form of a tutorial. It is
recommended, therefore, that you read these chapters in sequence.
Resources
In addition to this document, Oracle provides the following resources for Oracle
Database XE on the Oracle Technology Network:
http://www.oracle.com/technology/xe
http://www.oracle.com/technology/xe/registration
http://www.oracle.com/technology/xe/documentation
http://www.oracle.com/technology/xe/forum
Hi Friend,
Class.forName("oracle.jdbc.driver.OracleDriver");
3) Connect to database:
where oracle.world is the TNS NAMES entry and root is the username and password.
where localhost is the host,3306 is the port, roseindia is the database and root is the username and password.
a)create statement:
Statement st = connection.createStatement();
b)write query and execute the query:
rs.close();
st.close();
connection.close();
Example:
<%@ page import="java.sql.*"%>
<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin: @localhost:3306:Oracle", "rose", "rose");
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from data");
while(rs.next(){
String name=rs.getString("name");
String add=rs.getString("address");
out.println(name+" "+add);
}
rs.slose();
st.close();
connection.close();
}
catch (Exception ex) {}
%>
Thanks