Search code examples
mysqljdbi

Binding table names to SQL query using JDBI


I am issuing a simple mysql select query using JDBI. In that if I try to bind the table name, I am getting wrong SQL syntax error.

Code snippet:

@SqlQuery("select * from <table> where rowid=:rowid")
@SingleValueResult
@Mapper(ContentRecordMapper.class)
public abstract Optional<Document> getRecord(@Define("table") String table, @Bind("rowid") String rowid);

If I execute this, I am getting

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<table> where rowid='row1'' at line 1 [statement:"select * from <table> where rowid=:rowid", located:"select * from <table> where rowid=:rowid", rewritten:"/* ContentDAO.getRecord */ select * from <table> where rowid=?", arguments:{ positional:{}, named:{rowid:'row1'}, finder:[]}]

If I hard code the table name, I am getting proper results. Kindly help. Thanks


Solution

  • You should annotate with @UseStringTemplate3StatementLocator. It was already discussed here.