0

I want to delete a row from my database, but it doesn't work. I've got 2 exception: - NumberFormatException: null (it's for the parseInt part in the Servlet) - PSQLException: ERROR: relation "patients" does not exist... but it does exist! Can u help me to solve my problem?

DB.java (just the method)

public int deletePatient(int patID) {
        try {
            if (conn != null) {
                String delete = "DELETE FROM \"Patients\" WHERE Patients.PatientID = ?";
                PreparedStatement pstmt = conn.prepareStatement(delete);
                pstmt.setInt(1, patID);
                affectedRows = pstmt.executeUpdate();
            } else {
                System.out.println("Connection is not created! 5");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return affectedRows;
    }
    return affectedRows;
}

ServletP.java (just the method)

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

        try {
            int patID = Integer.parseInt(request.getParameter("patID2"));
            db.deletePatient(patID);
        } catch (NumberFormatException n) {
            n.printStackTrace();
        }

        PrintWriter writer = response.getWriter();
        String htmlRespone = "<html>";
        htmlRespone += "<h2>Action is done!</h2>";
        htmlRespone += "<a href=\"/WebApp/patients\">Back</a>";
        htmlRespone += "</html>";
        writer.println(htmlRespone);
}

jsp:

<div>
            <form method="POST" action="patients">
                <table border="2" align="center">
                    <tr>
                        <td>ID:</td>
                        <td><input type="text" name="patID2"></td>
                        <td><input type="submit" value="Delete"></td>
                    </tr>
                </table>
            </form>
</div>
vv_wow
  • 23
  • 4

1 Answers1

2

Apparently you created your table(s) using double quotes (which is a really bad idea to begin with). Once you do that, you have to always enclose your table and column names in double quotes - everywhere.

So

"DELETE FROM \"Patients\" WHERE Patients.PatientID = ?"

should be:

"DELETE FROM \"Patients\" WHERE \"Patients\".\"PatientID\" = ?"
  • now it's working, thanks, but still got the :NumberFormatException: null – vv_wow Jul 04 '19 at 10:53
  • @vv_wow: that is something entirely different - but you should [ask another question](https://stackoverflow.com/questions/ask) for that - only ask one question per question please (provide the input values to the parseInt() function there. –  Jul 04 '19 at 10:54