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

Part Number A83724-01

Library

Solution Area

Contents

Index

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

Sample: Connecting, Querying, and Processing the Results

The steps in the preceding sections are illustrated in the following example, which registers an Oracle JDBC Thin driver, connects to the database, creates a Statement object, executes a query, and processes the result set.

Note that the code for creating the Statement object, executing the query, returning and processing the ResultSet object, and closing the statement and connection all follow standard JDBC syntax.

import java.sql.*; 
import java.math.*;
import java.io.*;
import java.awt.*;

class JdbcTest { 
   public static void main (String args []) throws SQLException { 
      // Load Oracle driver
      DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
      // Connect to the local database
      Connection conn = DriverManager.getConnection 
                ("jdbc:oracle:thin:@myhost:1521:ORCL","scott", "tiger");
    
      // Query the employee names 
      Statement stmt = conn.createStatement (); 
      ResultSet rset = stmt.executeQuery ("SELECT ename FROM emp");
      // Print the name out 
      while (rset.next ())
         System.out.println (rset.getString (1));
    
      //close the result set, statement, and the connection
      rset.close();
      stmt.close();
      conn.close();
   } 
} 

If you want to adapt the code for the OCI driver, replace the Connection statement with the following:

Connection conn = DriverManager.getConnection 
                  ("jdbc:oracle:oci8:@MyHostString", "scott", "tiger");

Where MyHostString is an entry in the TNSNAMES.ORA file.



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

All Rights Reserved.

Library

Solution Area

Contents

Index