public class ProductDaoImpl implements ProductDao {
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
//根据tid返回每个种类的总数
@Override
public int findCountProduct(int tid) throws SQLException {
String sql = "select count(1) from product where t_id = ?";
int i = (int)qr.query(sql, new ScalarHandler(), tid);
return i;
}
}
public class MyTest {
public static void main(String[] args) throws Exception {
ProductDao productDao = new ProductDaoImpl();
System.out.println(productDao.findCountProduct(1));
}
}
qr.query()返回object类型 ,先转成 ScalarHandler的Long类型 然后 在转为 int类型,之前我直接就转成int类型所以就GG了呱~。