Oracle8i JDBC Developer's Guide and Reference
Release 3 (8.1.7)

Part Number A83724-01

Library

Product

Contents

Index

Go to previous page Go to beginning of chapter Go to next page

Verifying a JDBC Client Installation

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 Net8 and the OCI libraries.

Check Installed Directories and Files

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 Oracle JServer products creates, among other things, an [ORACLE_HOME]/jdbc directory containing these subdirectories and files:

Check that all these directories have been created and populated.

Check the Environment Variables

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:

or:

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 15, "Connection Pooling and Caching", then you will also need to have jta.zip and jndi.zip in your CLASSPATH.  


JDBC OCI Drivers:

If you are installing the JDBC OCI driver, you must also set the following value for the library path environment variable

JDBC Thin Drivers:

If you are installing the JDBC Thin driver, you do not have to set any other environment variables.

Make Sure You Can Compile and Run Java

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.

Determine the Version of the JDBC Driver

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.driver.*;

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());
   }
}

Testing JDBC and the Database Connection: JdbcCheckup

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.driver.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 "";
      }
   }
}



Go to previous page
Go to beginning of chapter
Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index