aspose file tools*
The moose likes JSP and the fly likes Test result set for listbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Test result set for listbox" Watch "Test result set for listbox" New topic
Author

Test result set for listbox

Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
I've been having problems with pages displaying when calling information that isn't there. Currently its a listbox created from a table in the database.

Code is:

try
{
con = pool.getConnection( );
Statement statement = con.createStatement( );
SQL = "SELECT * FROM tblHSUnits WHERE LandLordID = " + LandLordID;
ResultSet rs = statement.executeQuery( SQL );

while ( rs.next( ) )
{
UnitID = rs.getString("UnitID");
Address = rs.getString("Address1");
City = rs.getString("City");
%>
<option value="<%=UnitID%>"><%=Address%>, <%=City%></option>
<%
}

// this is where I'm trying to display a message when nothing exists in the database
if("".equals(UnitID))
{
%>
<option value="0" selected>No units to display</option>
<%
}

// close resultset
if ( rs != null ) { rs.close(); }
if ( statement != null ) { statement.close(); }

I really wanted to test the result set but couldn't get it to work. if(rs == null) and if(rs.length() == 0) didn't work, the latter gave me an error.

Is there a way to test a result set to see if nothing exists in it? A co-worker suggested doing a while (rs.next) loop and counting the rows and testing it if its 0 but that doesn't seem efficient or elegant. Which way would be better and less prone to errors cuz lord knows I got too many now for my liking.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

A couple of things:

1. When posting code please be sure to enclose the code in UBB code tags (see the helper button below text entry area on reply form). This will preserve the formatting of the code making it easier for people to read and help you.

2. I would not be doing database access directly in the JSP page. It is not only architecturally suspect, it makes it more difficult to diagnose Java failures. At best, be sure to model your application using a Model 2 (MVC) architecture where such pre-processing is performed in (or under the control of) a servlet controller. At the very least, factor the Java code out to a bean that you use from the JSP page.

That said...

The conventional idiom to iterate over the result set with a while loop testing the next() method. Since this method returns false when there is no next record, and empty result set will result in no iterations.


[ June 17, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
how about this



i think this way you can get the desired result.


Life called,so here I am.<br />Cheers<br />Niki.:-)
Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
Cool thanks I'll have to try that.

Its difficult here because my supervisor still doesn't want us using javabeans. Now there was a learning curve and I had to really track the lifecycles of the beans which resulted in display errors on a number of occassions (because I didnt' reload a bean with changes I sent to the database) I really saw the power and efficiency of using a bean. But my supervisor doesn't understand beans, the thought my tomcat error (socket reset by peer) was something to do with my javabean but it never made a connection to the database.

I guess I am just looking for ways to make our code more efficient, readable and closer to the standards in the industry but those aren't the goals of the business. They way bug-free code that is fast and easy to make and update. Sometimes our goals clash.

Thanks for the replies! I'll experiment after release if i can't do it beforehand.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

my supervisor still doesn't want us using javabeans


Does he make you code wearing mittens as well?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

They want bug-free code that is fast and easy to make and update.

That says enough for me.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65


"They want bug-free code that is fast and easy to make and update."

That says enough for me.


Doesn't it just?

Yes, forbidding the use of accepted standards, conventions and coding practices will certainly accomplish that.

This is about as reasonable as forbidding you from using the character "e" in your code.

I'll be quiet now.
Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
Lol, yeah I loved my beans but now the company wants to switch to C#.NET. One guy here is working with a project using the new language....should be interesting He suspects that when I leave my boss may have the project revamped to remove all beans from it. Ah well, I'll provide a report with my recommendation and showing how much easier the code is with the beans and leave the decision up to him.

Thanks for the replies!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Linda Thomas:
Lol, yeah I loved my beans but now the company wants to switch to C#.NET. One guy here is working with a project using the new language....should be interesting He suspects that when I leave my boss may have the project revamped to remove all beans from it. Ah well, I'll provide a report with my recommendation and showing how much easier the code is with the beans and leave the decision up to him.

Thanks for the replies!


RUN!
Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
Originally posted by Ben Souther:


RUN!


Lol, thankfully I graduate in December and he feels it would be better to just leave me coding JSP instead of trying to learn C#, lol. One guy here would rather we move to PHP or JSF. Thankfully I won't have to suffer through the move though it might be good experience.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Getting new technologies under your belt is never a bad thing. If you have the opportunity to use C#, php, and others, take advantage of it while you are being paid for it. Just keep in mind that Java is the bee's knees!

You also seem to have a good enough head on your shoulders to realize that your supervisor's ridiculous proscriptions are just, well, ridiculous. That skill will serve you well.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

That skill will serve you well.

Because this one won't be the last.
Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
Originally posted by Ben Souther:

Because this one won't be the last.


Ahhh, I have much to look forward to...

Thanks for the replies!
 
Don't get me started about those stupid light bulbs.
 
subject: Test result set for listbox