更新时间:2023-01-24 23:03:29
您只能绑定PreparedStatement
中的值,不能绑定语法元素或对象名(在这种情况下为表名).您将不得不求助于字符串操作:
You can only bind values in a PreparedStatement
, not syntactic elements or object names (in this case, the table name). You'll have to resort to string manipulation:
final String query = String.format("SELECT COUNT(*) FROM %s", tablename);
final PreparedStatement preparedStatement = connection.prepareStatement(query);
final ResultSet resultSet = preparedStatement.executeQuery();
请注意,此查询中没有占位符,因此使用PreparedStatement
相对于普通的Statement
是否真的有任何优势是令人怀疑的.
Note that there are no placeholders in this query, so it's questionable whether there's really any advantage in using a PreparedStatement
as opposed to a plain old Statement
.