具有多个条件的JDBC删除查询

时间:2018-04-15 05:00:05

标签: java sql jdbc

我一直在努力使用SQL Delete查询。我希望它删除一行,满足2条件。我得到的错误说我的SQL语法在最后的结尾处是错误的')'。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Test</title>
</head>
<body>
    <div class="container">
        <br>
        <h1 class="text-success text-center">Test Test</h1>
        <div class="col-lg-8 m-auto d block">

            <form action="#" onsubmit="return validation()" class="bg-light">

            <div class="form-group">
                <label>FieldLabel:</label> <input type="text" name="fieldLabel"
                    class="form-control" id="fieldLabel"></input> <span
                    id="fieldLabelError" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label>Version:</label> <input type="text" name="version"
                    class="form-control" id="version"></input> <span id="versionError"
                    class="text-danger"></span>
            </div>

            <input id="submit" name="submit" type="submit" value="Submit"
                onclick="validation()">

        </form>
    </div>
</div>

<script type="text/javascript">
    function validation() {
        var fieldLabel = document.getElementById("fieldLabel").value;
        var version = document.getElementById("version").value;
            if (fieldLabel == "") {
            document.getElementById("fieldLabelError").innerHTML = "**Please enter Field label";
            return false;
        }
            if (version == "") {
                document.getElementById("versionError").innerHTML = "**Please enter version";
                return false;
            }

        return true;
    }
</script>

</body>
</html>

我的变量具有正确的值,数据库中的数据完全对应。

4 个答案:

答案 0 :(得分:4)

以下是原始查询的示例:

DELETE
FROM course
WHERE username_entry = tim AND course_name = chemistry;

当然,这不是有效的SQL,因为您要将文本列与其他名为timchemistry的列进行比较。你真的希望上面的查询看起来像这样:

DELETE
FROM course
WHERE username_entry = 'tim' AND course_name = 'chemistry';

换句话说,您需要与正确转义的字符串文字进行比较。但实际上,最好的办法是使用自动处理格式的预处理语句:

String sql = "DELETE FROM course WHERE username_entry = ? AND course_name = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, courseToDelete.toUpperCase());
ps.executeUpdate();

答案 1 :(得分:-1)

您需要将字符串值封装到引号

String sql = "DELETE FROM course 
              WHERE (username_entry = '" + username + 
            "' AND course_name = '" + courseToDelete.toUpperCase() + "')";

但更好的方法是使用准备好的语句,因为它们会自动逃避

答案 2 :(得分:-1)

您不必为查询使用任何打开/关闭语法!

正如其他答案中所建议的那样,您实际上不需要使用变量注入,而是使用PreparedStatement

String sql = "DELETE FROM course 
          WHERE username_entry = '" + username + 
        "' AND course_name = '" + courseToDelete +"'";

希望这有帮助!

答案 3 :(得分:-1)

您是否尝试在Where子句后删除大括号。

更改后的查询如下所示:

String sql = "DELETE FROM course 
          WHERE username_entry = '" + username + 
        "' AND course_name = '" + courseToDelete.toUpperCase()+"'";