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.");
              
          
            
        }

Recommended Answers

All 5 Replies

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

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

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");

thanks for help..Will have a go at this.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.