Oracle8i JDBC Developer's Guide and Reference Release 8.1.5 A64685-01 |
|
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 driverDriverManager.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.
Note:
If you are creating code for an applet, the |