通过preparestatement的语句执行 order by 不排序

我尝试通过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;


吃鸡游戏
浏览 307回答 1
1回答

暮色呼如

在准备好的语句中,您使用?参数或占位符来传递实际值而不是列名,例如在WHERE子句中:WHERE column1 = ?.所以改成这样:String&nbsp;sql&nbsp;=&nbsp;"SELECT&nbsp;&nbsp;*&nbsp;FROM&nbsp;student&nbsp;ORDER&nbsp;BY&nbsp;"&nbsp;+&nbsp;name;并删除这一行:prepareStatement.setString(1,&nbsp;name);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java