Solved

placing values in an array

Posted on 2003-11-05
8
234 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:kurisuchine22
  • 3
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9692927
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.
0
 
LVL 1

Accepted Solution

by:
JNic earned 20 total points
ID: 9697178
I dont know the details of your application, but if it is a multi-thread-environment, I would use Vector instead of ArrayList, since it is synchronized. This is the main difference between the two classes.

Example with Vector:

import java.util.Vector;

Vector arr = new Vector();

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

Regards,

Nic
0
 

Expert Comment

by:tizay
ID: 9698527
thanks nic
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9699829
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:JNic
ID: 9700177
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
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9700237
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 ;-))
0
 
LVL 1

Expert Comment

by:JNic
ID: 9700290
Ooooooooops - SORRY TIZAY!
I meant of course kurisuchine22 (blush)

Thanks for you comment updon addElement jimmack. I'll post some points for you ! :-)
0
 

Expert Comment

by:tizay
ID: 9712311
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.:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apache server configuration 7 80
some methods private some are not 8 196
I am having a difficulty creating a responsive Page for my website. 10 83
reflection example 3 92
Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now