Create a DBase.getPupils() method
Update: Follow this link to watch a YouTube demonstration of the following
A method to fetch all the pupils. They are wrapped in the Student class and then returned as an ArrayList
public static ArrayList<Student> getPupils() { ArrayList<Student> students = new ArrayList(); Statement statement; makeConnection(); try { statement = con.createStatement(); ResultSet rs = statement.executeQuery("SELECT * FROM Student"); while (rs.next()) { Student student = new Student(); student.setForename(rs.getString("Forename")); student.setSurname(rs.getString("Surname")); student.setGender(rs.getString("Gender")); student.setForm(rs.getString("Form")); students.add(student); } con.close(); } catch (SQLException ex) { System.err.println(ex); } return students; }
Use the getPupils() method in the GUI
package dbase2016; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * * @author ajb */ public class GUIDisplayStudents extends JFrame { JTextArea textArea; JScrollPane scrollPane; GUIDisplayStudents(ArrayList<Student> students) { this.setSize(300,200); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textArea = new JTextArea(); scrollPane = new JScrollPane(textArea); for(Student student : students) { textArea.append(student.getForename() + " " + student.getSurname() + "\n"); } add(scrollPane); } }
package dbase2016; /** * * @author ajb */ public class DBase2016 { /** * @param args the command line arguments */ public static void main(String[] args) { GUIDisplayStudents displayPupils = new GUIDisplayStudents(DBase.getPupils()); displayPupils.setVisible(true); } }