4 Replies - 9613 Views - Last Post: 16 February 2011 - 05:42 AM Rate Topic: -----

#1 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

ResultSet to Array

Posted 16 February 2011 - 12:55 AM

Hello!

Just want to find out what would be the best way of converting your ResultSet to an Array? For example using a snipet of code:

Statement stmt = con.createStatement();
     ResultSet rs = stmt.executeQuery("SELECT Name,Surname FROM Contacts");



How would you now take ResultSet "rs" and convert it to an Array? Or even ArrayList, since you can then just convert the ArrayList to an Array...

Any help would greatly be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: ResultSet to Array

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: ResultSet to Array

Posted 16 February 2011 - 01:25 AM

I don't think that there IS a convenient way to do this. However, there are hundreds of methods in ResultSet that allow you to navigate through and get all the information you need. By navigating through it, I suppose you could make an ArrayList or a HashMap or something along those lines as you go.

Read up on ResultSet:
http://download.orac.../ResultSet.html
Was This Post Helpful? 1
  • +
  • -

#3 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: ResultSet to Array

Posted 16 February 2011 - 01:38 AM

Okay so I tried working something out:

Since I'm at a PC now that doesnt have the JDK installed I am unable to see if this code will work. But it would be appreciated if someone can maybe have a look at this and just see if it might work, syntax looks correct and all that.

Statement stmt = con.createStatement();
	ResultSet rs = stmt.executeQuery("SELECT Name, Surname FROM Contanct");
	
String myArray = new ArrayList();

while(rs.next()){
        myArray.add(rs.getString("Name") + " " + rs.getString("Surname"));
}
	
con.close();



Oh and it might be worth mentioning I'm using JDK 1.4.2

This post has been edited by Dean_Grobler: 16 February 2011 - 01:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: ResultSet to Array

Posted 16 February 2011 - 02:29 AM

1) I can't compile it right now, but this is not valid:
String myArray = new ArrayList();



2) Use JDK 1.5+ or you cannot use generics, which are integral to modern Java:
ArrayList<String> myArray = new ArrayList<String>();


Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2649
  • View blog
  • Posts: 11,172
  • Joined: 20-September 08

Re: ResultSet to Array

Posted 16 February 2011 - 05:42 AM

See the following:

http://technojeeves....-to-nested-list
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1