Using a prepared statement and variable bind Order By in Java with JDBC driver

前端 未结 3 1783
情深已故
情深已故 2020-12-01 14:27

I\'m using

  1. jdbcTemplate to make JDBC connections to a mySQL DB
  2. prepared statements to protect myself as much as possible from SQL injection attacks
3条回答
  •  我在风中等你
    2020-12-01 15:06

    Placeholders ? can only be used for parameter values but not with column and sort order directions. So the standard way to do this as is pointed e.g. here is to use String#format() or something similar to append your column name and order value to your query.

    Another option is to use Spring Data JPA where you can give to your method as an argument an instance of type Sort which can contain all needed info for database to sort.

提交回复
热议问题