if while statement question - beginner
When I use ' if ' it only returns the first row but I need all the records - when I use ' while ' it returns all but I need the if... Is there a way to use both in first line of the code below? I am just not sure how to write this.
if(while rs.next()){// something like this?
// something
}else{
// something else
}
Thanks in advance,
punnk
[696 byte] By [
punnkdork] at [2007-9-30 22:28:20]

Why do you think you need the if?
Please explain what you're trying to do.
The most common way to use a ResultSet is simply while (rs.next()) {
// do something
}
Is it that you want to do the else if there were no rows? If so, then one way would be like this: boolean rowsFound = false;
while (rs.next()) {
rowsFound = true;
// do stuff
}
if (!rowsFound) {
// something else
}
jverd at 2007-7-7 12:51:35 >

yuk!
maybe something like:
while ( rset.next() )
{
if ( myPackage.isBig() )
{
break;
}
else
{
log.info("My package is small");
}
}
> yuk!Is that directed at the OP or at me?I'm not sure what she's trying to do, but if it's what I think it is, then your solution will not work.
jverd at 2007-7-7 12:51:35 >

> > yuk!
>
> Is that directed at the OP or at me?
>
> I'm not sure what she's trying to do, but if it's what
> I think it is, then your solution will not work.
that was for the op.
i have no idea what they are trying to do, but my code will tell you if you have a big package.
> Why do you think you need the if?
Okay sorry about that, jverd...
Basically what I am doing is building a TreeMap - If there are rows being returned for a particular key then associate them - however if a key doesnt have any rows then I set a message " No Tooling to report".
If I use the ' if ' statement it doesnt return all the rows within a key - just the first one. If I use while it does. I am just not sure how to get all that into one small statement.
if(rs.next()){
...
gaugeNum = rs_content.getString(2);
calProc = rs_content.getString(3);
...
contentList.add(gaugeNum);
contentList.add(calProc);
...
reportMap.put(subGroupName, contentList);
owner.setReportMap(reportMap);
} else {
reportMap.put(subGroupName, msg);
owner.setReportMap(reportMap);
}
I hope that makes sense. Please let me know if I need to clarify more..
Thanks,
punnk
So it's "if there are rows, process them, otherwise (no rows) take special no-rows action"? If so then see reply 1.
jverd at 2007-7-7 12:51:35 >

> Why do you think you need the if?
>
> Please explain what you're trying to do.
>
> The most common way to use a ResultSet is simply
> while (rs.next()) {
> // do something
> }
>
> Is it that you want to do the else if there
> were no rows? If so, then one way would be like this:
> boolean rowsFound = false;
> while (rs.next()) {
> rowsFound = true;
> // do stuff
> }
>
> if (!rowsFound) {
> // something else
> }
Or :
while (rs.next()) {
// do stuff
}
if ( rs.isBeforeFirst() ) {
// something for no rows
}
> I am just not sure how to get all that into
> one small statement.
You don't, AFAIK.
I think somebody once proposed a while (condition) {
body
}
else {
other
}
that would do basically this--enter the else section if and only if the condition never evaluated to true. Seems like it could be mildly handy, but not hugely so.
jverd at 2007-7-7 12:51:35 >

> Or :
> > while (rs.next()) {
> // do stuff
> }
>
> if ( rs.isBeforeFirst() ) {
> // something for no rows
> }
>
>
I thought that isBeforeFirst wasn't supported on all drivers or all ResultSets or something, though I could be wrong.
I agree though--if there's a sure-fire way to ask the RS, "Did you have any rows?" then that's preferable to explicitly managing a separate flag.
jverd at 2007-7-7 12:51:35 >

As Far As I Know
jverd at 2007-7-7 12:51:35 >

okay, I thought you were calling me names :) Thanks for the help.
> okay, I thought you were calling me names :) Thanks> for the help.Okay, you caught me. I was calling you "Ass Face And Idiot King" ;-)
jverd at 2007-7-7 12:51:36 >

I knew it - a hugely informative and helpful jerk you are. Please never stop.
Gosh!!! AFAIK is "As Far As I Know"!!!! He was kiding you!!!!
About the:
while( condition ) {
// code block1
} else {
// code block2
}
It strikes me as being idiotic!! Just because, unless you use a break statement or return, the condition will allways end up false if the code ever leave the loop, so it would have the same effect as:
while( condition ) {
// code block1
}
// code block2
Or in the case o breaking:
while( condition ) {
// code block1
if( condition2 ) {
break;
}
}
if ( condition2 ) {
// code block2
}
Yuk!!
May the code be with you.
> Gosh!!! AFAIK is "As Far As I Know"!!!! He was kiding
> you!!!!
I think the OP knows that. Calling me a "helpful jerk" suggests humor on his part.
>
> About the:
> > while( condition ) {
> // code block1
> } else {
> // code block2
> }
>
>
> It strikes me as being idiotic!! Just because, unless
> you use a break statement or return, the condition
> will allways end up false if the code ever leave the
> loop, so it would have the same effect as:
I think you misunderstand.
That construct is not legal in Java. I think it was suggested as a "wish list" item. IIRC (If I Recall Correctly, or, Idiot, Imbecile, Racist, Creep), the desired behavior would be that if the condition ever evaluated to true, then the else block would not be entered at all. If the condition was never true, and the body of the while loop was never entered, then, and only then, would the else be executed.
Thus, it would be equivalent to the legal code I posted in reply 1: boolean flag = false;
while (condition) {
flag = true;
do stuff
}
if (!flag) {
other stuff
}
It would be, as I said, moderately useful, but not a huge benefit.
> > I am just not sure how to get all that into
> > one small statement.
>
> You don't, AFAIK.
>
> I think somebody once proposed a while
> (condition) {
> body
> }
> else {
> other
> }
that would do basically this--enter the else
> section if and only if the condition never evaluated
> to true. Seems like it could be mildly handy, but not
> hugely so.
You get this in Python. And notivago, your point is dealt with (in Python :p) [url=http://www.python.org/doc/current/tut/node6.html#SECTION006400000000000000000]like this[/url]
Kind regards and blessings of the Code, :)
lutha
notivago!! I was joking with jverd - sorry it wasn't more apparent. Just trying to be a funny girl, sheesh.punnk
> > Or :
> > > > while (rs.next()) {
> > // do stuff
> > }
> >
> > if ( rs.isBeforeFirst() ) {
> > // something for no rows
> > }
> >
> >
>
> I thought that isBeforeFirst wasn't supported on all
> drivers or all ResultSets or something, though I could
> be wrong.
You are correct. Not all drivers nor all driver versions (given that that method did not exist in JDBC 1.0)
> notivago!! I was joking with jverd - sorry it wasn't> more apparent. Just trying to be a funny girl, sheesh.Seemed obvious to me.
Try this:
if (rs.next()) {
do {
//something
} while (rs.next());
}
else {
//something else
}
> notivago!! I was joking with jverd - sorry it wasn't
> more apparent. Just trying to be a funny girl, sheesh.
>
> punnk
Sorry, sometimes I loose track of when people really get offended or is just joking, there has being so much bickering around here and so much zealots whinning for good behaviour that is difficult to tell them from the people with humor.
About the while()/else construct, my mistake I really didn't got the picture on how it would work, it moves the feature from the plain idiotic to the bizzarre-wish-it-never-get-into-the-language list.
May the Code be with you.
P.S.: Lutha, tanks you showed me something that should be obvious to me, "Code" must be written starting with uppercase :)
> Yuk!!what does "Yuk" mean?
It means "What a horrible thing to see, it makes me feel sick just to look at.", quite a lot of meaning.
Or a laugh, as in: http://dictionary.reference.com/search?q=Yuk
"Used to express rejection or strong disgust"thanks.
> Sorry, sometimes I loose track of when people really
> get offended or is just joking, there has being so
> much bickering around here and so much zealots
> whinning for good behaviour that is difficult to tell
> them from the people with humor.
No worries here, notivago..!!
I dont think I explained my situation very yesterday...
This is what I have working so far - Its pulling back the information for each group name and adding the " No Tooling to report " message when there isn't any information to show BUT because I have two 'next' statements on the while its starting on the second row.
I need to step back one. I am off to figure this out but I thought I would show what I meant anyways..
this.conn = SybaseDAO.createConnection();
state_content = conn.prepareCall("{call mt." + buildSubGroup + "('"+ grabAreas[i] + "')}");
rs_content = state_content.executeQuery();
if(rs_content.next()){
while(rs_content.next()){
gaugeNum = rs_content.getString(2);
calProc = rs_content.getString(3);
sn = rs_content.getString(4);
contentList.add(gaugeNum);
contentList.add(calProc);
contentList.add(sn);
reportMap.put(subGroupName, contentList);
owner.setReportMap(reportMap);
}
} else {
reportMap.put(subGroupName, msg);
owner.setReportMap(reportMap);
}
the do - while fixed it.. as everyone probably already knows ;-)
> > notivago!! I was joking with jverd - sorry it
> wasn't
> > more apparent. Just trying to be a funny girl,
> sheesh.
> >
> > punnk
>
> Sorry, sometimes I loose track of when people really
> y get offended or is just joking, there has being so
> much bickering around here and so much zealots
> whinning for good behaviour that is difficult to tell
> them from the people with humor.
>
> [...]
>
> May the Code be with you.
> P.S.: Lutha, tanks you showed me something that
> t should be obvious to me, "Code" must be written
> starting with uppercase :)
Well, of course! ;)
My pleasure!
cheers,
lutha
> Try this:
> > if (rs.next()) {
>do {
> //something
>} while (rs.next());
> }
> else {
>//something else
> }
>
Yeah, I like that better that the explicit flag I suggested.
> Try this:
> > if (rs.next()) {
>do {
> //something
>} while (rs.next());
> }
> else {
>//something else
> }
>
Yeah, I like that better that the explicit flag I suggested.
i've always felt the while() { } was nearly the same as do { } while()whats the specific nuance that differentiates themor is it onlywhile() always executes anddo {} while() only excuites if the condition is true?
> i've always felt the while() { } was nearly the same
> as do { } while()
>
> whats the specific nuance that differentiates them
> or is it only
> while() always executes and
> do {} while() only excuites if the condition is true
> ?
You've got it backwards.
The body of a while loop might execute zero times. The body of a do/while always executes at least once. This is because while is test,execute but do/while is execute/test.