Search code examples
javamysqljakarta-eecrudapache-commons-dbutils

How to solve this sql string in Dbutils ?


    public List<Hero> list() throws SQLException {
        return list(0, Short.MAX_VALUE);
    }

    public List<Hero> list(int start, int count) throws SQLException{

             Connection c = this.getConnection();                
             QueryRunner runner = new QueryRunner();
             String sql = "select * from hero order by id desc limit ?,? ";  
             Object params[] = {"start", "count" };                               
             List<Hero> heros = (List<Hero>) runner.query(c,sql,new BeanListHandler(Hero.class),params);     

             DbUtils.closeQuietly(c);    

             return heros;
    }

Before that I have imported Dbutils JAR that I need, like org.apache.commons.dbutils.handlers.BeanListHandler and org.apache.commons.dbutils.QueryRunner But after running my project, it goes wrongs whose message is :

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 ''start','count'' at line 1 Query: select * from hero order by id desc limit ?,? Parameters: [start, count]

I know that something wrong in program, but I dont want to find all, I just want to find part of my table using limit ?.? (I only know this sql sentence can do that).

Could u please help me?


Solution

  • You're passing strings as your parameters, so it's running your SQL limit literally as LIMIT 'start','count'

    Try this instead:

    Object params[] = {start, count };

    So that you're building a parameter array of your actual int values (now autoboxed to Integer)