2

I want to print all student objects' properties(name, subject, registrationNo) stored in a ArrayList object.

student details are getting from a database and insert them into student objects. Then these student objects are insert into the ArrayList.

Finally I want to print these student object properties one by one as follows. Question Summery Followings are my codes.

DBconn.java

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;
    }

}

Student.java

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;
    }

}

StudentList.java

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;
    }

}

ViewStudent.java

public class ViewStudent {

    public static void main(String[] args) {

        int size = StudentList.getStudentList().size();
        for (int i = 0; i < size; i++) {
            System.out.println(StudentList.getStudentList().get(i));
      //I want to get all student's name,subject and registrationNo 

        }

    }

}

Thanks.

1
  • Just use your getters. Something like StudentList.getStudentList().get(i).getName() Commented Nov 26, 2015 at 18:12

2 Answers 2

5

Simply give Student a public String toString() method override, one that returns a String holding all key properties, and then this will work fine:

System.out.println(StudentList.getStudentList().get(i));
Sign up to request clarification or add additional context in comments.

Comments

3

Instead of System.out.println(StudentList.getStudentList().get(i));, you should access the student's properties and print it out.

Student s = (Student) StudentList.getStudentList().get(i);
System.out.println(s.getName());
System.out.println(s.getCourse());
System.out.println(s.getRegistrationNo());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.