gpt4 book ai didi

java - 执行 order by 语句 by preparedstatement 不排序

转载 作者:行者123 更新时间:2023-12-02 10:36:36 24 4
gpt4 key购买 nike

我尝试通过preparedstatement查询一个带有order by语句的select sql,它执行了,但是打印时,它没有排序。有人帮助我吗?

Connection conn = ConnectionUtils.getConnect();
String sql = "SELECT * FROM student ORDER BY ?";
List<Student> listStudent = new ArrayList<>();
try {
PreparedStatement prepareStatement = conn.prepareStatement(sql);
prepareStatement.setString(1, name);
ResultSet rs = prepareStatement.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId();
......vv
listStudent.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listStudent;

最佳答案

在准备好的语句中,您使用 ? 参数或占位符来传递实际值而不是列名称,如在 WHERE 子句中: WHERE column1 = ?

所以改成这样:

String sql = "SELECT  * FROM student ORDER BY " + name;

并删除此行:

prepareStatement.setString(1, name);

关于java - 执行 order by 语句 by preparedstatement 不排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com