Slide 10.7: Calling a CGI script (Perl)
Slide 10.9: Using Oracle9.2.0i on the server with an IP address 172.20.4.9
Home

How to Construct the Exercise II (Cont.)

  1. Database Accessing Embedded in Java Host Language (JDBC) The JDBC program ListTitles.java lists the book titles from the books table if each of them contains the name from the Web, a command-line argument args[0]. If the name is empty, then list all titles. The method trim( ) of the class String returns a copy of the string, with leading and trailing whitespace omitted.

    ~wenchen/public_html/cgi-bin/351/week10/ListTitles.java
    /*********************************************************
    
      This program shows how to list the book titles in the
        books table.
    
      To use this program, you need to create a table
        books by using the following command:
    
      SQL> create table  books (
        2    title     varchar(128)  not null,
        3    ISBN      char(12)      primary key,
        4    authors   varchar(128)  not null,
        5    price     number(5,2)   not null  check( price    >= 0.0 ),
        6    quantity  integer       not null  check( quantity >= 0 ) );
    
      Table created.
    
    *********************************************************/
    
    // You need to import the java.sql package to use JDBC.
    import  java.sql.*;
    import  java.io.*;
    
    class  ListTitles {
      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:thin:@172.20.4.9:1521:aero", "userid", "password" );
    
        try {
          Statement stmt = conn.createStatement ( );
          String query = "select  title  from  books  where ";
          query += "authors  like  '%" + args[0].trim( ) + "%'";
          System.out.print( "<em>" + query + "</em><br /><br /><br />" );
          ResultSet rset = stmt.executeQuery( query );
    
          // Iterate through the result and print the data.
          while ( rset.next( ) ) {
            System.out.print  ( rset.getString( 1 ) );
            System.out.println( "<br />");
          }
          // Close the Statement and ResultSet.
          stmt.close( );
          rset.close( );
        }
        catch ( SQLException ex ) {
          System.out.println( ex );
        }
        // Close the connection.
        conn.close( );
      }
    }