jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes problem with using rs.getString() in servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem with using rs.getString() in servlet." Watch "problem with using rs.getString() in servlet." New topic
Author

problem with using rs.getString() in servlet.

raghu yeggina
Greenhorn

Joined: Feb 11, 2009
Posts: 27
Hi Everyone,

I've got a problem with using rs.getString() in my servlet.

for example:

I'm able to successfully compile a program that includes the following code:

and when i run the application i can see the output "test mail subject" ,which is the value coming successfully from the database on the server console.

But when i write where 'Mail' is a javabean

i'm getting an sql exception saying 'no data found'.

its working fine if i hard code it like this

can any one please help me to sort out why i'm able to print the mail subject but cannot pass it into a javabean setter

method. Thanks in advance












Alejandro Casarrubias
Greenhorn

Joined: Sep 21, 2006
Posts: 12

once i had a similar problem.
I fixed saving the rs.getString(1) into a variable and just calling the variable.


Hope this help you.


SCJP 5.0
raghu yeggina
Greenhorn

Joined: Feb 11, 2009
Posts: 27
tried it too.. but not successful..
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

raghu yeggina wrote:
and when i run the application i can see the output "test mail subject"



this is your exact output? if yes, then how?

raghu yeggina wrote:

i'm getting an sql exception saying 'no data found'.


i guess, you need to check your query

raghu yeggina wrote:

its working fine if i hard code it like this



then it should work with m.setSubject(rs.getString("")); also
raghu yeggina
Greenhorn

Joined: Feb 11, 2009
Posts: 27
Hi Seetharaman,

I'm calling the MailsDAO class from my ViewMailServlet and in that MailsDAO class i'm printing the output on the tomcat console to trace out the request processing.

In that course i'm printing the rs.getString(1),rs.getString(2),rs.getString(3) successfully on the tomcat console.

and when i run the application i can see the output "raghu@noah.com" and similarly for the remaining column values

Later i'm creating a Mail class object which is a java bean and then trying to set the values retrieved by the query using the Mail's setter methods..

this is not the output i need. i want to return a Mail object with its properties values set with the values retrieved from the query.

i dont think that the query is wrong because as i specified earlier i was able to print the resultant column values of the query.

Anyway here's the code




and the error is 'internal server error :500' on the webpage

and 'no data found ' on the tomcat console



Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



System.out.println("rs.getString(1)"+rs.getString(1));//prints raghu@noah.com
System.out.println("rs.getString(2)"+rs.getString(2));//prints test subject1
System.out.println("rs.getString(3)"+rs.getString(3));//prints test content



Always the best practise is to store the resultsets values to local varibales and use the variables hence forth. This is because ResultSet implementation differs from one driver implementation to another.. and you cannot rely on calling the rs.getString(1) more than once will yeild resultts always..

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

mmm.... when you get the problem, the if (rs.next) loop executed ? i doubt on your ResultSet .
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

as a previous post mentioned, you should only call getXXX() once for each value.
Therefore your should convert the ResultSet into some variables, and then use the variables.

From the API:


For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.
raghu yeggina
Greenhorn

Joined: Feb 11, 2009
Posts: 27
Hi Everyone,

thanks for your valuable posts.. i'm able to resolve the issue as per the suggestions that we should store the result set values in local variables and access the local variables further.

Thanku Alejandro,Seetharam,Balu Sadhasivam and David O'Meara.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with using rs.getString() in servlet.
 
Similar Threads
Yahoo Mail is Rejecting My Mail Server
JAAS and Tomcat container managed authentication
error on get from resultset
HTML in Desktop.mail(URI)?
Values from bean to a servlet