从数据库删除行有什么问题?

时间:2019-07-04 10:42:39

标签: java postgresql jdbc quoted-identifier

我想从数据库中删除一行,但这行不通。我有2个例外: -NumberFormatException:空(用于Servlet中的parseInt部分) -PSQLException:错误:关系“患者”不存在...但是它确实存在! 你能帮我解决我的问题吗?

DB.java(只是方法)

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(只是方法)

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 个答案:

答案 0 :(得分:2)

显然,您使用双引号创建了表(开始时这是一个非常糟糕的主意)。完成此操作后,您必须始终 enclose your table and column names-在所有地方都用双引号引起来。

所以

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

应为:

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