1

I am getting 3 columns for each row from mysql table by using ResultSet. I am not sure about the number of rows I will get by using query. To store those rows I am using ArrayList as given code below:

while (rs.next()) 
{
    String[] row = new String[numcols];
    for (int i = 0; i < numcols; i++) 
    {
        row[i] = rs.getString(i + 1);                                  
    }  
    rowsList.add(row);                     
}
rs.close();

When I debugged the code I found columns values are present in the ArrayList. Now I am unable to access the columns values because rowsList.get(index) will return only value at specific index but I have further 3 more values at that index how to access those values.

3
  • please follow my answer. Good luck using list and class rather than using Array in such a complex way Commented Sep 2, 2014 at 5:17
  • try-- String colValue=rowsList.get(rowIndex)[columnIndex]; Commented Sep 2, 2014 at 5:19
  • why don't you use a dto instead get the rows then add the column values to the dto object add the object to the list Commented Sep 2, 2014 at 5:21

3 Answers 3

3

List.get() in your case will return a String array: String[]. You can access the elements of an array by using the [] index operators:

String[] row = rowList.get(0);
for (int i = 0; i < row.length; i++)
    System.out.println(row[i]);

// Or you can use the enhanced for to loop through the array:
for (String s : row)
    System.out.println(s);

If you want to process all rows:

for (String[] row : rowList) {
    // Process row
    for (String s : row) {
        // Do something with the string
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I would like to suggest you to learn about List.

And the second would be to create a class that holds everything that comes from your query.

And finally save each object of the class to the array.

Example

Suppose you get id, name and address columns from your query

Create a class

public class YourClass{
int id;
String name, address;
//create getters and setters or use a constructor
//example of setter to set field id
public void setId(int id){
this.id = id;
     }
}

And then while retrieving the records from query, create an object of your class and set the columns as field of your class as follow:

YourClass anObject = new YourClass();
anObject.setId(id);// get similar columns from query
anObject.setName(name);

And finally add the object to the ArrayList as below:

yourArrayList.add(anObject);

To take care of multiple number of records you need to keep these code inside the while loop And define your List before while loop as follow:

List<YourClass> yourArrayList = new ArrayList<YourClass>();

And I think this is the best approach as it uses OOP that you should begin using instead of using bare array.

Comments

1

you would want to specify what object is in the list. to achieve this make your List to List<String[]> rowsList = new ArrayList<String[]>();

then use your while loop

while (rs.next()) 
{
    String[] row = new String[numcols];
    for (int i = 0; i < numcols; i++) 
    {
        row[i] = rs.getString(i + 1);                                  
    }  
    rowsList.add(row);                     
}
rs.close();

when you access each item in the list it will always return a String[] which you can iterate to get the values.

for(int i = 0 ; i< rowsList.size() ; i++){
   rowsList.get(i);//will return a String[]
}

OR

for(String[] rows : rowsList){
   //iterate the rows
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.