0

我有一个具有以下布局的数据库表:

 Columns:
 _________________________
 id | user_name | password

但是我无法使用用户名删除指定的行。我收到以下错误:

MySQLSyntaxErrorException:“where 子句”中的未知列“vipin”

vipin是表中的一个值。谁能帮我?

public void deleteFclty() {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String username = removeText.getText();
    ArrayList<String> values = new ArrayList();

    String qry = "SELECT user_name From users ";
    try {
        stmt = (PreparedStatement) connection.prepareStatement(qry);
        rs = stmt.executeQuery();

        while (rs.next()) {
            values.add(0, rs.getString(("user_name")));
            System.out.println(values);
        }
    } catch (SQLException ex) {
        Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (values.contains(username)) {
        username = removeText.getText();
        Boolean isAdmin = false;

        try {
            System.out.println(username);
            preparedStatement = (PreparedStatement) connection.prepareStatement("DELETE FROM users WHERE user_name=" + username + "");
            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        Util.showErrorMessageDialog(username + " is not found.Please try again.");
    }
}
4

5 回答 5

5

由于您已经在使用PreparedStatement,请正确使用它并传递usernameas 参数,而不是仅仅连接字符串:

//no need to use a cast here
preparedStatement = connection.prepareStatement(
    //note the usage of ? instead of concatenating Strings
    "DELETE FROM users WHERE user_name=?");
//setting the first parameter in the query string to be username
preparedStatement.setString(1, username);
preparedStatement.executeUpdate();

使用它,您不会有任何连接问题,而且更好的是,您的代码不会容易出现SQL 注入


与您的问题没有直接关系,但如果您将代码移动到 execute和INSERTstatements到单个方法会更好。UPDATEDELETE

public void executeUpdate(Connection con, String query, Object ... params)
    throws SQLException {
    PreparedStatement pstmt = con.prepareStatement(query);
    if (params != null) {
        int i = 1;
        for(Object param : params) {
            pstmt.setObject(i++, param);
        }
    }
    pstmt.executeUpdate();
    pstmt.close();
}

因此,您的代码将大大减少为:

String deleteSQL = "DELETE FROM users WHERE user_name=?";
executeUpdate(deleteSQL, username);

请注意,您可以基于此方法创建一个新方法来执行SELECT语句。

另外,不要忘记关闭您的资源。使用这样的方法也可以显着减少这种情况:

public void closeResource(AutoCloseable res) {
    try {
        if (res != null) {
            res.close();
        }
    } catch (Exception e) {
        //handle this exception...
        //basic example, not meant to be used in production!
        e.printStacktrace(System.out);
    }
}

请注意ConnectionStatement(及其子项PreparedStatementCallableStatement)和ResultSet接口已经从AutoCloseable.

于 2013-07-11T14:47:31.483 回答
4

您没有引用要插入查询的用户名,因此它被视为对字段名称的引用:

DELETE FROM users WHERE user_name='"+username+"'"
                                  ^--          ^--

注意:像这样构建查询会使您容易受到SQL 注入攻击。改为使用准备好的语句和占位符。

于 2013-07-11T14:46:00.570 回答
1

您将要引用您String的 s

"DELETE FROM users WHERE user_name="+username+"";

像这样:

"DELETE FROM users WHERE user_name='" + username + "'";

更好的是PreparedStatement按预期使用:

"DELETE FROM users WHERE user_name = ?";

然后使用:

preparedStatement.setString(1, username);

打电话之前executeUpdate

于 2013-07-11T14:46:38.830 回答
1

我认为您可能需要在 where 子句中的用户名周围加上引号

connection.prepareStatement("DELETE FROM users WHERE user_name='"+username+"'");
于 2013-07-11T14:45:22.343 回答
0

查询应如下所示

preparedStatement = (PreparedStatement) connection.prepareStatement("DELETE FROM users WHERE user_name='"+username+"'");

注意:注意用于的单引号user_name='"+username+"'"

于 2013-07-11T14:53:23.040 回答