Reading all rows from database and putting them into ArrayList - Java

I've the below code,

ArrayList<ArrayList<String>> outer = new ArrayList<ArrayList<String>>();
ArrayList<String> inner = new ArrayList<String>(); 
ResultSet rs = statement.executeQuery(sqlQuery);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
    for(int i=1; i<=columnsNumber; i++){
        inner.add(rs.getString(i));
    }                   
}
outer.add(inner);
return outer;

What I get for outer is something like this

[[100, 200, 300, 100, 200, 300]]

What I'm trying to get is

[[100, 200, 300], [100, 200, 300]]

I want each row to be an unique item in the list, but what I get is a list of all rows in one item in the list.

How do I do it?

Can someone help me out?

Create a new inner for each record and add it to outer.

ArrayList<String> inner; 
ResultSet rs = statement.executeQuery(sqlQuery);
ResultSetMetaData rsmd = rs.getMetaData();

while (rs.next()) {
    inner = new ArrayList<String>(); 
    for(int i=1; i<=columnsNumber; i++){
       inner.add(rs.getString(i));
    }    
    outer.add(inner);               
}

Reading Data From Database and storing in Array List object, private void reloadButtonActionPerformed(java.awt.event. You have to create a new customer object in every iteration and then add that newly created What you are doing is adding to the list the same customer and then modifying it. database first and store in ArrayList but when i return array list on html page it display� There are several different ways in which you can read data from a file into an ArrayList. When you only need to read the lines as elements use Files.readAllLines ; when you have data that can be easily parsed use Scanner ; when working with large files use FileReader wrapped with BufferedReader ; when dealing with an array of objects use ObjectInputStream (but make sure the data was written using ObjectOutputStream ).

On each step of the while loop, you have to re-initialize the inner list with an empty one and and add it to the outer list before the step has finished:

while (rs.next()) {
    inner = new ArrayList<String>();
    for(int i=1; i<=columnsNumber; i++){
        inner.add(rs.getString(i));
    }                   
    outer.add(inner);
}

Fetch data from database and put into arrayList, I need to store all the data and put into arraylist.. java.sql.Statement stm1 = con. I want to take all values and keep it in single array NamePosition("Corsley Edwards", "G")); // and then be able to access the name So you just need to amend my code to fill up the list from your database response instead� What do I do to put this resultset into an array and loope through it? Any hints or suggestion is greatly appreciated. I think the best way is to wrap the String values in one object for each row, and then add the objects to the ArrayList.

Since outer is a list of lists, in each iteration of the while loop you will need to:

  1. Reinitialize the inner list
  2. Fill the inner list
  3. Store this version of the inner list in the outer list.

I would also keep the inner declaration inside the while loop to limit its scope, like this:

List<List<String>> outer = new ArrayList<>();
ResultSet rs = statement.executeQuery(sqlQuery);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
    List<String> inner = new ArrayList<>(); 
    for(int i = 1; i <= columnsNumber; i++) {
       inner.add(rs.getString(i));
    }    
    outer.add(inner);               
}
return outer;

Java: Read a File into an ArrayList, There are many ways to go about Reading and Writing Files in Java. we need to put the contents of the file back into memory and perform operations. Since Java 7, it's possible to load all lines of a file into an ArrayList in a very simple way: The second flavor is for when you don’t know how many lines you want to read, you want to read all lines, want to read lines until a condition is true, or want something that can grow and shrink over time. These examples involve using a more flexible object oriented approach like a vector (C++) or an arraylist (Java).

How to retrieve all rows from MySQL table using JDBC – iByteCode , This JDBC example explains how to retrieve all rows from a MySQL database table. Open Eclipse IDE and create a new Java project and name it as “ SelectQuery“. FROM employee"; List<Employee> list = new ArrayList< Employee>(); This method reads data from database ResultSet, stores it in� We assign the value that we reed form the file “peopledata.ser” and make sure we cast the value into an ArrayList of Person. readStream.close(): close the reading stream. System.out.println(people2.toString()): print the new ArrayList to the console to see if all the data is loaded correctly.

How to read a text file into ArrayList in Java, Prior to Java 7, reading a text file into an ArrayList involves lot of boiler plate coding, as you This method return a List of String which contains all lines of files. Since I have given the relative path here, make sure you put that file inside Eclipse how to read names and integers from file and put it in different arrays in java. In the following code, we'll demonstrate how to read lines from a file into an ArrayList, using FileReader: ArrayList<String> result = new ArrayList<> (); try (FileReader f = new FileReader (filename)) {. StringBuffer sb = new StringBuffer (); while (f.ready ()) {. char c = ( char) f.read (); if (c == ' ') {.

One common method of database access in Java is the use of Java database connectivity (JDBC, see Lesson: JDBC Basics) using database access object (DAO) classes (see Java Practices -&gt; Data access objects).

Comments
  • You are a genious. It helped me buddy