简体   繁体   English

提供参数时查询不起作用

[英]Query doesn't work when parameter's are provided

This Works just fine 这很好

conn = DatabaseConnection.getConnection();
stmt = conn.prepareStatement("SELECT * FROM Persons ORDER by firstName Desc");
rs = stmt.executeQuery();

But this one doesn't work 但是这个不起作用

conn = DatabaseConnection.getConnection();
stmt = conn.prepareStatement("SELECT * FROM Persons ORDER by ? ?");
stmt.setString(1, "firstName");
stmt.setString(2, "Desc");
rs = stmt.executeQuery();

Not sure why this would not work. 不知道为什么这行不通。 Both my parameters are variables and that's the reason I would want to set it explicitly. 我的两个参数都是变量,这就是我要明确设置它的原因。

Placeholders (?) in prepared statements are used for column replacement. 准备好的语句中的占位符(?)用于列替换。 You cannot set the 'DESC' attribute of ORDER BY using setString method. 您不能使用setString方法设置ORDER BY的'DESC'属性。

From PreparedStatement setString javadocs : PreparedStatement setString javadocs

  * Sets the designated parameter to the given Java <code>String</code> value. * The driver converts this * to an SQL <code>VARCHAR</code> or <code>LONGVARCHAR</code> value * (depending on the argument's * size relative to the driver's limits on <code>VARCHAR</code> values) * when it sends it to the database. 

In case you want to take the ORDER as a parameter to your DAO method then simply use String replacement in the query. 如果要将ORDER用作DAO方法的参数,则只需在查询中使用字符串替换即可。 Maybe like this: 也许是这样的:

public myDAOMethod(String firstName, String order) {

    String query = "SELECT * FROM Persons ORDER by ? " + order;
    conn = DatabaseConnection.getConnection(query);
    stmt = conn.prepareStatement();
    stmt.setString(1, "firstName");
    rs = stmt.executeQuery();

}

Actually your query converted into something like in case of prepared statement: 实际上,您的查询转换为类似预准备语句的情况:

SELECT * FROM Persons ORDER by 'firstname' 'Desc'

This behaviour provides protection against SQL injection. 此行为可防止SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate 查询不适用于将值作为绑定参数 (?) 但在放置在查询中时有效 - Hibernate query doesn't work with value as a bound parameter (?) but works when placed in query 带有本机查询和有序参数的查询字符串,sql 不起作用 - Query String with native query and ordered parameter, sql doesn't work 给NamedParameterJdbcOperation获取日期作为参数时,JDBC查询不起作用(在params映射中) - JDBC query doesn't work when giving NamedParameterJdbcOperation gets date as parameter (in params map) SpringBoot - 为什么 @Cacheable 在其方法有一个对象作为参数时不起作用? - SpringBoot - Why @Cacheable doesn't work when it's method has an object as parameter? 提供了 2 个参数但查询中仅存在 1 个参数的错误 - Error that 2 parameter(s) provided but only 1 parameter(s) present in query Apache Camel sftp 延迟查询参数似乎不起作用 - Apache Camel sftp delay query parameter doesn't seem to work XSLT参数不起作用 - XSLT parameter doesn't work sqlite数据库未填充,查询不起作用 - sqlite database is not filled and query's doesn't work JPA 查询:java.lang.IllegalArgumentException:提供了至少 1 个参数,但查询中只存在 0 个参数 - JPA query: java.lang.IllegalArgumentException: At least 1 parameter(s) provided but only 0 parameter(s) present in query JPQL查询不起作用 - JPQL Query doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM