Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

return string array  RSS feed

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dale con
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dale con
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think something like this should do the trick.

 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course I mean :

 
dale con
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your help
 
I love a good mentalist. And so does this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!