• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on the Data class

 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
 
Rashid
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please let us know from where you have downloaded the assignment. I, too, wanna proceed
Thanx
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic