?
Solved

java - while loop troubles

Posted on 2005-04-04
3
Medium Priority
?
338 Views
Last Modified: 2010-03-31
i was hoping for a hint setting up my while loop. i have this code that displays to a user an item they searched for
and i am able to master this :) however, if they enter an item that does not exist it still shows somethings..like the column names..i just want it to display a message "No record" of sometype...

im just having issues with my while loop.. i know i want to check to see if records are found but i am having trouble with the logic. any suggestions???? thanks in advance annie :)

try
{
//convert user data to number as per the DB
int id = Integer.parseInt(request.getParameter("form_PROGRAMID"));

//connect to the DB
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String dbURL = "jdbc:oracle:thin:@172.20.138.6:1521:ORA1";
Connection conn = DriverManager.getConnection(dbURL ,"myDB_test", "password");
Statement stmt = conn.createStatement();

String searchSQL = "SELECT * FROM SHOWS WHERE ProgramID = '"+ id +"' ";
int sqlResult = id;

ResultSet rset = stmt.executeQuery(searchSQL);
ResultSetMetaData rsetMetaData = rset.getMetaData();
int columnCount = rsetMetaData.getColumnCount();

//display results to user
out.println("<P>Here are the results. </P>");    
out.println("<TABLE BORDER= '0' CELLPADDING= '3'>");
out.println("<TR>");

//loop through records to search
for(int i = 1; i <= columnCount; i++)
{
//display column names
String s = rsetMetaData.getColumnName(i);
out.println("<TD>" +s+ "</TD>\n");
}//end for

int count = 0;
while(rset.next())
{
//increment count here for each record that is found...
//count++;
/*after loop check records found
if(no records found)
{
out.println("Unable to find match for search criteria!");
}
if(records found)
{
//display record found (which works now)
}*/

out.println("<TR>");
//display contents of table to user
for(int i = 1; i <= columnCount; i++)
{
out.print("<TD>" + rset.getString(i));
}//end for
out.println();
}//end while
//close connections
.
.
}//end try
0
Comment
Question by:annie613
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
aozarov earned 600 total points
ID: 13700415
change:
while(rset.next())
{
...

}

with:
if (!set.next())
{
// print out no records found
}
else
{
     do
     {
        // put here the logic you had in your while loop
    }
    while (rs.next());
}
0
 

Author Comment

by:annie613
ID: 13700454
perfect. i see i was trying to put the if else inside the while loop and messed up the logic. thanks for your suggestions! cheers!!!!!!!! :)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13700481
Right, once you entered the loop is a clear indication that you did get a result.
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question