Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Display error when record not found

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I seem to have a problem at which i have been working on for about five hours and cant seem to get to a solution.

This is what i am doing, i have a search field which queries a database and displays the results of its search in a table below it. The problem is that when i search for a non existent record i want to display an error message that says a record was not found.

I have tried the following:
1. Surrounded the code with a try catch which caught SQLExceptions, though when a non existent record was searched no error was caught.

2.I tried to somehow count the returned record count from the resultset and if the count was zero display the error, but had no luck there either

Here is the code:


I thank everyone in advance.

Thank you
Talhah Mafawalla
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried to somehow count the returned record count from the resultset and if the count was zero display the error, but had no luck there either


How did you count the number of records in the result set?
 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitesh Kant,

That was the problem i couldnt find a way to count the number of records in the record set. Though that would have made it easy.

I have solved the problem by using the following code which forces the compiler to throw an error when there is no records in the record set:
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which forces the compiler to throw an error when there is no records in the record set.


What is the exception that is thrown?
Okie, as far as i think, you should increment a counter inside the while(rs.next()){} loop. If after the loop execution, the counter value is zero, that means that there are no records in the resultset and you can print the same on the screen.
Do let me know as to which exception is thrown by the code when the result set does not have any rows.
 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitesh,

The error that i got using ex.getMessage() was:

[Microsoft][ODBC Driver Manager] Invalid cursor state
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Talhah,

in your jsp it is not going to print "no records found"
because no exception is thrown.
when the resultset does not contain any rows it never goes
into the while loop
so before you go into the loop check whether there is a row
in the resultset and if not there display the message.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- Avoid using scriptlets/Java code in JSPs
- Avoid using DB stuff in JSPs

Anyways, you can use a VO, often known as TO, instead of directly using resultset for rendering.

Otherwise take a boolean variable say, hasValues=false outside your while(rs.next()) and make it true under your loop. Then you can check for that variable. But its just a work around. There are many others as well.

Remove your workarounds, now. Do the trick with your original code.
[ April 09, 2007: Message edited by: Adeel Ansari ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nelson christos:
so before you go into the loop check whether there is a row
in the resultset and if not there display the message.



Thats the exact thing he was unable to do.
 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Adeel and Nelson,

I think i have got a solution by using a variable in the while loop to count the number of times it get executed, thus if the number is zero i displayed a "Record not found" message. Otherwise the table gets built.

Though Adeel would the reason you recommend not using scriptlets/Java code in JSPs and DB stuff in JSPs because of security reasons?

Should the database stuff go into a java class to get executed a then only return a result(e.g. I have a class that handles the database connection either for MySQL or Access by passing to it a string and it returns a Connection). Would this be what you are talking about?

Also what would "VO" and "TO" stand for. I have just started learning jsp and i am looking for good guidlines to follow (coding conventions etc.)

The code that i have produced now is as follows:


Sorry for posting so much code but i have made chages to various parts of the jsp file.

Thanks again for everyones help
Talhah Mafawalla

[ April 09, 2007: Message edited by: Talhah Mafawalla ]
[ April 09, 2007: Message edited by: Talhah Mafawalla ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Talhah Mafawalla:
Though Adeel would the reason you recommend not using scriptlets/Java code in JSPs and DB stuff in JSPs because of security reasons?



No, its not about security. Its about readability and ease of maintenance. Better say its about using the right tool in a right way. Since, JSP is a display technology we must not do other stuff in there.


Should the database stuff go into a java class to get executed a then only return a result(e.g. I have a class that handles the database connection either for MySQL or Access by passing to it a string and it returns a Connection). Would this be what you are talking about?



Yes. But, there are best practices to follow such as Connection Pooling, DAO, TO/VO, ORM Tools etc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic