Reputation: 37
I try to query a select sql with order by statement by preparestatement, It excuted, but when print, it 's not sorted. some one help me?
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;
Upvotes: 0
Views: 82
Reputation: 164139
In a prepared statement you use ?
parameters or placeholders to pass actual values and not column names, like in a WHERE
clause: WHERE column1 = ?
.
So change to this:
String sql = "SELECT * FROM student ORDER BY " + name;
and remove this line:
prepareStatement.setString(1, name);
Upvotes: 1