【问题标题】:What's the problem with deleting a row from database?从数据库中删除一行有什么问题?
【发布时间】:2019-07-04 10:42:39
【问题描述】:

我想从我的数据库中删除一行,但它不起作用。我有2个例外: - NumberFormatException: null(用于 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>

【问题讨论】:

    标签: java postgresql jdbc quoted-identifier


    【解决方案1】:

    显然,您使用双引号创建了您的表(这是一个非常糟糕的开始)。一旦你这样做了,你必须在双引号中总是 enclose your table and column names - 无处不在。

    所以

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

    应该是:

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

    【讨论】:

    • 现在它正在工作,谢谢,但仍然得到 :NumberFormatException: null
    • @vv_wow:那是完全不同的东西——但你应该ask another question——每个问题只问一个问题(向那里的 parseInt() 函数提供输入值。
    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 2018-12-19
    • 2014-12-29
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多