准备语句的问题
preparedStatement = connection.prepareStatement("select fname,lname, "
+ "sportman_code,start,finish,salary,amount,number,pnumber "
+ "from sportman,customer "
+ "where customer.customer_code = "
+ "sportman.customer_code order by ? limit ?,?");
preparedStatement.setString(1, "fname");
preparedStatement.setInt(2, 0);
preparedStatement.setInt(3, 9);
resultSet = preparedStatement.executeQuery();
订购依据不起作用。 为什么?
当我用 fname 代替时?它工作正常。
"sportman.customer_code order by fname limit ?,?");
我怎样才能做到这一点?
preparedStatement = connection.prepareStatement("select fname,lname, "
+ "sportman_code,start,finish,salary,amount,number,pnumber "
+ "from sportman,customer "
+ "where customer.customer_code = "
+ "sportman.customer_code order by ? limit ?,?");
preparedStatement.setString(1, "fname");
preparedStatement.setInt(2, 0);
preparedStatement.setInt(3, 9);
resultSet = preparedStatement.executeQuery();
order by didn't work.
why?
when i put fname instead ? it work correctly.
"sportman.customer_code order by fname limit ?,?");
how can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 ORDER BY 有效,但不符合您的预期。当您使用
它时,它会像这样进行排序
,而不是像您期望的那样
您问题中的代码就像按字母顺序对 M&M 包装进行排序
Your ORDER BY works, but not as you expect it to. When you use
it will make an ORDER BY like this
and not as you expect
The code in your question will then be like sorting a package of M&Ms alphabetically
您不能绑定表名或列名等标识符,只能绑定要插入、比较等的值
You can't bind in identifiers like table names or column names, only values that you want to insert, compare, etc
绑定适用于查询中的文字,不适用于关键字或标识符。如果您希望排序字段是动态的,则需要使用另一种方法来清理它。
Binding works for literals in the query, not for keywords or identifiers. You'll need to use another approach for sanitizing the sort field if you want it to be dynamic.