0

I am having trouble getting a servlet to work in my dynamic web project. I have a method in my StudentUtils class which works fine. It connects to my sql database and prints the contents to the console. See below.

public class StudentUtils {

public static void main (String[] args) throws Exception {

getAllStudents();

}   

public static void getAllStudents() throws Exception {

    try {

        Connection dbase = getConnection();
        String mysql = "select * from course";

        PreparedStatement output = dbase.prepareStatement(mysql);

        ResultSet rs = output.executeQuery();

        while(rs.next()) { 

            ArrayList <Student> list = new ArrayList<Student>();

            Student s = new Student(null, null, null);

            s.setStudentID(result.getString(1));
            s.setStudentName(result.getString(2));
            s.setStudentLevel(result.getString(3));

            list.add(s);

            System.out.println(s.toString());

            System.out.println("added"); } } 

    catch(Exception e) {

            System.out.println("could not add");
    }

This method when run prints each of the students to the console screen and the message added. However, I need to create a servlet which runs this method.

So far, in my servlet, I have created an instance of my dao (studentUtils) and an empty array list but then I am stuck with trying to populate the array list using the method in the dao. See below.

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    StudentUtils studetUtils = new StudentUtils();
    ArrayList <Student> list = new ArrayList<Student>();

Any help in getting my servlet to populate this array list using my method in the dao would be appreciated.

YakovL
  • 7,557
  • 12
  • 62
  • 102
JavaNewbie
  • 21
  • 9

1 Answers1

0

Your method getAllStudents() should probably return List<Student>. Also, note that in your current implementation the List is created inside the loop, re-creating it in each iteration, which is probably not the behaviour you want. You should create it outside the loop.

Apart from that, you should probably close your resources like Connection after using them.

user140547
  • 7,750
  • 3
  • 28
  • 80
  • Thanks. I have added List to my method and set the return. I have also moved the list to outside of the loop. What do I need to add to my servlet to iterate over this is/run the method? – JavaNewbie Mar 06 '16 at 16:19
  • @JavaNewbie: You can call `List list = StudentUtils.getAllStudents()` in your servlet and then output something or (better) use a JSP as described in the duplicate question. – user140547 Mar 06 '16 at 16:29
  • When I do that and run the servlet, I get the `System.out.println("could not add")` exception from the DAO (StudentUtils). – JavaNewbie Mar 06 '16 at 16:38
  • Then use `e.printStackTrace()` to find out what exception is thrown – user140547 Mar 06 '16 at 16:40
  • I get a Null Pointer Exception at getAllStudents(). It also shows an empty arraylist as I have printed that to console too. – JavaNewbie Mar 06 '16 at 16:45
  • Should I be putting the 'List list = StudentUtils.getAllStudents()' in the doGet method? I have spent hours on this now and not got anywhere. – JavaNewbie Mar 06 '16 at 21:18