title: How to get all users from MySql database and put into arraylist
body: <p>I am creating a java application which will allow users to register, login and update their details. The data is stored in a MySQL database which is already successfully connected to my application.</p>
  
  <p>I would like to be able to get all data of a user, which will be used to create a user object and make a array list of all users, which will be type "user".</p>
  
  <p>What I would like to know is if ID be a good property to have for a "user" object as I have their names and usernames(which is unique to the users).</p>
  
  <p>When the user log in to their account the user list should have all users in the arraylist and be positioned to the logged in user. Which I do not know how to do.</p>
  
  <p>I have uploaded my current code so far:</p>
  
  <pre><code>public class UserAccountList {
  
  private static UserAccountList instance;
  private ArrayList&lt;user&gt; userList;
  user newUser = null;
  int currentUser;
  
  protected UserAccountList() throws IOException {
      this.userList = new ArrayList&lt;&gt;();
      readDatabase();   
  }
  
  public static void main(String[] args) throws IOException{
      ArrayList&lt;user&gt; userList1 = new UserAccountList().getUserList();
      for(int i=0;i&lt;userList1.size();i++){
          System.out.println(userList1.get(i).getUsername());
      }
  }
  public ArrayList getUserList() throws IOException {
      return this.userList;
  
  }
  
  public static UserAccountList getInstance() throws IOException {
      if (instance == null) {
          instance = new UserAccountList();
      }
      return instance;
  }
  
  
  
  private void readDatabase() throws IOException {
       PreparedStatement ps;
       ResultSet rs;
  
       String query = "SELECT * FROM `user`" ;
  
       try{
      ps = MyConnection.getConnection().prepareStatement(query);
  
  
      rs = ps.executeQuery();
  
      while(rs.next() != false){
  
          String uname = rs.getString("username");
          String pass = rs.getString("password");
          String fname = rs.getString("firstname");
          String lname = rs.getString("lastname");
          String email = rs.getString("email");
          String uni = rs.getString("university");
          String course = rs.getString("course");
  
          user newUser = new user(fname, lname, uname, pass, email, uni, course);
          addUser(newUser);
      }
  
     }
  
      catch (SQLException ex) {
          Logger.getLogger(FXMLRegistrationController.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  
  
  public void addUser(user newUser){
      userList.add(newUser);
      return;
  }
  }
  </code></pre>