?
Solved

java - while loop troubles

Posted on 2005-04-04
3
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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