Examples: dataset package

Examples: dataset package

These examples do not have any associated UI and are intended as code snippets that demonstrate how a particular class or method is used. The examples require the following lines of code at the top of your class file:

package borland.samples.examples;
import borland.jbcl.dataset.*;
import borland.sql.dataset.*;
import borland.jbcl.util.*;
import java.sql.*;


public class DataSetExamples {
//
// Copy any of the following code snippets here 
//
}

Example: DataSet.inBounds(), first(), next(), last(), prior()

This example shows usage of DataSet inBounds(), first(), next(), last(), and prior()

  public static final void inBoundsExample(DataSet dataSet)
    throws DataSetException
  {
    // Go to first row and then navigate through all the rows.
    //
    dataSet.first();
    while(dataSet.inBounds()) {
      //
      // Application logic...
      //
      dataSet.next();
    }

    // DataSet.inBounds() can also be used when traversing backwards.
    //
    dataSet.last();
    while(dataSet.inBounds()) {
      //
      // Application logic...
      //
      dataSet.prior();
    }
  }

Example: ParameterRow class

This example shows how to use the ParameterRow class to supply parameter values for your parameterized query.

  public static final void parameterRowExample(Database database)
    throws DataSetException
  {
    QueryDataSet dataSet = new QueryDataSet();

    // For efficiency, set these up once.
    //

    String lowDate   = "lowDate";
    String highDate  = "highDate";
    String jobCode  =  "jobCode";

    // Set up the parameter row values. (Normally done through query
    // property editor inside jbInit() method. 
    //
    ParameterRow parameterRow = new ParameterRow();
    parameterRow.addColumn(lowDate, Variant.TIMESTAMP);
    parameterRow.addColumn(highDate, Variant.TIMESTAMP);
    parameterRow.addColumn(jobCode, Variant.STRING);

    // Nonsense query that shows and/or compares the same column.
    // Comparison can be performed by having multiple columns in the
    // ParameterRow.  Note that JDBC '?' question parameter markers
    // can be used, but then parameter binding is left to right.
    //
    String query = "select * from employee where (HIRE_DATE >= :lowDate and HIRE_DATE = <:highDate) or JOB_CODE = :jobCode";

    // Last two booleans are executeOnOpen and asynchronouseExecution. 
    // Both disabled.  Call QueryDataSet.executeQuery() to get query running.
    //
    dataSet.setQuery(new QueryDescriptor(database, query, parameterRow,
false, false));

    dataSet.open();

    // Note that you can always get the associated parameterRow from a
    // Query/ProcedureDataSet by calling getParameterRow();
    //
    parameterRow  = dataSet.getParameterRow();

    // Set Parameters.
    //
    parameterRow.setTimestamp(lowDate, Timestamp.valueOf("1989-2-6 12:00:00.0"));//2/6/89 12:00:00 AM"));
    parameterRow.setTimestamp(highDate, Timestamp.valueOf("1989-4-17 12:00:00.0"));//4/17/89 12:00:00 AM"));
    parameterRow.setString(jobCode, "Admin");

    dataSet.executeQuery();


    // Check that example is still working correctly.
    //
    Diagnostic.check(dataSet.getRowCount() == 6);
  }

Example: PickListDescriptor

This example shows how to create a PickList.

  public static final void pickListExample(Database database)
    throws DataSetException
  {
    QueryDataSet country  = new QueryDataSet();
    QueryDataSet employee  = new QueryDataSet();
    employee.setQuery(new borland.jbcl.dataset.QueryDescriptor(database,
"select * from employee", null, true, false));
    country.setQuery(new borland.jbcl.dataset.QueryDescriptor(database,
"select * from country", null, true, false));

    // Normally this code would be generated by a PickList property
    // editor.  Currently there is no PickList property editor so code
    // like the following must be added to the jbIinit().
    // Uses the same "code patterns" normally generated in jbInit by
    // designer. This example simulates code generated for a persistent
    // column by the JBuilder UI designer.

    // Normally added when a column is made persistent in the designer.
    //
    Column column1  =  new Column();
    // Normally added when a column is made persistent in the designer.
    //
    column1.setColumnName("JOB_COUNTRY");
    column1.setDataType(borland.jbcl.util.Variant.STRING);

    // Add this line by hand to the jbInit() method because designer has
    // no property editor for PickList.  "country" is the DataSet to choose from,
    // the first String array is the list of columns from country to fill in from.
    // The second String array is the list of columns to display from country.
    // The third String array is the list of columns to fill into the employee
    // DataSet.  With this property setting, all model-view controls
    // (only GridControl, ListControl, FieldControl) show a java.awt.Choice 
    // derivation called borland.jbcl.control.PickListItemEditor when the value
    // in the column is edited.
    //
    // This editor is simplistic and uses only  the first entry in
    // all the String arrays passed to the PickListDescriptor.  It is easy to plug in
    // replacement editors (as they are created by Borland or third-party developers),
    // by setting the Column.ItemEditor property.
    // See the source code for PickListItemEditor (available with JBuilder Professional
    // and Client/Server editions) to get more info on creating custom editors.
    //
    // Note that you can also associate a custom ItemPainter for a Column by setting the
    // Column.ItemPainter property.
    //
    //
    column1.setPickList(new PickListDescriptor( country,
												new String[]{"COUNTRY"},
                                                new String[]{"COUNTRY"},
                                                new String[]{"JOB_COUNTRY"},
                                                false) );
    // Normally added when a column is made persistent in the designer.
    //
    employee.setColumns(new Column[] {column1});


    // Note:  The only code you really had to add was the
    //        column.setPickList() method call.
    //        All the rest could have been generated in the designer.
  }