从数据库中删除行

时间:2013-07-11 14:43:10

标签: java jdbc

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

 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.");
    }
}

5 个答案:

答案 0 :(得分:5)

由于您已经在使用PreparedStatement,请使用它并将username作为参数传递,而不是仅仅连接字符串:

//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 Injection


与您的问题没有直接关系,但如果您将代码移至单个方法执行INSERTUPDATEDELETE语句会更好。

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延伸。

答案 1 :(得分:4)

您尚未引用要插入查询的用户名,因此将其视为对字段名称的引用:

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

注意:构建这样的查询会让您对SQL injection attacks开放。使用预先准备好的语句和占位符。

答案 2 :(得分:1)

我认为你可能需要在where子句

中围绕用户名引用一些引号
connection.prepareStatement("DELETE FROM users WHERE user_name='"+username+"'");

答案 3 :(得分:1)

您想要引用String

"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

之前

答案 4 :(得分:0)

查询应如下所示

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

注意:请注意user_name='"+username+"'"

使用的单引号