aspose file tools*
The moose likes Servlets and the fly likes JSP what's wrong with following code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP what Watch "JSP what New topic
Author

JSP what's wrong with following code?

ph lee
Greenhorn

Joined: Jun 21, 2001
Posts: 3
my question is why code below else cause wont execute.
if u know what's going on here..please tell me..
thank u
here is code
====================
<html>
<head>
</head>
<%@ page language="java" import="java.sql.*" %>
<body>
<CENTER><H1>Search Result</H1></CENTER><BR>
<%
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(Exception e)
{
e.getMessage();
System.err.println("Unable to load driver.");
}

try
{
Connection myConn =
DriverManager.getConnection("jdbc:mysql://localhost/RDB?user=root&password=");
Statement stmt = myConn.createStatement();

String rname= request.getParameter("rname");
String state= request.getParameter("state");
String cuisine= request.getParameter("cuisine");

if (rname != null )
{
String query = "select * from restaurant where rname="+"'"+rname+"'";
ResultSet myResultSet = stmt.executeQuery(query);
if (myResultSet != null)
{
while (myResultSet.next())
{
//specify the field name
String oname = myResultSet.getString("rname");
String ostate= myResultSet.getString("state");
String ocuisine= myResultSet.getString("cuisine");
}
}
}
else ///??? why below code below else wont execute???
{ String query2 = "select * from restaurant";
ResultSet myResultSet = stmt.executeQuery(query2);
if (myResultSet != null)
{
while (myResultSet.next())
{
String oname = myResultSet.getString("rname");
String ostate= myResultSet.getString("state");
String ocuisine= myResultSet.getString("cuisine");
%>
<B><%= oname %></B>
<%=ostate %>
<%=ocuisine %><br>
<%
}
}


// Clean up ourselves

stmt.close();
myConn.close();
}
catch (SQLException E)
{
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}

%>
</body>
</html>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

I think your problem is expecting a query with no results to return "null" for a resultset. It doesn't. What you get instead is an "empty" resultset (next() returns false the very first time.). You can use the "isAfterLast()" method to replace the test for a null ReportSet.


Customer surveys are for companies who didn't pay proper attention to begin with.
Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
Hai Lee,
This is one of the problem which i also faced the matter is exactly the one which Tim Holloway explained.Not only this a java.sql.ResultSet object will NEVER CAN BE NULL(unless some serious internal server problems occured).So you are getting an Empty ResultSet object .So try this
if (myResultSet.first())
{
do
{
//specify the field name
String oname = myResultSet.getString("rname");
String ostate= myResultSet.getString("state");
String ocuisine= myResultSet.getString("cuisine");
}while (myResultSet.next()) ;
}

I just want to know is there any body call my bean's Getter and Setter methods with "Please" in front - My favorite quip from Bugzilla
hahahaforever
Greenhorn

Joined: Jul 12, 2001
Posts: 3
I think just
while (myResultSet.next())
{
//specify the field name
String oname = myResultSet.getString("rname");
String ostate= myResultSet.getString("state");
String ocuisine= myResultSet.getString("cuisine");
};
will work, no need outside if{}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP what's wrong with following code?