aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes question on the Data class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "question on the Data class" Watch "question on the Data class" New topic
Author

question on the Data class

ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
I am have downloaded my assignment recently and before going ahead with design / implementation , I thought of creating a sample client and invoke the different methods on Data class.
I am getting the following exception when I tried to create a new record in the database and followed by trying to modify
and delete
A new record is added successfully to the database
Exception while modifying the record in the database Attempt to create a duplicate key by modification
The record is deleted successfully in the database
The question is how the Data class does not let me modify the record but let me delete it.
My code is as follows
String[] addData = { "BB001" ,
"SFO" ,
"LHR" ,
"SpeedyAir ",
"2000 " ,
"Mon" ,
"11:20" ,
"11h65m " ,
"24 "
};
for ( int j = 0; j < addData.length; j++ ) {

int fieldLen = addData[j].length();
System.out.println("Field Value = "+addData[j]);
System.out.println("Field length = "+fieldLen);

}

try {
db.add(addData);
System.out.println("A new record is added successfully to the database");

} catch(DatabaseException dbe)
{
System.out.println("Exception while adding the record to the database "+dbe.getMessage());
}
// Create a DataInfo object
String[] modifyData = { "BB001" ,
"SEA" ,
"SFO" ,
"EVA ",
"2000 " ,
"Mon" ,
"11:20" ,
"11h65m " ,
"26 "
};

DataInfo dataRecord = new DataInfo(count, description, modifyData);
// This method updates the record specified by the record number
// Field in the DataInfo argument. The fields are all modified
// to reflect the values in that argument. If the key field
// specified in the argument matches any record other than the
// one indicated by the record number of the argument, then a
// RuntimeException is thrown.
try {
db.modify(dataRecord);
System.out.println("The record is modified successfully in the database");

} catch(DatabaseException dbe)
{
System.out.println("Exception while modifying the record in the database "+dbe.getMessage());
}
// This method deletes the record referred to by the record
// number in the DataInfo argument.
// Create a DataInfo object
String[] deleteData = { "BB001" ,
"SFO" ,
"LHR" ,
"SpeedyAir ",
"2000 " ,
"Mon" ,
"11:20" ,
"11h65m " ,
"26 "
};

dataRecord = new DataInfo(count, description, deleteData);
try {
db.delete(dataRecord);
System.out.println("The record is deleted successfully in the database");

} catch(DatabaseException dbe)
{
System.out.println("Exception while deleting the record to the database "+dbe.getMessage());
}


SCJP, SCJD, SCWCD, SCBCD, SCEA
Rashid
Greenhorn

Joined: Jan 09, 2002
Posts: 11
Can you please let us know from where you have downloaded the assignment. I, too, wanna proceed
Thanx
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Hi Rahid
You may download it by going to the Sun Certification database at www.galton.com/~sun
Once you have access, select the "test history" button and then click the "assignments" button and follow the links. You shouldn't have any problems getting to the instructions.
-- Ravi
HenkGijsbert
Greenhorn

Joined: Jan 07, 2002
Posts: 28
Hi Jannapareddy,
You get the modification error, because the record number (count in your code) and the key of the record are out of sync.
What the server now tries to do is modify the record with record number 'count', which would result in its key bein changed to "BB001", which already exists for another record (in fact the record you thought you where modifying, but you aren't!). This would result in a duplicate key, so an exception is thrown.
Regards,
Henk van Jaarsveld
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Hello HenkGijsbert
My mistake !
I have now inserted the following line of code to get the latest count and then proceeded to modify the record
// Gets the latest count i.e number of records stored in the database.

count = db.getRecordCount();
Now , It is working fine !
Thanks
-- Ravi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on the Data class
 
Similar Threads
NullPointerException
FBN :Exception Handling
unclosed string in db.db ?
suncertify.db.Data
FBN: criteriaFind(String criteria) method; is this ok ?