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]
# 1

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 > top of Java-index,Security,Event Handling...
# 2

yuk!

maybe something like:

while ( rset.next() )

{

if ( myPackage.isBig() )

{

break;

}

else

{

log.info("My package is small");

}

}

fredsanford at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 3
> 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 > top of Java-index,Security,Event Handling...
# 4

> > 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.

fredsanford at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 5

> 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

punnkdork at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 6
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 > top of Java-index,Security,Event Handling...
# 7

> 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

}

notivago at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 8

> 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 > top of Java-index,Security,Event Handling...
# 9
okay, thanks all.
punnkdork at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 10

> 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 > top of Java-index,Security,Event Handling...
# 11
what does AFAIK mean?
punnkdork at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 12
As Far As I Know
jverd at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 13
okay, I thought you were calling me names :) Thanks for the help.
punnkdork at 2007-7-7 12:51:35 > top of Java-index,Security,Event Handling...
# 14
> 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 > top of Java-index,Security,Event Handling...
# 15
I knew it - a hugely informative and helpful jerk you are. Please never stop.
punnkdorka at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 16

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.

notivagoa at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 17

> 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.

jverda at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 18

> > 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

lutha_brothera at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 19
notivago!! I was joking with jverd - sorry it wasn't more apparent. Just trying to be a funny girl, sheesh.punnk
punnkdorka at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 20

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

jschella at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 21
> notivago!! I was joking with jverd - sorry it wasn't> more apparent. Just trying to be a funny girl, sheesh.Seemed obvious to me.
jschella at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 22

Try this:

if (rs.next()) {

do {

//something

} while (rs.next());

}

else {

//something else

}

whoisfreda at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 23

> 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 :)

notivagoa at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 24
> Yuk!!what does "Yuk" mean?
UBorbaa at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 25
It means "What a horrible thing to see, it makes me feel sick just to look at.", quite a lot of meaning.
notivagoa at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 26
Or a laugh, as in: http://dictionary.reference.com/search?q=Yuk
notivagoa at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 27
"Used to express rejection or strong disgust"thanks.
UBorbaa at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 28

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

}

punnkdorka at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 29
the do - while fixed it.. as everyone probably already knows ;-)
punnkdorka at 2007-7-20 1:04:44 > top of Java-index,Security,Event Handling...
# 30

> > 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

lutha_brothera at 2007-7-20 1:04:49 > top of Java-index,Security,Event Handling...
# 31

> Try this:

> > if (rs.next()) {

>do {

> //something

>} while (rs.next());

> }

> else {

>//something else

> }

>

Yeah, I like that better that the explicit flag I suggested.

jverda at 2007-7-20 1:04:49 > top of Java-index,Security,Event Handling...
# 32

> Try this:

> > if (rs.next()) {

>do {

> //something

>} while (rs.next());

> }

> else {

>//something else

> }

>

Yeah, I like that better that the explicit flag I suggested.

jverda at 2007-7-20 1:04:49 > top of Java-index,Security,Event Handling...
# 33
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?
talon747a at 2007-7-20 1:04:49 > top of Java-index,Security,Event Handling...
# 34

> 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.

jverda at 2007-7-20 1:04:49 > top of Java-index,Security,Event Handling...