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

How to Convert Long to String

 
Lijoy John
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Using the ResultSet Object i want to retrieve a data in a column , data type being 'Long'
While (rs.next()){
long ng = 1L;
ng = rs.getLong("Request_Data");
}
out.println(ng);
While doing this i am getting the foll. error:
java.SQLException:failed to convert to internal representation
Kindly let me know how I could print the value to the browser. Is it adbvisable to convert long to string, if yes then how can i proceed.
Any replies would be appreciated.
Thanks,
Regards,
John
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lijoy,
It doesn't seem to be a printing problem to me. You have placed the variable ng initialization inside of a loop which might be causing the problem because if there is more than one row and the "Request_Data" is not of type long in that row.
If printing if your problem then you can just do the following:
out.println( Long.toString( ng ) );
to get the long into a string.
Regards,
Manfred.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me there are two main possibilities: (1) the database has column "Request_Data" declared as some type incompatible with long. Check the declared type against this table to determine the best way to access it. Or (2) the driver you use simply doesn't know how to do this conversion properly. Try to find a better driver for your database. Or simply try using other methods like getInt() or getString() - one of them will probably work. For getString() you would then have to use Long.parse() to convert to long, which is less efficient then getting a long to begin with, but if that's what you need to do...
[This message has been edited by Jim Yingst (edited August 03, 2001).]
 
Lijoy John
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lijoy,
It doesn't seem to be a printing problem to me. You have placed the variable ng initialization inside of a loop which might be causing the problem because if there is more than one row and the "Request_Data" is not of type long in that row.
If printing if your problem then you can just do the following:
out.println( Long.toString( ng ) );
to get the long into a string.
Regards,
Manfred.
Hi Manfred,
Thanks for reply to my query.
I had initialised a long variable and then using that I wanted to store the data by using resultset.getLong("REQUEST_DATA");
I was retrieving 8 fields in total from the database and only this one was causing a problem. It seems that in Oracle the Long datatype is not similar to java long, so that could have been the problem. The datatype defined for REQUEST_DATA is Long and when i used :
String str = resultset.getString("REQUEST_DATA"), i was able to store it in the str variable and print it also.
out.println( Long.toString( ng ) ); is a better way to print the output on the browser.
Thanks for the timely reply and help.
Regards,
John
 
Lijoy John
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Jim,
Thanks for replying to my query. I had checked the dataype for the particular column REQUEST_DATA and it is of Long type.To retrieve the data from each column i had been using the getXXX method of the resultset. It seems that Long data type in Oracle and java is not the same as, It worked fine when i used rs.getString() instead of rs.getLong().
Thanking you once again for the timely help and advise given.
Regards,
John
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic