This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

problem with using rs.getString() in servlet.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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












 
Greenhorn
Posts: 12
Netbeans IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raghu yeggina
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tried it too.. but not successful..
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mmm.... when you get the problem, the if (rs.next) loop executed ? i doubt on your ResultSet .
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!