Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views7 pages

Supplement L: Tutorial For Oracle For Introduction To Java Programming, 5E by Y. Daniel Liang

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Supplement L: Tutorial for Oracle

For Introduction to Java Programming, 5E


By Y. Daniel Liang

0 Introduction
Oracle 9i Enterprise runs on Windows 2000, Linux and
Solaris. It can support multiple users concurrently on the
network. Students can access an Oracle 9i from a Web browser
without installing any Oracle software.

1 Using Oracle
There are many ways to access Oracle. The easiest is to use
iSQL*Plus, which enables you to access Oracle from a Web
browser. It requires no installation by the user. Suppose an
Oracle 9i Enterprise database has been installed on the host
liang.armstrong.edu with HTTP server enabled, the user can
access it from a Web browser using the URL
http://liang.armstrong.edu/isqlplus, as shown in Figure 1.1.

Figure 1.1

You can start iSQL*Plus from a Web browser.

Enter the username (e.g., scott) and password (e.g., tiger)

8
© Copyright Y. Daniel Liang, 2005
and click Log In to log into the database. The iSQL*Plus
user interface is shown in Figure 1.2.

Figure 1.2

You can enter SQL statements, save a SQL script, and


load SQL script from iSQL*Plus.

Enter the following SQL statements in the Enter Statement


text box and click the Execute button. The execution result
is displayed on the work screen, as shown in Figure 1.3.
create table State(
name varchar(15) not null,
capital varchar(25),
population integer);

insert into State values ('Georgia', 'Atlanta', 8383915);


insert into State values ('New York', 'Albany', 19011378);

9
© Copyright Y. Daniel Liang, 2005
select * from State;

commit;

The commit statement ends the current transaction and makes


permanent all changes performed in the transaction.

Figure 1.3

The execution result of the SQL statements is displayed in


the work screen.

10
© Copyright Y. Daniel Liang, 2005
You can choose the Output type to display the result in the
work screen, file, or a separate window. You can save the
statements in a script file by clicking the Save Script
button. You can load the script file from the local client
by clicking the Browse button.

Another way to access Oracle is through SQL*Plus. You have


to first install SQL*Plus on your computer. If Oracle
database is also installed on your machine, you can access
it by typing sqlplus scott/tiger from the command window. If
Oracle database is on a different machine, you need to
create an Oracle network service name for the remote
database. Figure 1.4 shows an example of using SQL*Plus to
access the oracle database on the host liang.armstrong.edu.
The alias name for the database is liangora9i.

Figure 1.4

You can start SQL*Plus from the DOS command prompt.

If you have typing errors, you have to retype the whole


command. To avoid retyping the whole command, you can save
the command in a file, and then run the command from the
file. To do so, type edit filename.sql from the SQL command
prompt as shown in Figure 1.5. This command invokes Windows
Notepad as shown in Figure 1.6. Type SQL commands in the
Notepad and save and exit Notepad. To comment a line,
precede with two dashes. You can now run the script file by
typing @filename.sql, as shown in Figure 1.7.

11
© Copyright Y. Daniel Liang, 2005
Figure 1.5

You can use the edit command to launch an editor.

Figure 1.6

You can save the SQL commands using Notepad.

12
© Copyright Y. Daniel Liang, 2005
Figure 1.7

You can run the SQL commands in a script file from


Oracle.

As shown in Figure 1.7, an error is generated when the


create table statement is executed, because the table
already exists. The insert statements cause errors because
they violate the primary key constraint.

4 Accessing Oracle Using Java


NOTE: you are not familiar with SQL and JDBC,
please Chapter 25 on basics of SQL and how to
write Java database programs.

The JDBC driver for Oracle is


oracle.jdbc.driver.OracleDriver contained in
c:\book\classes12.jar. To run a Java program that accesses
an Oracle database, you must put classes12.jar in the
classpath.

13
© Copyright Y. Daniel Liang, 2005
The database URL for Oracle is
jdbc:oracle:thin:@hostname:port#:oracleDBSID. For example,
if the database is named ora9i on host liang.armstrong.edu
on port 1521, the URL is
jdbc:oracle:thin:@liang.armstrong.edu:1521:ora9i.

The following is a sample program


import java.sql.*;

public class SimpleJdbc {

public static void main(String[] args)

throws SQLException, ClassNotFoundException {

// Load the JDBC driver

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

System.out.println("Driver loaded");

// Establish a connection

Connection connection = DriverManager.getConnection

("jdbc:oracle:thin:@liang.armstrong.edu:1521:ora9i",

"scott", "tiger");

System.out.println("Database connected");

// Create a statement

Statement statement = connection.createStatement();

// Execute a statement

ResultSet resultSet = statement.executeQuery

("select firstName, mi, lastName from Student where lastName "

+ " = 'Smith'");

// Iterate through the result and print the student names

while (resultSet.next())

System.out.println(resultSet.getString(1) + "\t" +

resultSet.getString(2) + "\t" + resultSet.getString(3));

// Close the connection

connection.close();

14
© Copyright Y. Daniel Liang, 2005

You might also like