public List<Course> searchCourse(String name) {
List<Course> list = null;
Course course = null;
PreparedStatement pstmt = null;
ResultSet rs = null,rs1 = null;
String sql = "select username,course_name from view_teacourse where username=? or course_name=? ";
try{
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, name);
rs = pstmt.executeQuery();
while(next()){
String sql1 = "select * from courseinfo where course_name=?";
pstmt.setString(1, rs.getString("cousre_name"));
rs1 = pstmt.executeQuery();
while(rs1.next()){
course.setCourseid(rs.getInt("course_id"));
course.setCourseName(rs1.getString("course_name"));
course.setCourseScheme(rs.getString("course_scheme"));
course.setCourseBook(rs.getString("course_book"));
course.setCourseTerm(rs.getString("course_term"));
course.setIntroduction(rs.getString("introduction"));
course.setStartTime(rs.getTimestamp("start_time"));
course.setEndTime(rs.getTimestamp("end_time"));
course.setValidation(rs.getBoolean("validation"));
course.setCourseType(rs.getInt("coursetypeid"));
list.add(course);
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
DBBase.releaseResultSet(rs1);
DBBase.releaseResultSet(rs);
DBBase.releaseStatement(pstmt);
DBBase.releaseConnection(conn);
}
return list;
}
rs.next()
的值變得錯誤。它不能進入while循環。但是,如果我寫rs.wasNull()
,所以我再次犯錯。所以我很困惑。我可以得到正確的結果在我的數據庫中使用相同的SQL。當它在一個Java文件中不起作用!我找不到錯在哪裏!爲什麼ResultSet不爲null但我無法從ResultSet獲取數據
next()方法在做什麼? – 2013-04-22 11:51:19
你說rs.next()是錯誤的。這意味着你沒有在數據庫中找到'name'的任何匹配項。所有其他問題都源於此。 – 2013-04-22 11:55:57
'Course course = null;' 但是這絕不會用對象初始化..! – 2013-04-22 11:57:01