Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Ever Feel Like Life Is Just Scrolling By?

 
 

Articles Index


Aaron Alpar uses this code as an example of how to use the JDBC to implement a workaround for a scrolling cursor. It illustrates a number of important techniques, such as how to handle connections, rows, and columns. Enjoy!

 
// Very simple example of implementing  
// something that acts like a
// scrolling cursor in JDBC.  Don't use this as 
// an example of good 
// program structure, rather use it as an example 
// for how to get the job done.


class OrderTable extends Object {

     public double   orderID;    // variables for my
                                 // column values
     public String   orderDate;  // " "
     public String   description;// " "

    public  Connection   // connection for executing
       theConnection;
                          // SQL
    public  Vector  keys;// Vector that will hold my
                          // primary key values

//
// Primary Constructor
//
// This constructor primes the "keys" Vector
// with all of my primary keys from the order
// table. This is what provides 
// the ability to reference rows at random, 
// via indexing into the vector of primary key 
// values. The memory overhead of 
// loading an array of primary keys is very 
// small when compared to the option of 
     // keeping the entire row in memory. 
     //
     public OrderTable ( Connection aConnection ) {
       ResultSet                       aResult;

       theConnection = aConnection;
       // assign the instance's
       // JDBC connection

       keys = new Vector();
       try {
       // Exceute the SQL that will select all
       // of the desired keys, then load all of
       // those key values into the "keys"
       // Vector so I can index them later

       aResult = theConnection.prepareStatement(
         "select order_id from order"
         ).executeQuery();
             while( aResultSet.next() )
                keys.addElement ( new 
                Double(aResultSet.getDouble(1)) );
           } catch ( SQLException e ) {
            System.out.println ( e.getMessage() );
             }
      }

     //
     // loadElementAt This is the method that does
     // the other half of the work, loading the
     // column values into the instance variables.
     // Note that I reference the primary key in the
     // "keys" Vector.
     //
       
     public void loadElementAt( int anIndex ){
       ResultSet  aResult;
       Statement  aStatement;

       try {
         // Execute the SQL (using variable
         // replacement )
         
         aStatement = aConnection.prepareStatement
           ( "select order_id,
           order_date, description
           from order where order_id = ?" );
         aStatement.setDouble ( 1,
           (keys.elementAt( anIndex ));
           // this replaces the "?" with
           // the key value
           aResult = aStatement.executeQuery();
           aResult.next();
           orderID = aResult.getDouble (
           "order_id");
           // load the values one by one
           orderDate = aResult.getString (
             "order_date");
           // ...
           description = aResult.getString (
             "description");
           // ...
          } catch ( SQLException e ) {
               System.out.println ( e.getMessage() );
            }
      }
}


//
// init()
//
// Here's an example of how to use this ...
//

init()
{
  theConnection = ...Connection to JDBC database ...
  OrderTable theOrderTable = 
    new OrderTable( theConnection );
  theOrderTable.loadElementAt( 2 ); // load the row
                                    // at index #2
  System.out.println ( theOrderTable.description );
}