我按下一个按钮以更改航班状态。但是我收到了NumberFormatException。下面,我介绍了从数据库中获取数据的方法。
private Flight getFlight(ResultSet rs) throws SQLException, DBException {
Flight flight = new Flight();
flight.setId(rs.getInt("ID"));
flight.setName(rs.getString(NAME));
flight.setStartPoint(rs.getString("STARTPOINT"));
flight.setEndPoint(rs.getString("ENDPOINT"));
flight.setNumber(rs.getInt("NUMBER"));
flight.setDepartureDate(rs.getString("DEPARTURE_DATE"));
flight.setDepartureTime(rs.getInt("DEPARTURE_TIME"));
flight.setStatus(rs.getBoolean("STATUS"));
flight.setAircompanyId(rs.getInt("AIRCOMPANY_ID"));
return flight;
}
public Flight findFlightById (int id) throws DBException{
Flight flight = null;
for (Flight f : getFlights()) {
if (f.getId() == id) {
flight = f;
break;
}
}
return flight;
}
public boolean updateFlightStatusById(int id)throws DBException{
Connection con = null;
PreparedStatement pstmt = null;
Boolean status = null;
try {
con = getConnection();
pstmt = con.prepareStatement(UPDATE_FLIGHT_STATUS);
Flight flight = findFlightById(id);
int k = 1;
if (flight.isStatus()) {
pstmt.setBoolean(k++, false);
status = true;
} else {
pstmt.setBoolean(k++, true);
status = false;
}
pstmt.setInt(k++, flight.getId());
pstmt.executeUpdate();
con.commit();
} catch (SQLException e) {
rollback(con);
throw new DBException("cannot update flight status", e);
} finally {
close(con);
close(pstmt);
}
return status;
}
然后,我在另一个类中调用接收到的方法,该类将路径返回到jsp页面
int id = Integer.parseInt(request.getParameter("flightId")); // here i get an exception
db.updateFlightStatusById(id);
session.setAttribute("flights", flight);
我还将在jsp页面中提供一些代码。
<c:forEach var="bean" items="${sessionScope.flights}">
<td><form action="controller" method="post">
<input type="hidden" name="command" value="getFlights">
<input type="hidden" name="flightId" value="${bean.id}">
<c:if test="${bean.status eq true}">
<input type="submit" value="<fmt:message key="correct"/>">
</c:if>
<c:if test="${bean.status eq false}">
<input type="submit" value="<fmt:message key="incorrect"/>">
</c:if>
</form></td>
</c:forEach>
我已经做了类似的任务,但是没有这样的错误。我到底是怎么了我将感谢您的帮助!
我的SQL请求
private static final String UPDATE_FLIGHT_STATUS = "UPDATE FLIGHT SET status=? where id=?";