wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Data class in Beta Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Data class in Beta" Watch "Data class in Beta" New topic
Author

Data class in Beta

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Alright, I finally got my db.db file. Tough time finding time to do this.
Unfortunately I found the time because I have to work late, it's now midnight, to recreate tables that someone else here at work dropped from our production database.
OK, now to my question. Is anyone reusing the code they supplied in the Data class of the current SJCD in their beta Data class.
I am wondering if their offset and read and write methods work with this data format, or if I will have to spend more time reviewing the IO classes.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Cutting real close to the deadline now and I'm still way behind where you seem to be. I've been considering using New I/O stuff for the implementatio of the DB interface. I wasn't sure if the Sun reviewer would object to seeing code from the current SCJD assignment since the instructions say "you must not submit any code that is not your own work" but then again that is in the context of using an IDE. Of course we could easily change a few things around and maybe even improve on the current exam's code.
Are you using NIO or the old IO?
Did you create any sub-interfaces for DB? What I'm doing is extending DB because there aren't any methods in it to actually open the database or specify the location and I thought that would be something I'd want in the interface. I also included the "magic number" as a constant in my sub-interface. Any comments on this approach?


Junilu - [How to Ask Questions] [How to Answer Questions]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well the Magic number has to be a constant, since it is the only way to verify the db.db file as being a real db.
I plan on using the new IO in the Data class. However I used the old-fashion way to create my db.db file.
IO Channels are pretty cool and make things easier.
I am just worried about the null terminated strings in db.db and the offset, and how to determine the reading of records.
I think we need at least a DataInfo type class to store the data in memory. BUt offset and getting to the record you want to update/modify is tricky to me.
Mark
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Are you using NIO or the old IO? --Junilu Lacar
NIO for the network (SocketChannel & ServerSocketChannel) and old IO in the database for reading/writing the db.db. I'm thinking about migrating to New IO for everything, not quite sure yet...
Did you create any sub-interfaces for DB? --Junilu Lacar
I didn't because I let the DB open itself when the DB instance is created, and thus, I don't need the open() method in the interface.
I am just worried about the null terminated strings in db.db and the offset, and how to determine the reading of records. --Mark Spritzler
In my case, the header length (magic number, schema and stuff) takes 74 bytes. The record length 160 bytes. So I determine the offset within the file with the simple formula:
offset = headerLength + (recNum * recordLength)
and that works like a charm...
Concerning the null terminated string, I truncate them after reading them with something like:
readString = readString.substring(0,readString.indexOf('0'))
which has the effect of discarding all 0-bytes stuffed after the field value to reach the required field length.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
My header size is 70 bytes and record size is 184 bytes. Must have different schemas...


SCJP2
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Is SCJD Beta exam still available?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
My header size is 70 bytes and record size is 184 bytes. Must have different schemas...
Yes, there are different assignment versions.
Is SCJD Beta exam still available?
No, it's not. The beta assignments have to be turned in within two weeks.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Size - 170 or 184. My question about Null pointer is that I didn't manually put any Null Pointer in my db.db file
So if I try that Offset will I be off in my record reading.
Well I tried to copy and paste from my db.db file, but it says there are nulls there. So then how does it know that the null terminated field is the number of characters. Should you be able to use the Header lenght, then read N number of lines to get to the record?
Thanks
Mark
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
Hi Mark. I use RandomAccessFile.seek() to go where I want in the file. We know exactly which position the first record starts and where the next one begins. If your header size is 70 bytes and record size is 184 bytes, then seek(70+n*184) will point to the start of record n. Not sure if this is what you are trying to do...
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Mark,

Here is my code for create(String data[])
byte[] currentData=new byte[recordLength];
for(int i=0;i<fieldSize.length;i++){
toCopy=fieldSize[i];
byte []buff=data[i].getBytes();
int dataSpace = (buff.length >= toCopy) ? toCopy : buff.length;
for(int j=0;j<dataSpace;j++){
currentData[offset+j]=buff[j]; }
offset += toCopy;
}
db.write(currentData);

The main thing here is before writting any field i check the lenght of the field with the the actual lenght thats allocated for that field which we got from the db earlier if its length is greater than the length that allocated for that field then i use the field length that we have for that field from the db, So that we can be sure every record in the db file will be of same length.
For Reading:
------------
read(recNo)
{
setFilePointer(recNo);
byte [] buffer = new byte[recordLength];
db.read(buffer);
if (buffer[0] == VALID) {
rv = new String[fieldList.length];
for (int i = 0; i < fieldList.length; i++) {
String field = fieldList[i];
int maxSize = fieldSize[i];
rv[i] =
(new String(buffer, offset, maxSize)).trim();
offset +=maxSize;
}
}
}
I think there is no need of any explanation for the above code.
I belive this will help u Mark.
-rameshkumar
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Here's my code for creating the db.db file, If you notice the lines that go in for the data, I am not making sure that it is exactly the same number of character for each line. Is this going to cause the going to the record number calculation not work.

I removed all the data in the arrays, so it wouldn't take up much room. But I have 20 records that get added with different values.
Mark
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
Yeah I think that will create unequal record sizes. This is how I would make sure each record is the same size, using your example:
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Thanks Tybon, that is what I was looking for.
Mark
Dayanand Kangala
Greenhorn

Joined: Sep 09, 2002
Posts: 26
Hi All,
The following is what I used for creating db.db. Can you comment on this code?
import java.io.*;
import java.util.*;
public class DBConfig
{
public static int VALID_RECORD = 0;
public static int DELETED_RECORD = 0xFF;

public static void DBWrite(DataOutputStream dos, int fieldLen, String strValue) throws IOException {
char[] charField;
char[] valueField;
charField = new char[fieldLen];
valueField = strValue.toCharArray();
for (int i=0; i<valueField.length; i++)
{
charField[i] = valueField[i];
}
dos.writeBytes(new String(charField));
}
public static void main(String[] args)
{
try
{
File f = new File("suncertify.db");
FileOutputStream fos = new FileOutputStream(f);
// dataoutputstream is used to store the interger values in the
// data file
DataOutputStream dos = new DataOutputStream(fos);

File dbconfig = new File("dbconfig.txt");
FileReader fis = new FileReader(dbconfig);
BufferedReader br = new BufferedReader(fis);
StringTokenizer dbconfigLines;
String strLine = "";
strLine = br.readLine();
dbconfigLines = new StringTokenizer(strLine, "~");
strLine = dbconfigLines.nextToken();

// 4 byte numeric, magic cookie value
int intCookie = 1000;
// 2 bytes, number of fields in each record
short shortNFields = (short) Integer.parseInt(dbconfigLines.nextToken());
int[] fieldLen = new int[shortNFields];

System.out.println("Storing db-schema the schema description section");
// write the cookie
dos.writeInt(intCookie);
// write the no.of fields
dos.writeShort(shortNFields);
int count = 0;
while ((strLine = br.readLine()).charAt(0) != '#')
{
dbconfigLines = new StringTokenizer(strLine, "~");
strLine = dbconfigLines.nextToken();
// 1 byte numeric, length of field name
dos.write(strLine.length());
// n bytes (defined by previous entry), field name
dos.writeBytes(strLine);
strLine = dbconfigLines.nextToken();
// 1 byte numeric, field length in bytes
fieldLen[count] = Integer.parseInt(strLine);
dos.write(fieldLen[count]);
count++;
}
System.out.println("Storing sample records in the data section");
while ((strLine = br.readLine()) != null)
{
dos.write(DBConfig.VALID_RECORD);
dbconfigLines = new StringTokenizer(strLine, "~");
count = 0;
while (dbconfigLines.hasMoreTokens()) {
strLine = dbconfigLines.nextToken().replace('"', ' ');
strLine = strLine.trim();
DBConfig.DBWrite(dos, fieldLen[count], strLine);
count++;
}
}
br.close();
dos.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
I create a char array of the field length and then store this char array as bytes into the file. I read the following text file:
#fields~6
name~32
location~64
specialties~64
size~6
rate~8
owner~8
#records
"1"~"C"~"A,B"~5~"100"~""
"2"~"C"~"A"~5~200~""
"3"~"C"~"B"~10~150~""
Do I need to do any enhancements in the code? Any comments?
Dayanand.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
I don't mean to be the grump here, but is it a good idea to actually be posting code for the exam? AS I recall, this is not normally done here.
M, author
The Sun Certified Java Developer Exam with J2SE 1.4


Java Regular Expressions
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
No I wouldn't post or read code that will be handed in for evaluation. You don't have to submit the code for creating the db file. You don't even need to create the db file programmatically (use a hex editor if you want So I think in this case it's ok.
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
{ sb.setLength(0); sb.insert(0, names[i]); sb.setLength(FIELD_NAME_LENGTHS[0]);

Tybon, shouldn't the length of the String buffer be set to the length of the data fields?
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
You're right Ronnie. It should be FIELD_DATA_LENGTHS according to Mark's code. Thanks.
Dayanand Kangala
Greenhorn

Joined: Sep 09, 2002
Posts: 26
Originally posted by Max Habibi:
I don't mean to be the grump here, but is it a good idea to actually be posting code for the exam? AS I recall, this is not normally done here.
M, author
The Sun Certified Java Developer Exam with J2SE 1.4

Sorry for posting the code.
I just thought of getting some tips on improving the code!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

For the most part posting code that is to be submitted is frowned upon. Parts of code, little fragments are also ok.
However, this code is not turned in.
Mark
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Wait a minute! You got db.db file and I did not get...
Well Sun told me that I have to create it from scratch, they'll not provide it. And I have to provide at least 20 entries there.
Is this because I could download my assignment only 3 days earlier from now? Did you all get db.db file?


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Ashik,
I don't think anybody taking the BETA got a db.db file: we all have to create our data files from scratch. My requirements don't even specify that the data file must named db.db
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Data class in Beta