Dbutils中这个sql字符串怎么解决?
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;
}
在此之前,我已经导入了我需要的 Dbutils JAR,例如 org.apache.commons.dbutils.handlers.BeanListHandler
和
org.apache.commons.dbutils.QueryRunner
但是在 运行 我的项目之后,它出错了,消息是:
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]
我知道程序有问题,但我不想找到全部,我只想使用限制找到我的 table 的一部分?.? (我只知道这句sql可以做到)
你能帮帮我吗?
您将字符串作为参数传递,因此 运行 您的 SQL 字面上的限制为 LIMIT 'start','count'
试试这个:
Object params[] = {start, count };
这样您就可以构建实际 int
值的参数数组(现在自动装箱为 Integer
)
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;
}
在此之前,我已经导入了我需要的 Dbutils JAR,例如 org.apache.commons.dbutils.handlers.BeanListHandler
和
org.apache.commons.dbutils.QueryRunner
但是在 运行 我的项目之后,它出错了,消息是:
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]
我知道程序有问题,但我不想找到全部,我只想使用限制找到我的 table 的一部分?.? (我只知道这句sql可以做到)
你能帮帮我吗?
您将字符串作为参数传递,因此 运行 您的 SQL 字面上的限制为 LIMIT 'start','count'
试试这个:
Object params[] = {start, count };
这样您就可以构建实际 int
值的参数数组(现在自动装箱为 Integer
)