Link to home
Start Free TrialLog in
Avatar of kurisuchine22
kurisuchine22

asked on

placing values in an array

i have an array ar[] and i want to store values in it from the databse,how will i do it?
this is just a part of the code:
 
String[] arr = null;
i nt i = 0 ;
while(rs2.next()){
String duid = rs2.getString("userid");
String ddn = rs2.getString("dn");
String dtn = rs2.getString("tn");                        
arr[i] = dtn;
i++;
}statement2.close();connection2.close();%>

i want to put the values dtn in my array arr[] and pass it to a bean.
hop u can help me on this.thanks.
Avatar of jimmack
jimmack

The problem is that you don't know how big the ResultSet is until you've iterated through it.

It is easier to store the data in an ArrayList so that it can "grow" as you add new elements, then convert that to an array later (or better yet, just store the ArrayList in your bean).

ArrayList arr = new ArrayList();

while...
.
.
String dtn = rs2.getString("tn");
arr.add(dtn);

You don't need the "i" counter then either.
ASKER CERTIFIED SOLUTION
Avatar of JNic
JNic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks nic
I'm not happy about this.  JNic simply copied my answer with a different container.

I'll give it 24 hours before I contact CS to resolve this.
Sorry jimmack, I did not mean to steal your points! :-(
I just thought a Vector would be more suitable in a threaded environment since it is synchronized, and to make it simple, I used your example again, since tizay obviously is not yet a wizard in java.

Would it satisfy you, if I posted 20 points for you, or how would you like it?

Regards,

Nic
Hi JNic.

If you don't mind, that would be good :-)

My comment was really for kurisuchine22.  I know you can't split 20 points, but the fact that their comment showed no recognition for my contribution really annoyed me.

Thanks JNic ;-)

(BTW: I'd use add() instead of addElement().  addElement() is not deprecated (yet), but if you use add(), you can switch between other containers - like ArrayList - without changing the methods ;-))
Ooooooooops - SORRY TIZAY!
I meant of course kurisuchine22 (blush)

Thanks for you comment updon addElement jimmack. I'll post some points for you ! :-)
how come you guys are fighting over my question?i dont want to cause arguments in this page.hope you understand.i'm just asking a question.:-)