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.