Query a database and generate an ArrayList object

Hi I have a problem I am having trouble solving here it is:

Write the code for the getPeople () method so that it constructs and returns an
ArrayList of Person type objects retrieved from a database.

An object of type java.sql.Connection has been initialized.
The SQL database contains a people table with 3 columns: id (autoincrementing integer)
, first_name (varchar), last_name (varchar).

Using the Connection object, retrieve all the records from the table, and for each
record use the corresponding fields to create an object of type Person.Add each
Person type object thus created in your ArrayList.
Return the resulting list.

Here is the code that must be completed:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql. *;
import java.util.ArrayList;
import java.util. *;

public class ExerciseImpl {

    public void runExercise (String [] argv) throws Exception {
    }

    public ArrayList <Person> getPeople () throws Exception {
        Connection conn = this.getCandidateConnection ();
        / * ---------- DO NOT CHANGE THE CODE ABOVE THIS LINE, IT WILL BE RESET ON RUN ---------- * /

        / **** Enter your code here **** /

        / * ---------- DO NOT CHANGE THE CODE BELOW THIS LINE, IT WILL BE RESET WHEN RUNNING ---------- * /
    }

   
}

class Person {
    public int id;
    public String firstName;
    public String lastName;

    Person (int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

Here is the code I came up with:

public class ExerciseImpl {

    public void runExercise (String [] argv) throws Exception {
    }

    public ArrayList <Person> getPeople () throws Exception {
        Connection conn = this.getCandidateConnection ();
        / * ---------- DO NOT CHANGE THE CODE ABOVE THIS LINE, IT WILL BE RESET ON RUN ---------- * /

        / **** Enter your code here **** /

        / * ---------- DO NOT CHANGE THE CODE BELOW THIS LINE, IT WILL BE RESET WHEN RUNNING ---------- * /
    }

    private Connection getCandidateConnection () {
    }
}

class Person {
    public int id;
    public String firstName;
    public String lastName;

    Person (int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

Honestly, I couldn’t complete much (I just created the getCandidateConnection () method without a body)
because I don’t know how
complete it and also for the rest, I do not know how to query a database in a java program.
Basically, I have a hard time with this problem.

Can you help me please?

(… Honestly, if this stuff was covered when I was going thru CS stuff, my job would have been way cooler, and so much less boring… Edit: In COLLEGE, it would’ve been cooler, and nice that they would’ve showed stuff like this… )

The proper imports are needed for P1 and P2 classes ( import java.sql.*;
/*if more specific searchable imports, check method documentation on ORACLE for Java-based SQL queries, and affiliated reading of radixes from files/ foreign objects in the absolute/relative file pathname… etc. */ ) .
If potential errors are thrown, then use try-catch statements to allow for internal error handling in the driver… Otherwise, it’s all right there in the literature; in black and white.

I did the following code:

public ArrayList <Person> getPeople () throws Exception {

         ResultSet resultSet = null;
        try (Connection conn = this.getCandidateConnection ();
             Statement statement = conn.createStatement ();) {
            String selectSql = "SELECT id, first_name, last_name from people";
            resultSet = statement.executeQuery (selectSql);

     } catch (SQLException e) {
            e.printStackTrace ();
        }

         }

But I am asked to retrieve all the records from the table people and for each record
to use the corresponding fields to create an object of type Person.
And also to add these type objects
Person thus created to my ArrayList (Indeed, I must build and return an ArrayList of type Person
extracts from the database).

How should I proceed? I thought about using a Map object to retrieve the fields from the database.
Is this a good idea, please?

Reading the docs is critical here…

Did you get to the part about the while(resultSet.has()) loop structure?

Another example URL with Java Queries interacting w/ SQL…

i. Java Example of SQL dB File Reading: https://alvinalexander.com/java/edu/pj/jdbc/jdbc0003/

ii. Simple Java SQL Command interactions (The 4 Primary SQL Commands):

Do you know how to close the rS FileStream once parsing to or collecting data from the SQL dB is over?

Is the pathName of your SQL query structure accessed by the P1/P2/Driver Objects ?

Do you know how to Parse your Person_k Objects using for loops, while loops, or forEach loops? Given the above example (ii), do you know how to Perform an Insert using the VALUES command when queries from Person_k attempt to access it therein, where the statement object passes the command to parse the queried dB for all residual values of ArrayList<Person_k> that iterates over all such object values of type?

Do your files, when debugged (For Linux-esque GUI’s/ operators, the commands like in PUTTY are as follows:

javac -theFileNameHere.java […compiler crap if you fracked up…] \n java theFileNameHere) … [code output here in compiler] ; for GUI’s like Eclipse, it auto runs the state, so long as files have been parsed for error, and can run),

yield any critical data about how the looping structure fails as the rS object is parsing (Object) Person_k (k = [1,n] for dB Name Here, or something), if at all?