title: How to get properties of an object stored in ArrayList using Java
body: <p>I want to print all student objects' properties(name, subject, registrationNo) stored in a ArrayList object.</p>
  
  <p>student details are getting from a database and insert them into student objects.
  Then these student objects are insert into the ArrayList.</p>
  
  <p>Finally I want to print these student object properties one by one as follows.
  <a href="https://i.stack.imgur.com/MdwGw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MdwGw.jpg" alt="Question Summery"></a>
  Followings are my codes.</p>
  
  <p>DBconn.java</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  public class DBconn {
  
      static Connection conn;
  
      public static Connection getConnection() {
          try {
              Class.forName("com.mysql.jdbc.Driver");
              try {
                  conn = DriverManager.getConnection("jdbc:mysql://localhost/student_database", "root", "");
              } catch (SQLException ex) {
  
              }
          } catch (ClassNotFoundException ex) {
  
          }
          return conn;
      }
  
  }
  </code></pre>
  
  <p>Student.java</p>
  
  <pre><code>public class Student {
  
      private String name;
      private String RegistrationNo;
      private String course;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getRegistrationNo() {
          return RegistrationNo;
      }
  
      public void setRegistrationNo(String RegistrationNo) {
          this.RegistrationNo = RegistrationNo;
      }
  
      public String getCourse() {
          return course;
      }
  
      public void setCourse(String course) {
          this.course = course;
      }
  
  }
  </code></pre>
  
  <p>StudentList.java</p>
  
  <pre><code>import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.ArrayList;
  
  public class StudentList {
  
      public static ArrayList getStudentList() {
          ArrayList list = new ArrayList();
  
          String sql = "SELECT * FROM student";
  
          try {
              Statement stm = DBconn.getConnection().createStatement();
              ResultSet rs = stm.executeQuery(sql);
  
              while (rs.next()) {
                  Student student = new Student();
  
                  student.setName(rs.getString(1));
                  student.setCourse(rs.getString(2));
                  student.setRegistrationNo(rs.getString(3));
  
                  list.add(student);
              }
  
          } catch (SQLException ex) {
  
          }
          return list;
      }
  
  }
  </code></pre>
  
  <p>ViewStudent.java</p>
  
  <pre><code>public class ViewStudent {
  
      public static void main(String[] args) {
  
          int size = StudentList.getStudentList().size();
          for (int i = 0; i &lt; size; i++) {
              System.out.println(StudentList.getStudentList().get(i));
        //I want to get all student's name,subject and registrationNo 
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Thanks.</p>