Programming Exercise IV Construction (Cont.)

  1. Database Accesses Embedded in Java (JDBC)
  2. The JDBC program ListRoutes.java lists the routes from the routes table if each of them contains any of the stops in command-line arguments args[i] such as
     /usr/bin/java  -Djava.security.egd=file:/dev/./urandom  \
       ListRoutes UND School
    The JDBC program then issues the following SQL command:
      SELECT DISTINCT rs.rid FROM route_stop rs, stop s
        WHERE ( UPPER(s.name) LIKE '%UND%' OR UPPER(s.name) LIKE '%SCHOOL%' )
          AND s.sid = rs.sid ORDER BY rs.rid
    The method trim of the class String returns a copy of the string, with leading and trailing whitespace omitted. Without using the Java option:
       -Djava.security.egd=file:/dev/./urandom
    the JDBC program ListRoutes.java would be very slow and inconsistent because of an issue with the Oracle JDBC driver which uses a blocking random number generator by default on Linux (check the Stackoverflow).

    ~/public_html/cgi-bin/280/4/ListRoutes.java
    /*******************************************************************
    *                                                                  *
    *  ListRoutes.java                                                 *
    *  This program lists the routes including the bus stop keywords.  *
    *                                                                  *
    *******************************************************************/
    
    // Import the following packages and class to use JDBC.
    import  java.sql.*;
    import  java.io.*;
    import  oracle.jdbc.*;
    import  oracle.jdbc.pool.OracleDataSource;
    
    public class  ListRoutes {
      public static void  main( String args[ ] ) throws SQLException {
        String user     = "C##user.id";
        String password = "password";
        String database = "65.52.222.73:1521/cdb1";
    
        // Open an OracleDataSource and get a connection.
        OracleDataSource ods = new OracleDataSource( );
        ods.setURL     ( "jdbc:oracle:thin:@" + database );
        ods.setUser    ( user );
        ods.setPassword( password );
        Connection conn = ods.getConnection( );
    
        try {
          // Create, compose, and execute a statement.
          Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
          String query;
          query  = "SELECT DISTINCT rs.rid FROM route_stop rs, stop s";
          query += " WHERE ( ";
          if ( args.length == 0 )
            query += "UPPER(s.name) LIKE '%%'";
          else
            for ( int i = 0; i < args.length; i++ ) {
              query += "UPPER(s.name) LIKE '%" + args[i] + "%'";
              if ( i != args.length-1 )  query += " OR ";
            }
          query += " ) AND s.sid = rs.sid ORDER BY rs.rid";
          System.out.println( query );
          ResultSet rset = stmt.executeQuery( query );
    
          if ( !rset.next( ) )
            System.out.print( "Nothing is found!" );
          else {
            System.out.print( "The following routes contain the stops: " );
            rset.beforeFirst( );
            // Iterate through the result and print the data.
            while ( rset.next( ) )
              System.out.print( "Route " + rset.getInt(1) );
          }
          // Close the ResultSet and Statement.
          rset.close( );
          stmt.close( );
        }
        catch ( SQLException ex ) {
          System.out.println( ex );
        }
        // Close the Connection.
        conn.close( );
      }
    }

  3. Testing the Exercise Thoroughly



      “Even strength must bow to wisdom sometimes.”    
      ― Rick Riordan, The Lightning Thief