我尝试通过preparestatement查询带有order by statement的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;
1 回答
暮色呼如
TA贡献1853条经验 获得超9个赞
在准备好的语句中,您使用?
参数或占位符来传递实际值而不是列名,例如在WHERE
子句中:WHERE column1 = ?
.
所以改成这样:
String sql = "SELECT * FROM student ORDER BY " + name;
并删除这一行:
prepareStatement.setString(1, name);
添加回答
举报
0/150
提交
取消