Oracle8i JDBC Developer's Guide and Reference Release 3 (8.1.7) Part Number A83724-01 |
Library | Service | Contents | Index

## Basic Samples

This section provides elementary samples that print employee information from a table and insert employee information into the table.

- Listing Names from the EMP Table--Employee.java

- Inserting Names into the EMP Table--InsertExample.java

These samples are located in the following directory on the product CD:

    [Oracle Home]/jdbc/demo/samples/oci8/basic-samples

For a step-by-step discussion of basic JDBC functionality, see "First Steps in JDBC".

### Listing Names from the EMP Table--Employee.java

This example retrieves and prints all the employee names from the `EMP` table.

Note: Do not confuse this `Employee.java` with the one used later as an example of a `CustomDatum` implementation of a custom Java class.

    /*
     * This sample shows how to list all the names from the EMP table
     */

    // You need to import the java.sql package to use JDBC
    import java.sql.*;

    class Employee
    {
      public static void main (String args [])
           throws SQLException
      {
        // Load the Oracle JDBC driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

        // Connect to the database
        // You can put a database name after the @ sign in the connection URL.
        Connection conn =
          DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");

        // Create a Statement
        Statement stmt = conn.createStatement ();

        // Select the ENAME column from the EMP table
        ResultSet rset = stmt.executeQuery ("select ENAME from EMP");

        // Iterate through the result and print the employee names
        while (rset.next ())
          System.out.println (rset.getString (1));

        // Close the RseultSet
        rset.close();

        // Close the Statement
        stmt.close();

        // Close the connection
        conn.close();
      }
    }

### Inserting Names into the EMP Table--InsertExample.java

This sample uses a prepared statement to insert new employee rows into the `EMP` table.

    /* This sample shows how to insert data in a table.*/

    // You need to import the java.sql package to use JDBC
    import java.sql.*;

    class InsertExample
    {
      public static void main (String args [])
           throws SQLException
      {
        // Load the Oracle JDBC driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

        // Connect to the database
        // You can put a database name after the @ sign in the connection URL.
        Connection conn =
          DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");

        // Prepare a statement to cleanup the emp table
        Statement stmt = conn.createStatement ();
        try
        {
          stmt.execute ("delete from EMP where EMPNO = 1500");
        }
        catch (SQLException e)
        {
          // Ignore an error here
        }

        try
        {
          stmt.execute ("delete from EMP where EMPNO = 507");
        }
        catch (SQLException e)
        {
          // Ignore an error here too
        }

        // Close the statement
        stmt.close();

        // Prepare to insert new names in the EMP table
        PreparedStatement pstmt =
          conn.prepareStatement ("insert into EMP (EMPNO, ENAME) values (?, ?)");

        // Add LESLIE as employee number 1500
        pstmt.setInt (1, 1500);          // The first ? is for EMPNO
        pstmt.setString (2, "LESLIE");   // The second ? is for ENAME
        // Do the insertion
        pstmt.execute ();

        // Add MARSHA as employee number 507
        pstmt.setInt (1, 507);           // The first ? is for EMPNO
        pstmt.setString (2, "MARSHA");   // The second ? is for ENAME
        // Do the insertion
        pstmt.execute ();

        // Close the statement
        pstmt.close();

        // Close the connecion
        conn.close();
      }
    }

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index