从数据库删除行有什么问题? [英] What's the problem with deleting a row from database?

查看:49
本文介绍了从数据库删除行有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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>

推荐答案

显然,您使用双引号创建了表(开始时这是一个非常糟糕的主意).完成此操作后,您必须始终 将表名和列名用双引号括起来-随处可见.

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.

所以

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

应为:

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

这篇关于从数据库删除行有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆