for the Java Programming Language - Classes
The CodeBase classes are extensive enough to provide a complete database solution
and still small enough to enable fast downloading in the Client/Server version. They represent a
balance between size, functionality and the ability to easily scale from standalone to
client/server. Of course because we provide full client-side source code, you can always
add to the Java classes any additional functionality you may require. The
following table lists the Java classes provided with all versions of CodeBase for the Java Programming Language:
Code4 Class
CodeBase uses the Code4 class to specify initialize CodeBase, and to set various process-wide
behaviors, such as locking and file access methods, and to perform actions such as connecting
to the database server (C/S only). Generally, only one Code4 class object is used in any one
application. Under client/server the Code4.connect() method must be executed successfully
before a Data4 table object can be constructed and used to manipulate a data file.
| Code4 ClassMethods |
| accessMode |
lockClear |
unlock |
| connect |
readLock |
unlockAuto |
| defaultUnique |
readOnly |
|
| lock |
safety |
|
Example using Code4 class (client/server)
import codebase.*;
import java.applet.Applet;
import java.io.IOException;
import java.net.UnknownHostException;
class Example1
{
public static void main(String args[])
throws Error4,UnknownHostException,IOException
{
Code4cs client = new Code4cs();
client.connect(null, 23165, "user1", "");
client.safety(true);
client.accessMode(Code4.DENY_WRITE);
Data4cs dataFile = new Data4cs(client,"NAMES");
dataFile.top();
System.out.println("At the top of the data file");
dataFile.close();
}
}
Data4 Class
The Data4 class contains instance methods that are used to store and retrieve information
from data files. Each data file has a current record and a selected index. The Data4 class
also keeps track of the end of file (eof) and the beginning of file (bof). The Data4
constructor can not be executed successfully until a Code4 object is created and initialize.
A new data file may be created by calling Data4.create() after the Data4 constructor has
been called. A particular data file may be opened more than once in an application and thus
there may be multiple instances of a Data4 class for a particular data file. Any desired
field objects may be created only after a data file has been opened or created.
| Data4 Class Methods |
| Data4 |
createIndex |
openIndex |
reindex |
| append |
go |
pack |
seek |
| blank |
lockAdd |
position |
seekUnicode |
| bottom |
lockAddAll |
positionSet |
select |
| close |
lockAddFile |
recCount |
top |
| create |
open |
recNo |
update |
Example using Data4 class:
import codebase.*;
import java.io.IOException;
import java.net.UnknownHostException;
class Example
{
public static void main(String args[])
throws Error4,IOException,UnknownHostException
{
int recCount;
Code4cs client = new Code4cs();
client.connect(null, -1, "user1", "");
Data4cs dataFile = new Data4cs(client,"Country" );
Field4stringBuffer country = new Field4stringBuffer(dataFile,"Country");
dataFile.bottom();
//now change the last record
country.set(StringBuffer("Italy"));
dataFile.update();
System.out.println(country.get());
dataFile.close();
}
}
Field Manipulation Classes
The various Field4... classes are used to access and store field values from the current
record of the data file. Each of these classes can be mapped to several different field types,
representing the various data types of the .DBF file format.
For every field in the data file that needs to be accessed, an appropriate object must be
created and associated with a field. Each of these objects has an instance variable called
contents that contains the value of that field from the current record. It is through the
variable contents that the field can be retrieved and modified directly. When the data file
is positioned to a new record, the field values of the new record are copied into the instance
variables of the field objects associated with the data file.
CodeBase contains the following Field classes:
- Field4stringBuffer
- Field4stringBufferUnicode
- Field4double
- Field4date
- Field4boolean
Field4stringBufferUnicode class can be mapped to any field type, which includes Character,
Date, Double, Logical, Numeric, Floating Point, Memo, General and Binary.
Field4double class can be mapped to Numeric, Floating Point and Character fields.
Field4date class can be mapped to a Date or Character field.
Field4boolean class can be mapped to Logical and Character fields.
Example using Field4 class:
import codebase.*;
import java.util.Date;
import java.io.IOException;
import java.net.UnknownHostException;
class Example
{
public static void main(String args[])
throws Error4,IOException,UnknownHostException
{
Code4cs client = new Code4cs();
client.connect("", -1, "user1", "");
Data4cs db=new Data4cs(client);
db.open("INFO");
Field4double age=new Field4double(db,"AGE");
Field4date bdate=new Field4date(db,"BIRTH_DATE");
db.top();
Date now = new Date();
long milliAge=now.getTime()-bdate.contents.getTime();
int dayAge=(int)(((milliAge/1000)/3600)/24);
System.out.println(bdate.get() + " "+now);
System.out.println("Age in days based on birth date: " + dayAge);
System.out.println("Age based on age field " + age.get());
db.close();
}
}
|