Java JDBC PreparedStatement未替换?带设置字符串(MariaDB)

Java JDBC PreparedStatement未替换?带设置字符串(MariaDB),java,mysql,jdbc,mariadb,Java,Mysql,Jdbc,Mariadb,我正试图从数据库中获取特定作者的书籍。我将JDBC与MariaDB一起使用。我使用的驱动程序是mariadb-java-client-2.3.0.jar 我的DAO课上有以下内容 public class BookDao implements Dao<Book> { Connection con; ... ... ... public List<Book> getByAuthor(String author) { String qu

我正试图从数据库中获取特定作者的书籍。我将JDBC与MariaDB一起使用。我使用的驱动程序是
mariadb-java-client-2.3.0.jar

我的DAO课上有以下内容

public class BookDao implements Dao<Book>
{
    Connection con;
...
...
...
    public List<Book> getByAuthor(String author)
    {
        String query_str = "select * from BOOKS where Author = ?";
        List<Book> res = new ArrayList<Book>();
        try
        {
            PreparedStatement stmt = con.prepareStatement(query_str);
            stmt.setString(1, author);
            System.err.println(stmt);
            ResultSet rset = stmt.executeQuery(query_str);
            while(rset.next())
                res.add(bookFromRset(rset));
        }
        catch(Exception ex)
        {
            System.err.println("Error while selecting * from BOOKS by author");
            System.err.println(ex);
            // throw ex;
        }
        return res;
    }
...
...
...
}
似乎它只是不能代替这个?符号与我的字符串。 更有趣的是,我有一个方法,它通过ID获取书籍,并且可以工作:

    public Book get(int id)
    {
        String query_str = "select * from BOOKS where Id = ?";
        Book res = null;
        try
        {
            PreparedStatement stmt = con.prepareStatement(query_str);
            stmt.setInt(1, id);
            System.out.println(stmt);
            ResultSet rset = stmt.executeQuery();
            rset.next();

            res = bookFromRset(rset);
        }
        catch(Exception ex)
        {
            System.err.println(ex);
        }
        return res;
    }
我还有一个
UserDao
类,它有字符串替换,它也可以工作:

public class UserDao implements Dao<User>
{
    Connection con;

...

    public User get(String username, String password)
    {
        String query_str = "select * from USERS where Username = ? and Password = ?";
        User res = null;
        try
        {
            PreparedStatement stmt = con.prepareStatement(query_str);
            stmt.setString(1, username);
            stmt.setString(2, password);

            System.out.println(stmt);
            ResultSet rset = stmt.executeQuery();
            rset.next();

            res = userFromRset(rset);
        }
        catch(Exception ex)
        {
            System.err.println(ex);
        }
        return res;
    }

...

}
我不明白为什么
getByAuthor
不起作用。我试过把
符号放在周围吗?但还有一个例外。我还尝试了直接替换方法,比如
stringquery\u str=“select*fromsbooks,其中Author='stephenking',它工作了,但我想使用任何字符串,所以它没有用

关于这个问题有什么提示吗?

您正在打电话

stmt.executeQuery(查询)

这是
语句
的一种方法,用于执行给定参数
query\u str
中的查询。它不执行准备好的语句。看

改为

stmt.executeQuery()
-无参数方法


这是
PreparedStatement
中的方法。

使用
executeQuery()
执行(查询)@一匹没有名字的马非常感谢。我甚至不知道我怎么会错过这样的错误。
    public Book get(int id)
    {
        String query_str = "select * from BOOKS where Id = ?";
        Book res = null;
        try
        {
            PreparedStatement stmt = con.prepareStatement(query_str);
            stmt.setInt(1, id);
            System.out.println(stmt);
            ResultSet rset = stmt.executeQuery();
            rset.next();

            res = bookFromRset(rset);
        }
        catch(Exception ex)
        {
            System.err.println(ex);
        }
        return res;
    }
public class UserDao implements Dao<User>
{
    Connection con;

...

    public User get(String username, String password)
    {
        String query_str = "select * from USERS where Username = ? and Password = ?";
        User res = null;
        try
        {
            PreparedStatement stmt = con.prepareStatement(query_str);
            stmt.setString(1, username);
            stmt.setString(2, password);

            System.out.println(stmt);
            ResultSet rset = stmt.executeQuery();
            rset.next();

            res = userFromRset(rset);
        }
        catch(Exception ex)
        {
            System.err.println(ex);
        }
        return res;
    }

...

}
MariaDB [library]> desc BOOKS;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| Id        | int(11)      | NO   | PRI | NULL    | auto_increment |
| Name      | varchar(255) | YES  | UNI | NULL    |                |
| Author    | varchar(255) | YES  |     | NULL    |                |
| Genre     | varchar(255) | YES  |     | NULL    |                |
| Available | int(11)      | NO   |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
5 rows in set (0.001 sec)