【发布时间】:2021-07-28 10:59:28
【问题描述】:
我正在尝试从数据库中获取特定作者的书籍。我将 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;
}
...
...
...
}
然后我像这样在 main 中调用它:
public class Main
{
public static void main(String[] args)
{
BookDao bookHandler = new BookDao();
List<Book> books = bookHandler.getByAuthor("Stephen King");
for(Book b : books)
{
System.out.println(b);
}
}
}
由于某种原因,结果如下:
sql : 'select * from BOOKS where Author = ?', parameters : [''Stephen King'']
Error while selecting * from BOOKS by author
java.sql.SQLSyntaxErrorException: (conn=61) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1
似乎它只是不能替代 ?我的字符串的符号。 更有趣的是,我有通过 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;
}
...
}
这里是BOOKS表的描述:
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)
我不明白为什么getByAuthor 不起作用。我试过把' 符号放在周围?但有另一个例外。我也试过直接用String query_str = "select * from BOOKS where Author = 'Stephen King'";之类的方法替换,效果很好,但我想使用任何字符串,所以没用。
关于这个问题的任何提示?
【问题讨论】:
-
使用
executeQuery();不executeQuery(query_str); -
@a_horse_with_no_name 非常感谢。不知道我怎么会错过这样的错误。