Slide 13.9: Oracle listeners
  Slide 13.10: Web hosting
  Home


How to Construct My Memento (Cont.)

  1. Database Accessing Embedded in Java Host Language (JDBC) The following JDBC program shows how to list the memento items from the memento table by reading the keyword from the file p1. If the keyword (or the file p1) is empty, then list all messages.

     ~wenchen/public_html/cgi-bin/handheld/wml/ListItems.java 
    /*******************************************************************
    
       This program shows how to list the memento items in the
         memento table.
    
       To use this program, need to create a table memento
         by using the following command:
    
       SQL> create table  memento (
         2    time     varchar(32) not null,
         3    temp     number not null,
         4    message  varchar(128) not null);
    
       Table created.
    
    ********************************************************************/
    
    // Import the java.sql package to use JDBC.
    import  java.sql.*;
    import  java.io.*;
    
    class  ListItems {
      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.2.253:1521:aero", "userid", "password" );
    
        try {
          // Read the parameter keyword from the file p1.
          FileInputStream    stream     = new FileInputStream( "p1" );
          InputStreamReader  iStrReader = new InputStreamReader( stream );
          BufferedReader     reader     = new BufferedReader( iStrReader );
          String  keyword = reader.readLine( );
          if ( keyword == null )  keyword = "";
          else  keyword = keyword.trim( );
    
          try {
           	// Create, compose, and execute a statement.
           	Statement stmt = conn.createStatement( );
           	String query = "select time, temp, message from memento where message like '%";
           	query += keyword + "%'";
           	ResultSet rset = stmt.executeQuery( query );
    
           	// Iterate through the results and print the data.
           	while ( rset.next( ) ) {
              System.out.print   ( rset.getString( 1 ) + "<br />");
    	  System.out.println ( rset.getInt   ( 2 ) + " degrees<br />");
    	  System.out.println ( rset.getString( 3 ) + "<br /><br />");
    	}
    	// Close the Statement and ResultSet.
    	stmt.close( );
    	rset.close( );
          }
          catch( SQLException ex ) {
            System.out.println( ex );
          }
          // Close the FileInputStream.
          stream.close( );
        }
        catch( IOException ex ) {
          System.out.println( ex );
        }
        // Close the Connection.
        conn.close( );
      }
    }