0

Hi people,

I want to have an if statement in my servlet that if no results are found from database i print out an error to a html page. Im not to sure how to do this though..See my attempt in code below, can someone tell me what im doing wrong..I know its prob a stupid error but im prety new to java so please excuse

try
              { 
                  	out.println("JOBID --- SECTOR --- LOCATION --- SALARY" +
                  			" --- EMAIL --- DESCRIPTION --- COMPANY <br>");  
            		stmt = con.createStatement();
            		rs = stmt.executeQuery("SELECT * FROM FMC_JOBS WHERE SECTOR ='"+thisSector+ "'" +
            				" AND LOCATION =  '"+Location+"'" );
            		while (rs.next())
                   out.println( rs.getString("JOBID") + "---" + rs.getString("SECTOR") + "--- " +
                      rs.getString("LOCATION") + "--- " + rs.getString("SALARY") + "--- " + rs.getString("EMAIL")
                      + "--- "+rs.getString("DESCRIPTION") + "---" + rs.getString("COMPANY") +"<br><br>"); 
            		
            		   }
                    catch (Exception e) {
            	     		out.println("<BR>An error has occurred during the Statement/ResultSet phase.");
            	      }
                    
                    if (rs.equals("")){
                    	out.println("Sorry, No results returned.");
              
          
            
        }
2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by javaAddict
0

The rs is of type ResultSet, the "" is type String. Why do you think that this : rs.equals("") will work, since you are comparing a ResultSet object with a String object. If the query returns no data, then the first call to the rs.next() will return false.

Also it is better to read the database, put the results into a collection (Vector) of objects, and then send that object to the jsp to display it. Never use a servlet for displaying.
Have an object with attributes: JOBID, SECTOR, LOCATION, SALARY, EMAIL, DESCRIPTION, COMPANY

0

thanks for reply javaAddict. How do i go about creating the vector of objects? i have never used vectors before..

0

Look at the API of the java.util.Vector class.

In a separate class have a method that roughly does this:

{
Vector<YourObject> v = new Vector<YourObject>();

declare the Statements, ResultSet, .....
execute the query;

while (rs.next()) {
   String jobId = rs.getString("JOBID");
   String sector = rs.getString("SECTOR");
   ....
   create the object
   YourObject yo = new YourObject(jobId, sector, ....);
   v.add(yo);
}

close everything
return v;

}

Call the above method in the servlet, put it into the request and redirect to the page you want it displayed.

Vector<YourObject> v = call of the method
request.setAttribute("somename", v);
request.forward; // search for the syntax of that method on the net

To get the Vector at the jsp:

Vector<YourObject> v = (Vector<YourObject>)request.getAttribute("somename");
0

For a complete tutorial, look at the Read Me post at the top of the JSP forum that talks about the JSP Database Connectivity

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.