谁能告诉我一种在允许启动下一个任务之前强制完成 java 中的一个任务的方法?具体来说,我想编辑下面的代码,以便在调用下一个标记的两行代码之前完全完成前标记的两行代码。
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String idString = req.getParameter("id");
Long id = new Long(idString);
//complete the actions specified on next two lines
School school = new SchoolDAO().findSchool(id);
req.setAttribute("school", school);
//before even starting the actions specified on the next two lines
List<CourseSummary> coursesummaries = new CourseSummaryDAO().findAllcsum(id);
req.setAttribute("coursesummaries", coursesummaries);
jsp.forward(req, resp);
}
编辑:
为了更好地理解费尔南多的建议,我将 SchoolDAO 的一些相关部分包括如下:
public class SchoolDAO extends DataAccessObject{
public School findSchool(Long id) {
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
connection = getConnection();
String sql = "select * from schoolprog where id=?";
statement = connection.prepareStatement(sql);
statement.setLong(1, id.longValue());
rs = statement.executeQuery();
if (!rs.next()) {return null;}
return readSchool(rs);
}
catch (SQLException e) {throw new RuntimeException(e);}
finally {close(rs, statement, connection);}
}
private School readSchool(ResultSet rs) throws SQLException {
Long id = new Long(rs.getLong("id"));
String spname = rs.getString("spname");
String spurl = rs.getString("spurl");
School school = new School();
school.setId(id);
school.setName(spname);
school.setUrl(spurl);
return school;
}
}
同样,CourseSummaryDAO 包含:
public class CourseSummaryDAO extends DataAccessObject{
public List<CourseSummary> findAllcsum(Long sid) {
LinkedList<CourseSummary> coursesummaries = new LinkedList<CourseSummary>();
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
connection = getConnection(); //this is the line throwing null pointer error
String sql = "select * from coursetotals where spid=?";
statement = connection.prepareStatement(sql);
statement.setLong(1, sid);
rs = statement.executeQuery();
//for every row, call read method to extract column
//values and place them in a coursesummary instance
while (rs.next()) {
CourseSummary coursesummary = readcsum("findAll", rs);
coursesummaries.add(coursesummary);
}
return coursesummaries;
}
catch (SQLException e) {throw new RuntimeException(e);}
finally {close(rs, statement, connection);}
}
程序中断的行是:
connection = getConnection(); //