What's the problem with deleting a row from database?

倖福魔咒の 提交于 2021-02-11 12:38:10

问题


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>

回答1:


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\" = ?"


来源:https://stackoverflow.com/questions/56886234/whats-the-problem-with-deleting-a-row-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!