Search code examples
javaservletshsqldbserver-sidedao

How to delete a row in a table using JSP. Error: NumberFormatException: null. -> Integer.parseInt(Unknown Source)


I'm using a HSQLDB, trying to get CRUD operations to function on a table. Having trouble with the delete function. I'm not sure if I'm passing in the value for 'carId' correctly.

Edit: Now when I hit the delete link, a blank page is displayed with no errors showing.

index.jsp

<td>
      <c:if test="${sessionScope.user != null}">
      <a name="${car.id}"  href="DeleteCarServlet?carId=${car.id}"> Delete </a>
      </c:if>
</td>

CarDAO

public Car deleteCar(int id) {

    Connection connection = Utils.getConnection();
    Car car = null;
    try {
        PreparedStatement psmt = connection
                .prepareStatement("DELETE FROM CAR WHERE ID = ?");
        psmt.setInt(1, id);
        psmt.executeUpdate();
        //list();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return car;
}

DeleteCarServlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    int carId = Integer.parseInt(request.getParameter("carId"));
    Car car = CarDAO.instance.getById(carId);
    CarDAO.instance.deleteCar(carId);

    request.getRequestDispatcher("index.jsp").forward(request, response);

>


Solution

  • <a name="${car.id}" href="DeleteCarServlet?carId=${car.id}"> Delete </a>.

    anchor tags make a GET request, not a POST.

    your request is ending up in doGet()