PDA

View Full Version : Dynamic Array From mysql statement


smoke10010
07-01-2009, 02:37 AM
Hello,

I'm trying to fill an array from a mysql table. I have the SELECT working an it's pulling the data properly as well as assigning it to the array.

One thing I'm stuck on is creating the array so it's dynamic so it can allow for 10, 20, 100, or 1000 string. Here's an example of what I mean:


String strCoupons[] = new String[5];

stmt=con.createStatement();
rst=stmt.executeQuery("SELECT * FROM coupons ");
while(rst.next()){

strCoupons[i] = rst.getString(2);

i++;

}
rst.close();


Right now the problem with that is I can only load 5 strings into the array, how do I set it up so that I can load as many strings as I have stored in the database?

I guess the next question is.. what if I have 10,000,000 strings in the database, still a good idea to load the whole thing into the array?

Thanks.

Fou-Lu
07-01-2009, 03:51 AM
Use a generic collection, such as a linked list, vector, ArrayList etc. Any Collection object has a potential for an add method (although you'd need to check the api for you're specified concrete class to ensure that it has encorperated the add method) - these three do. Check out this link for more information about collections: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html

Otherwise, you'll need to write some new methods that will take an array, create a newly resized one, copy the old into the new, and destroy the old.

I would not recommend querying for 10,000,000 records and dumping the results into a single array. If each string where a single char, you're looking at about 9MB for that alone. Increase it to 100 chars each, and that will result in almost a GB of data. Doubt that the app will be very happy with that.

smoke10010
07-01-2009, 05:05 AM
wow lots of stuff here. Thanks a lot for the info.. that really helps. I actually come from a php background so all of this is fairly new. The 10,000 records thing is just more of a check. I'm building a coupon app and I'm pretty sure I'd go out of business if we had 10,000 coupons hehe.

ckeyrouz
07-02-2009, 06:23 PM
Try inserting a paginator somewhere and make it that you have in your page 50 coupons for example or even a 100.
And on each page change you go and retrieve the next 50 or 100.

never load all your data on the web.
Even if your internet connection might help you retrieve all of it, beware that your browser might crash (memory overload).