wood burning stoves 2.0*
The moose likes JSP and the fly likes Display error when record not found Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Display error when record not found" Watch "Display error when record not found" New topic
Author

Display error when record not found

Talhah Mafawalla
Greenhorn

Joined: Nov 05, 2006
Posts: 24
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
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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?


apigee, a better way to API!
Talhah Mafawalla
Greenhorn

Joined: Nov 05, 2006
Posts: 24
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

Joined: Feb 25, 2007
Posts: 1638

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

Joined: Nov 05, 2006
Posts: 24
Hi Nitesh,

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

[Microsoft][ODBC Driver Manager] Invalid cursor state
nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
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.


i think therefore i am
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
- 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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Nov 05, 2006
Posts: 24
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

Joined: Aug 15, 2004
Posts: 2874
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Display error when record not found
 
Similar Threads
Error comes whilke compiling the jsp page
Display error message in div tag
Selection Drop Down Question
urgent help in jsp taglib
Problem with iframe in mozilla browser