• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP what's wrong with following code?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()) ;
}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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{}
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic