有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java MySQL查询错误SQLException

我在试图将查询请求实现到程序中时遇到了这个问题。不使用此方法连接到数据库可以正常工作,但是,这会引发SQLException

    String query = "SELECT EXISTS ( SELECT * FROM usr WHERE username = admin AND password = pass )";
    boolean result;
    Connection con = null;
    try {
        con = DriverManager.getConnection(url, username, password);
        Statement stmt = (Statement) con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        result = rs.next();
    } catch (SQLException e) {
        throw new RuntimeException("Cannot connect to the database!", e);
    } finally {
        System.out.println("Closing the connection.");
        if (con != null)
            try {
                con.close();
            } catch (SQLException ignore) {
                System.out.println("Unknown Error Connecting to MySQL Server!");
            }
    }

我将查询引入到方法中以隔离它。 任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    通常搜索字符串-需要单引号引用值-因此在where子句中替换为'admin''pass'