_Database Management and Java_
by Art Sulger

Listing One
//---------------------begin source file--------------
import java.util.*;
import java.io.*;
import java.lang.*;

public class testX
   {
   public static void main(String [] args)
      {
      try{
       DataInputStream in = new DataInputStream(System.in);
       int i;
       String fname, outName;
       if (args.length < 1) 
          {
          System.out.println
          ("this extracts rows from an xBase file\n" +
           "you will be asked for a filename, column number and value,\n" +
           "the program will create a new xBase file composed of rows\n" +
           "that match the search\n");
          System.out.print("enter xBase file to read:");
          System.out.flush();
          fname = in.readLine();
          } 
       else 
          fname = args[0];
       Table t = new xTable(fname,true); 
       System.out.print("enter xBase file to create:");
       System.out.flush();
       outName = in.readLine();
       xColumn[]xcols = new xColumn[t.columnCount()];
       int iOffset = 1;
       for (i = 1;i <= t.columnCount();i++)
          {
          xcols[i - 1] = new xColumn(new StringBuffer(),iOffset,
          t.columnLength(i),t.columnName(i),
          t.columnDomain(i),
          t.columnDecimals(i)," ");
          iOffset += t.columnLength(i);
          }
       Table newX = new xTable(outName, xcols);
       for (i = 1; i <= t.columnCount(); i++)
          {
          System.out.print(i + ": " + t.columnName(i) + "\t");
          }
       System.out.print("\nenter column number to search on,\n" +
       "or enter 0 (zero) to skip the search test: ");
       System.out.flush();
       int C = 0;
       try{
          C = Integer.parseInt(in.readLine());
          }
       catch(NumberFormatException e)
          {
          System.out.println
             ("Invalid Number Format:" + e.getMessage());
          System.exit(0);
          }
       if (C != 0) 
          {
          if(!t.isColumn(C))
             {                  
             System.out.println("Invalid column number " + C);
             System.exit(0);
             }
          System.out.print
          ("enter string or string prefix to search for: ");
          System.out.flush();
          String searchTerm = in.readLine();
          System.out.println(new Date());
          String cName = t.columnName(C);
          System.out.println
          ("looking for " + searchTerm + " in " + cName);
          System.out.flush();
          try{
             while(t.next())
                {
                if (t.isMatch(C, searchTerm))
                   {
                   newX.newRow();
                   for (int j = 1; j <= t.columnCount(); j++)
                      {
                      System.out.print(t.display(j) + "\t");
                      newX.assign(j, t.display(j));
                      }
                   newX.write();
                   System.out.println(" ");
                   System.out.flush();
                   }
                }
             t.close();
             newX.close();
             System.out.println(new Date());
             System.out.flush();
             }
          catch (DatabaseException e)
             {System.out.println(e.getMessage());}
          }
       }
      catch (DatabaseException e)
         {System.out.println(e.getMessage());}
      catch (IOException e)
         {System.out.println(e.getMessage());}
      }
  } // end of class


Example 1: 

columns = new Columnizer[colCount] ;//interface array 
xcolumns = new xColumn[colCount] ;//xbase columns 
for (int i = 0; i < colCount; i++)
   { 
   xcolumns[i] = new xColumn(...  
   columns[i] = xcolumns[i]; 
   }


Example 2:

(a) 
static final int RSN = 13;//bit number 13 in the set 
static final int TIMESTAMP  = 21;//bit number 21 . . .  
BitSet ColType = new BitSet(26);//a set of 26 bits
ColType.set(RSN);// set the 13th bit 
ColType.set(TIMESTAMP);// set the 21st bit 
// do something if column is RSN or TIMESTAMP: 
if (ColType.get(RSN) | ColType.get(TIMESTAMP)) ...

(b) 
enum eElementType {
 . . .  
RSN      =0x1000,    //bit 13 (00000000001000000000000) 
TIMESTAMP=0x00100000,//bit 21 (00100000000000000000000) 
 . . . }ColType; 
ColType = RSN & TIMESTAMP; // makes the assignment 
if (ColType & (RSN | TIMESTAMP))...


Example 3: 

Table method       java.sql.ResultSet method 
close              close 
findColumn         findColumn 
display            getXXX //depending on domain 
next               next 
AreNulls(int[])    wasNull(for each column) 
isNull(int)        wasNull 
open               new ResultSet

Example 4: 

while (*(start_of_store) == ' ') // strip leading space 
if (*(start_of_store++) == '\0')                     
   break ;
while (*Value == ' ') // strip leading space           
if (*(Value++) == '\0')                              
   break ;


Example  5: 

try{ 
   for (i = 0, j = 0; i < iRowLength; i++, j++) 
      { 
      if (j < sbRow.length()) 
         sbRow.setCharAt(i, (char)readBuffer[j]); 
      else
         sbRow.setCharAt(i, ' '); } 
      } 
 catch(StringIndexOutOfBoundsException e)...

Example  6: 

ByteArrayOutputStream bo = new ByteArrayOutputStream(4);
DataOutputStream d1 = new DataOutputStream(bo); 
d1.writeInt(inInt);
// write integer on byte array 
ByteArrayInputStream bi = new
   ByteArrayInputStream(bo.toByteArray()); 
byte[]b=new byte[4];
bi.read(b,0,4); // read the byte array 
byte[]newB=new byte[4]; // new array for transposing 
newB[0]=b[3];   
newB[1]=b[2]; 
newB[2]=b[1];
newB[3]=b[0]; 
ByteArrayInputStream newBi = new
   ByteArrayInputStream(newB); 
DataInputStream di = new
   DataInputStream(newBi);//array to int return di.readInt();


Example 7: 
public class DatabaseException extends Exception 
   { 
   public DatabaseException()
      {  
      super(); 
      }  
   public DatabaseException(String error) 
      { // you can make your exception be a gui dialog 
        // or a command line printout 
        // this invokes the system message 
        // then prints an error in the java console
      super(); System.out.println("Database error:" + error); }  
      }


2



