0

I am using JDBC to run a SQL query in Java. I want to take the result of the query and store it in an arraylist so that I can display the data in a graph of some sort. I'm getting the same line printing out the same number of times as columnCount. Here is my code.

ArrayList <String[]> result = new ArrayList<String[]>();
int columnCount = rset.getMetaData().getColumnCount();
        if(rset!=null)
        {
            while(rset.next())
            {
                found=true;

                String[] row = new String[columnCount];
                for (int i = 0; i < columnCount; i++) {
                    row[i] = rset.getString(i + 1);                    
                    row[i] = rset.getString("Date") + " "
                            + rset.getString("Hour");
                    System.out.println(row[i]);
                }
                result.add(row); 
        }
2
  • it seems your code needs debug! Commented Jul 29, 2016 at 16:32
  • Why the String[]? Wouldn't it be better with a class? class Row { private String value; private Date date; /* getters and setters */}. Why also getting date and hour for each column? Commented Jul 29, 2016 at 16:37

1 Answer 1

1

Your second row[i] rewrites the value of the column. Just remove it and you'll see your records:

...
for (int i = 0; i < columnCount; i++) {
    row[i] = rset.getString(i + 1);                    
}
System.out.println(Arrays.toString(row));
result.add(row);
...
Sign up to request clarification or add additional context in comments.

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.