Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data: Unexpected database access problem

 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have developed a standalong application using JTable to show the initial flight information.
I am getting this Data: Unexpected database access problem when I run the application.
The code is as follows :
String os = ( String ) System.getProperty("os.name");
// if it is windows system
if ( os.startsWith("W") ) {
dbPath = "\\assignment\\scjd\\starting\\classes\\suncertify\\db\\db.db";
} // else if it is unix system
else {
dbPath = "/assignment/scjd/starting/classes/suncertify/db/db.db";
}
db = new Data(dbPath);
} catch(java.io.IOException io)
{}
String[] columnNames = { "Flight #" , "Origin Airport" , "Destination Airport" , "Carrier #" , "Price" , "Day" , "Time" , "Duration" , "Available Seats" };
int count = db.getRecordCount();
rowData = new String[count][9];
DataInfo row = null;

for ( int i = 1; i < count; i++ ) {
try {
if ( null != ( row = db.getRecord(i) ) ) {
rowData[i] = row.getValues();
}
} catch(DatabaseException dbe)
{
System.out.println(dbe.getMessage());
}
}
Data: Unexpected database access problem
Data: Unexpected database access problem
Data: Unexpected database access problem
Data: Unexpected database access problem
Data: Unexpected database access problem
Data: Unexpected database access problem ......

Any solutions ?
Thanks in advance.
-- Ravindra
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getRecord is returning this error for an IOException.
It getRecord calls the readRecord method. Did you fix the deprecated method there?
I'd look there. And my spider senses are saying to me that there is an easier way to get the file path, and file without having to find out what Operating system they are using. And it says it might be easier if the db.db file resided in the same directory as where you start your app.
Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark
I am getting this data access error after I tried to fix the deprecated method as given below.
//rv[i] = new String(buffer, 0, offset, description[i].getLength());
String enc = "ISO-8859-1";
rv[i] = new String(buffer, 0, offset, enc);
//newData[i].getBytes(0, toCopy, buffer, offset);
String enc = "ISO-8859-1";
newData[i].getBytes( enc );
Let me know the correct way if this is wrong.
And my spider senses are saying to me that there is an easier way to get the file path, and file without having to find out what Operating system they are using. And it says it might be easier if the db.db file resided in the same directory as where you start your app
I am testing the FBN in standalone mode. I have three folders client , data and server. I am runing FBNClient in client folder and db.db is located in data . Does this mean that db.db should be located in client folder for standalone mode ?
Thanks
-- Ravindra
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//rv[i] = new String(buffer, 0, offset, description[i].getLength());
String enc = "ISO-8859-1";
rv[i] = new String(buffer, 0, offset, enc);

Which String constructor is this? I can't find it in my API documentation. It looks like the same one that they had in there originally which is deprecated.
Did you compile it with the -deprecated argument in javac?
Look again
Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am referring to JDK 1.2 API . This is what it says...
public String(byte[] ascii,int hibyte,int offset,int count)
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a character-encoding name or that use the platform's default encoding.
String
public String(byte[] bytes,int offset,int length,String enc) throws UnsupportedEncodingException
Construct a new String by converting the specified subarray of bytes using the specified character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the subarray.

-- Ravindra
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's fine to use that constructor. There is still something in your coding of it that is throwing me off somewhere.
rv[i] = new String(buffer, 0, offset, enc);

and
public String(byte[] bytes,int offset,int length,String enc)

OK, the byte array as the first parameter is correct.
the second parameter is the offset, which you have as the third parameter. The offset is assigned 1, and you are passing 0 as the second parameter. Let alone it is a Magic Number.
So then for length you have the offset variable being passed. The values to the constructor you are passing are wrong.
try



Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark
It has worked after I made the following change in the code.
String enc = "ISO-8859-1";
rv[i] = new String(buffer, offset, description[i].getLength(), enc);
Thanks
Ravindra
 
Najib Coutya
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we really need the encoding at the end?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Najib Coutya:
Do we really need the encoding at the end?


Nah not really. I didn't.
Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it wrong if I use it ?
Thanks
Ravindra
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope it's neither right or wrong if you use it or not. The only time you really have to use it is when you application is going to be multi-national. Meaning it will run in China, Japan, Russia, France, Italy, Spain, and USA.
I think
Mark
 
Najib Coutya
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Thanks for your reply.
Even when the FlyByNight is going to be multi-national, encoding alone is not going to solve it. We probably need to change the readUTF and writeUTF as well as selecting an encoding from the environment. Because my understanding is that they force UTF encoding.
Is that a right assumption?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic