Oracle9i JDBC Developer's Guide and Reference Release 1 (9.0.1) Part Number A90211-01 |
|
This chapter begins by discussing compatibilities between Oracle JDBC driver versions, database versions, and JDK versions. It then guides you through the basics of testing your installation and configuration, and running a simple application. The following topics are discussed:
Table 2-1 lists the compatibilities between Oracle JDBC driver versions and Oracle database versions. The JDK versions supported by each JDBC driver version are also listed.
This section covers the following topics:
Installation of an Oracle JDBC driver is platform-specific. Follow the installation instructions for the driver you want to install in your platform-specific documentation.
This section describes the steps of verifying an Oracle client installation of the JDBC drivers. It assumes that you have already installed the driver of your choice.
If you have installed the JDBC Thin driver, no further installation on the client machine is necessary (the JDBC Thin driver requires a TCP/IP listener to be running on the database machine).
If you have installed the JDBC OCI driver, you must also install the Oracle client software. This includes Oracle Net and the OCI libraries.
This section assumes that you have already installed the Sun Microsystems Java Developer's Kit (JDK) on your system (although other forms of Java are also supported). Oracle offers JDBC drivers compatible with either JDK 1.2.x versions or JDK 1.1.x versions.
Installing the Oracle8i Java products creates, among other things, an [ORACLE_HOME]/jdbc
directory containing these subdirectories and files:
demo/samples:
The samples
subdirectory contains sample programs, including examples of how to use SQL92 and Oracle SQL syntax, PL/SQL blocks, streams, user-defined types, additional Oracle type extensions, and Oracle performance extensions.
doc
: The doc
directory contains documentation about the JDBC drivers.
lib
: The lib
directory contains .zip
files with these required Java classes:
classes12.zip
contains the classes for use with the JDK 1.2.x--all the JDBC driver classes except the classes necessary for NLS support.
nls_charset12.zip
contains the classes necessary for NLS support with the JDK 1.2.x.
jta.zip
and jndi.zip
contain classes for the Java Transaction API and the Java Naming and Directory Interface for JDK 1.2.x. These are only required if you will be using JTA features for distributed transaction management or JNDI features for naming services. (These files can also be obtained from the Sun Microsystems Web site, but it is advisable to use the versions from Oracle, because those have been tested with the Oracle drivers.)
classes111.zip
contains the classes for use with the JDK 1.1.x--all the JDBC driver classes except the classes necessary for NLS support.
classes111.zip
also contains Oracle extensions that allow you to use JDBC 2.0 functionality for objects, arrays, and LOBs under JDK 1.1.x.
nls_charset11.zip
contains the classes necessary for NLS support with the JDK 1.1.x.
The nls_charset12.zip
and nls_charset11.zip
files provide support for specific NLS character sets. They have been separated out from the classes*.zip
files to give you the option of excluding character sets in situations where complete NLS support is not needed. For more information on nls_charset12.zip
and nls_charset11.zip
, see "Globalization Support and Object Types".
readme.txt
: The readme.txt
file contains late-breaking and release-specific information about the drivers that might not be in this manual.
Check that all these directories have been created and populated.
This section describes the environment variables that must be set for the JDBC OCI driver and the JDBC Thin driver, focusing on the Sun Microsystems Solaris and Microsoft Windows NT platforms.
You must set the CLASSPATH
for your installed JDBC OCI or Thin driver. Depending on whether you are using the JDK 1.2.x versions or 1.1.x versions, you must set one of these values for the CLASSPATH
:
[Oracle Home]/jdbc/lib/classes12.zip
[Oracle Home]/jdbc/lib/nls_charset12.zip)
for full NLS character support)
or:
[Oracle Home]/jdbc/lib/classes111.zip
[Oracle Home]/jdbc/lib/nls_charset11.zip)
for full NLS character support)
Ensure that there is only one classes*.zip
file version and one nls_charset*.zip
file version in your CLASSPATH
.
Note:
If you will be using JTA features or JNDI features, both of which are discussed in Chapter 14, "Connection Pooling and Caching", then you will also need to have |
If you are installing the JDBC OCI driver, you must also set the following value for the library path environment variable
LD_LIBRARY_PATH
as follows:
[Oracle Home]/lib
This directory contains the libocijdbc9.so
shared object library.
PATH
as follows:
[Oracle Home]\lib
This directory contains the ocijdbc8.dll
dynamic link library.
If you are installing the JDBC Thin driver, you do not have to set any other environment variables.
To further ensure that Java is set up properly on your client system, go to the samples
directory (for example, C:\oracle\ora81\jdbc\demo\samples
if you are using the JDBC driver on a Windows NT machine), then see if javac
(the Java compiler) and java
(the Java interpreter) will run without error. Enter:
javac
then enter:
java
Each should give you a list of options and parameters and then exit. Ideally, verify that you can compile and run a simple test program.
If at any time you must determine the version of the JDBC driver that you installed, you can invoke the getDriverVersion()
method of the OracleDatabaseMetaData
class.
Here is sample code showing how to do it:
import java.sql.*; import oracle.jdbc.*; class JDBCVersion { public static void main (String args[]) throws SQLException { // Load the Oracle JDBC driver DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@host:port:sid","scott","tiger"); // Create Oracle DatabaseMetaData object DatabaseMetaData meta = conn.getMetaData(); // gets driver info: System.out.println("JDBC driver version is " + meta.getDriverVersion()); } }
The samples directory contains sample programs for a particular Oracle JDBC driver. One of the programs, JdbcCheckup.java
, is designed to test JDBC and the database connection. The program queries you for your user name, password, and the name of a database to which you want to connect. The program connects to the database, queries for the string "Hello World
", and prints it to the screen.
Go to the samples
directory and compile and run JdbcCheckup.java
. If the results of the query print without error, then your Java and JDBC installations are correct.
Although JdbcCheckup.java
is a simple program, it demonstrates several important functions by executing the following:
"First Steps in JDBC", describes these functions in greater detail. A listing of JdbcCheckup.java
for the JDBC OCI driver appears below.
/* * This sample can be used to check the JDBC installation. * Just run it and provide the connect information. It will select * "Hello World" from the database. */ // You need to import the java.sql package to use JDBC import java.sql.*; // We import java.io to be able to read from the command line import java.io.*; class JdbcCheckup { public static void main(String args[]) throws SQLException, IOException { // Load the Oracle JDBC driver DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); // Prompt the user for connect information System.out.println("Please enter information to test connection to the database"); String user; String password; String database; user = readEntry("user: "); int slash_index = user.indexOf('/'); if (slash_index != -1) { password = user.substring(slash_index + 1); user = user.substring(0, slash_index); } else password = readEntry("password: "); database = readEntry("database(a TNSNAME entry): "); System.out.print("Connecting to the database..."); System.out.flush(); System.out.println("Connecting..."); Connection conn = DriverManager.getConnection ("jdbc:oracle:oci8:@" + database, user, password); System.out.println("connected."); // Create a statement Statement stmt = conn.createStatement(); // Do the SQL "Hello World" thing ResultSet rset = stmt.executeQuery("select 'Hello World' from dual"); while (rset.next()) System.out.println(rset.getString(1)); // close the result set, the statement and connect rset.close(); stmt.close(); conn.close(); System.out.println("Your JDBC installation is correct."); } // Utility function to read a line from standard input static String readEntry(String prompt) { try { StringBuffer buffer = new StringBuffer(); System.out.print(prompt); System.out.flush(); int c = System.in.read(); while (c != '\n' && c != -1) { buffer.append((char)c); c = System.in.read(); } return buffer.toString().trim(); } catch(IOException e) { return ""; } } }
|
Copyright © 1996-2001, Oracle Corporation. All Rights Reserved. |
|