|
To achieve
"physical data independence" ,
the program must:
|
You are warned that the procedure is quit tedious (a lot of "if"-statements)
Name of a field 24 chars
DataType of the field 4 chars
Length of the field int
|
Sample meta data:
24 chars 4 chars int
<-----------------> <-----> <---->
SSN C 9
Name C 30
Department C 10
Salary I 4
|
We will use the following class to store one entry of meta data:
class DataDescription // Format of the Meta Data (description of the data)
{
String fieldName;
String fieldType;
int fieldSize;
}
|
/* ------------------------------------------------------------
(1) Variables used to store the DESCRIPTION of the data
------------------------------------------------------------ */
static DataDescription[] dataDes = new DataDescription[MAXNFIELDS];
static int n_fields; // Actual number of fields
// This is a CONSTRUCTOR method for static variables
static
{
// Need to create objects for the dataDes[]
for ( int i = 0; i < MAXNFIELDS; i++ )
{
dataDes[i] = new DataDescription(); // This is how you make
// an array of objects in Java
}
}
|
(The name of the meta data file is db-description )
How to read in the Meta Data (stored in the file "db-description")
public static void main(String[] args) throws IOException
{
DataFile descrFile = new DataFile("db-description");
// 1. Open the data description file
/* -------------------------------------------------------
Read in the data description and store them in the
DataDes[] array (define in (1))
------------------------------------------------------- */
n_fields = 0; // Count the actual number of fields in data
while ( true )
{
try
{
/* ======================================
Read the next field description entry
====================================== */
dataDes[n_fields].fieldName = descrFile.ReadString(24);
dataDes[n_fields].fieldType = descrFile.ReadString(4);
dataDes[n_fields].fieldSize = descrFile.ReadInt();
System.out.println("Field: " + dataDes[n_fields].fieldName
+ ", type: " + dataDes[n_fields].fieldType
+ ", size: " + dataDes[n_fields].fieldSize);
n_fields++; // Count # fields in the format
}
catch ( IOException e )
{
/* ----------------------------------------------------------------
When you reach the end of the file, it will raise this exception
---------------------------------------------------------------- */
System.out.println("\nFinish reading data description file....\n");
break; // Exit loop: no more data !!!
}
}
.....
}
|
|
How to run the program:
|