I make a button that changes the status of the flight. But I receive, NumberFormatException. Below I presented the methods where I get data from the database.
我做了一个改变航班状态的按钮。但我收到了,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;
}
Then I call the received method in another class, which returns the path to the jsp page
然后我在另一个类中调用receive方法,该类返回jsp页面的路径
int id = Integer.parseInt(request.getParameter("flightId")); // here i get an exception
db.updateFlightStatusById(id);
session.setAttribute("flights", flight);
Also I will provide some of the code with the jsp page.
此外,我将提供一些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>
I already did a similar task, but there was no such error. What was I wrong about? I will be grateful for the help!
我已经完成了类似的任务,但没有出现这样的错误。我错了什么?我将不胜感激!
My sql request private static final String UPDATE_FLIGHT_STATUS = "UPDATE FLIGHT SET status=? where id=?";
我的sql请求private static final String UPDATE_FLIGHT_STATUS =“UPDATE FLIGHT SET status =?where id =?”;
1 个解决方案
#1
3
From the documentation here and here, it's pretty straightforward.
从这里和这里的文档来看,它非常简单。
Integer.parseInt(String)
Parameters: s - a String containing the int representation to be parsed Returns: the integer value represented by the argument in decimal. Throws: NumberFormatException - if the string does not contain a parsable integer.
参数:s - 包含要解析的int表示形式的String返回:由十进制参数表示的整数值。抛出:NumberFormatException - 如果字符串不包含可解析的整数。
NumberFormatException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
抛出以指示应用程序已尝试将字符串转换为其中一种数字类型,但该字符串没有适当的格式。
If you debug your code you will find that the id
variable is either null
or has some other value that can't be converted to an integer. Check your request parameter map to see that a flightId
parameter exists and is not null.
如果您调试代码,您会发现id变量为null或具有一些无法转换为整数的其他值。检查请求参数映射以查看flightId参数是否存在且不为空。
#1
3
From the documentation here and here, it's pretty straightforward.
从这里和这里的文档来看,它非常简单。
Integer.parseInt(String)
Parameters: s - a String containing the int representation to be parsed Returns: the integer value represented by the argument in decimal. Throws: NumberFormatException - if the string does not contain a parsable integer.
参数:s - 包含要解析的int表示形式的String返回:由十进制参数表示的整数值。抛出:NumberFormatException - 如果字符串不包含可解析的整数。
NumberFormatException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
抛出以指示应用程序已尝试将字符串转换为其中一种数字类型,但该字符串没有适当的格式。
If you debug your code you will find that the id
variable is either null
or has some other value that can't be converted to an integer. Check your request parameter map to see that a flightId
parameter exists and is not null.
如果您调试代码,您会发现id变量为null或具有一些无法转换为整数的其他值。检查请求参数映射以查看flightId参数是否存在且不为空。