File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP what's wrong with following code?

 
ph lee
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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>
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17646
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • 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()) ;
}
 
hahahaforever
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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{}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic