Search code examples
javajdbcresultset

How would I go about creating a 2D ArrayList in Java?


I am creating a basic SQL editor using java and I'm wondering how I could create a 2d array which stores each 'Persons' data in one arraylist. I have a database named contactdetails which stores each persons Contact ID, full name, email, phone number etc. and it was just to see if anyone could drop some code in the comments as to how I could go about this? Here is the current code I am using to retrieve from the database:

public void SELECTALL() {
    db.Connnect();
    Connection conn = db.getConnection();
    if (conn!=null) {
        try {
            String SQL = "SELECT * FROM (?);";
            PreparedStatement stmt = conn.prepareStatement(SQL);
            //Gathering which table the user wants to view
            Scanner s = new Scanner(System.in);
            System.out.println("Enter name of table");
            String TABLENAME = s.nextLine();
            //Inserting table name into SQL command
            stmt.setString(0, TABLENAME);
            //Executing query
            ResultSet rs = stmt.executeQuery();
            //Displating data to user
            while (rs.next()) {
                //DATA GETS STORED IN 2D ARRAY SO EACH PERSON GETS OWN PERSONAL INTEGER TO BE CALLED BY E.G soandso.get(4) 
            }
        } catch (SQLException err) {
            System.err.println(err);
        }
            
    }
}

Thanks people. any feedback would be great as well.


Solution

  • List and Person class

    I would definitely recommend to create a Person class to store all data related to one instance of a Person. This class will include the person's contact ID, full name, email, phone number, etc. Something like this will work, although you'll want to make the fields private and make getters/setters for them eventually:

    public class Person {
        public int id;
        public String name;
        public String email;
        public String phoneNumber;
    }
    

    Then, when grabbing the result, you can create a new instance of your Person class and store all the person details there. Then afterwards, you can store all the Person instances in a List<Person>, like so:

    public void SELECTALL() {
        List<Person> persons = new ArrayList<Person>();
        ...
    
        while (rs.next()) {
            Person person = new Person();
            person.id = rs.getInt("idColumn");
            person.name = rs.getString("nameColumn");
            ...
    
            persons.add(person);
        }
    }
    

    Then you can return the entire List<Person> in the end if you want. To get a specific Person instance after you filled in your List<Person>, you can use Streams to filter that list, like this:

    Person john = persons.stream()
                         .filter(person -> person.name.equals("John"))
                         .findFirst()
                         .orElse(null);
    

    This will get the first Person instance in your persons list that has their name exactly equal to "John". If no person exists with that name, it will return null instead.

    2D ArrayList

    But if you really insist on using 2D ArrayList, you can do something like this:

    public void SELECTALL() {
        List<List<String>> persons = new ArrayList<List<String>>();
        ...
    
        while (rs.next()) {
            List<String> personData = new ArrayList<String>();
            personData.add(rs.getString("idColumn"));
            personData.add(rs.getString("nameColumn"));
            ...
    
            persons.add(personData);
        }
    }
    

    HashMap

    However, if you'll be doing it this way, I recommend trying out HashMap and make the person Id the "key" to the HashMap, like this:

    public void SELECTALL() {
        Map<Integer, List<String>> persons = new HashMap<Integer, List<String>>();
        ...
    
        while (rs.next()) {
            List<String> personData = new ArrayList<String>();
            int key = rs.getInt("idColumn");
            personData.add(rs.getString("nameColumn"));
            ...
    
            persons.put(key, personData);
        }
    }
    

    Then you can get the List<String> of person data by doing this:

    List<String> person = persons.get(key);