Forums Register Login

return string array

+Pie Number of slices to send: Send
Hi all

I'm trying to return a string array from a db table.

Can anyone help me out

I can return an array list, but i want the results as a string array

Here's my code for array list, but having difficulty retruning a string array

while (rs.next())
{
rsList = new ArrayList();

for (int counter = 1; counter <= rsmd.getColumnCount(); counter++)
{
rsList.add(rs.getString("fieldname"));
}

returnRs.add(rsList);
}

Thanks for your help
+Pie Number of slices to send: Send
You can convert an ArrayList into an array like this:

ArrayList list = ...
String[] array = (String[]) list.toArray(new String[list.size()]);

You can only use String[] here if all the items in the ArrayList are Strings, of course.
+Pie Number of slices to send: Send
Hi

When i do this

String[] array = (String[]) list.toArray(new String[list.size()]);

List returnRs = new ArrayList();
List rsList = new ArrayList();
String [] a;

try
{
.....
while (rs.next())
{
rsList = new ArrayList();

for (int counter = 1; counter <= rsmd.getColumnCount(); counter++)
{
rsList.add(rs.getString("fieldname"));
}

returnRs.add(rsList);
}

a = (String[]) returnRs.toArray(new String[returnRs.size()]);

....

I get the error java.lang.ArrayStoreException

Thanks for your help
+Pie Number of slices to send: Send
I don't know why you've done this, but returnRs is an ArrayList with one item in it: the ArrayList rsList. Change the code to simply eliminate returnRs everywhere; its unnecessary. Convert rsList to a String array and return that.
+Pie Number of slices to send: Send
You are also walking through all your columns and adding the value for one column ("fieldname") into an ArrayList columncount times.

I don't really know what you are trying to do, but the loops do not make any sense at the moment.
+Pie Number of slices to send: Send
My sql is like this

select fieldname from mytable WHERE .....'

So i'm just returning one column

Is there a better way of writing this?

Thanks for your help
+Pie Number of slices to send: Send
I think something like this should do the trick.

+Pie Number of slices to send: Send
Of course I mean :

+Pie Number of slices to send: Send
Thank you both for your help
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 9010 times.
Similar Threads
creating object out of class of same name
passing multiple values from one page to another
array of objects confusion
a thread code
iterate an object in struts-2
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:24:51.