我在獲取servlet以在我的動態Web項目中工作時遇到問題。我在我的StudentUtils類中有一個工作正常的方法。它連接到我的sql數據庫並打印內容到控制檯。見下文。訪問DAO並調用工作方法的Servlet(java)
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");
}
此方法時運行打印每個學生控制檯屏幕和消息added
的。但是,我需要創建一個運行此方法的servlet。
到目前爲止,在我的servlet中,我創建了一個我的dao(studentUtils)實例和一個空數組列表,但隨後我試圖使用dao中的方法填充數組列表。見下文。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentUtils studetUtils = new StudentUtils();
ArrayList <Student> list = new ArrayList<Student>();
任何幫助讓我的servlet填充這個數組列表使用我在dao的方法將不勝感激。
不應該getAllStudents()返回列表? – OldProgrammer