java - SQL语法中的MySQLSyntaxErrorException

标签 java mysql jsp mysql-connector

我正在尝试使用准备好的语句从表中选择数据。但似乎我遇到了无法单独解决的语法错误。

try {

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mydb";
        String dbusername = "root";
        String dbpassword = ""; // Change it to your Password

        // Setup the connection with the DB
        connection = DriverManager.getConnection(url, dbusername,
                dbpassword);

        String query = "SELECT * FROM admin WHERE username = ? AND password = ?";

        try {
            // connection.setAutoCommit(false);
            selectUser = connection.prepareStatement(query);
            selectUser.setString(1, username);
            selectUser.setString(2, password);
            // Execute preparedstatement
            ResultSet rs = selectUser.executeQuery(query);

            // Output user details and query
            System.out.println("Your user name is " + username);
            System.out.println("Your password is " + password);
            System.out.println("Query: " + query);

            boolean more = rs.next();

            // if user does not exist set the validity variable to true
            if (!more) {
                System.out
                        .println("Sorry, you are not a registered user! Please sign up first!");
                user.setValid(false);
            }

            // if user exists set the validity variable to true
            else if (more) {
                String name = rs.getString("name");

                System.out.println("Welcome " + name);
                user.setName(name);
                user.setValid(true);
            }


        } catch (Exception e) {
            System.out.println("Prepared Statement Error! " + e);
        }   
    } catch (Exception e) {
        System.out.println("Log in failed: An exception has occured! " + e);
    } finally {
    }
    if (connection != null) {
        try {
            connection.close();
        } catch (Exception e) {
            System.out.println("Connection closing exception occured! ");
        }
        connection = null;
    }

    return user;
}

我收到以下错误。

Prepared Statement Error! com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND password = ?' at line 1

但我没有在该代码行中看到任何错误。

最佳答案

改变

 ResultSet rs = selectUser.executeQuery(query);

 ResultSet rs = selectUser.executeQuery();

如果您已经在 connection.prepareStatement(query); 中准备了语​​句,那么为什么要在 selectUser.executeQuery(query); 中再次传递查询

关于java - SQL语法中的MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996408/

相关文章:

java - 如何在 Eclipse 中将 github maven 项目作为 jar 文件导入?

java - 在方法参数中设置数组项

java - 如何在单元测试中设置不同的类路径以使用 Spring 加载资源

java - 如何将服务器端变量传递给 JSP 中的核心标记?

java - 替换字符数组中多个相同的字符

php - 如何使用名称从首字母 A - D 开始的 sql 显示结果

php - 如何仅在 laravel whereBetween() 上查询日期部分

MYSQL Concat、Left Join、Group解决方案

java - Tomcat 端口重定向在版本 6.0.28 中不起作用

jquery - 按钮作为取消重定向到上一页