String sqlCount = "select count(id) as allcount from guestbook";//guestbook是表名
public class RecordCount {
//当前页的页码
private int totalPageNumber;
public int getTotalPageNumber() {
return totalPageNumber;
}
public void setTotalPageNumber(int totalPageNumber) {
this.totalPageNumber = totalPageNumber;
}
}
Context initContext;
private DataSource ds;
try {
initContext = new InitialContext();
ds = (DataSource) initContext.lookup("java:/comp/env/jdbc/oracleds");
} catch (NamingException e) {
e.printStackTrace();
}
//查询
QueryRunner qr = new QueryRunner(ds);
List result = null;
try {
result = (List)qr.query(sqlCount, null, new BeanListHandler(RecordCount.class));//RecordCount是专门为得到记录数编写的类 } catch (SQLException e) {
e.printStackTrace();
}
用这样的方式直接查询表中的记录是没有问题的,根据表的不同RecordCount这个类做相应的修改,但是我要将表中的有多少记录查出来,请问该怎么做,用传统的数据库访问方式是很容易得到的,我想用DBUtiles组件中的方法,请教各位高手了。。
public class RecordCount {
//当前页的页码
private int totalPageNumber;
public int getTotalPageNumber() {
return totalPageNumber;
}
public void setTotalPageNumber(int totalPageNumber) {
this.totalPageNumber = totalPageNumber;
}
}
Context initContext;
private DataSource ds;
try {
initContext = new InitialContext();
ds = (DataSource) initContext.lookup("java:/comp/env/jdbc/oracleds");
} catch (NamingException e) {
e.printStackTrace();
}
//查询
QueryRunner qr = new QueryRunner(ds);
List result = null;
try {
result = (List)qr.query(sqlCount, null, new BeanListHandler(RecordCount.class));//RecordCount是专门为得到记录数编写的类 } catch (SQLException e) {
e.printStackTrace();
}
用这样的方式直接查询表中的记录是没有问题的,根据表的不同RecordCount这个类做相应的修改,但是我要将表中的有多少记录查出来,请问该怎么做,用传统的数据库访问方式是很容易得到的,我想用DBUtiles组件中的方法,请教各位高手了。。
int result = -1;
try {
result = (Integer)qr.query(sqlCount, null, new ScalarHandler(1));
} catch (SQLException e) {
e.printStackTrace();
}
兄台你的方法我测试过了,执行到result = (Integer)qr.query(sqlCount, null, new ScalarHandler(1));就出错了
int result = -1;
try {
result = (Integer)qr.query(sqlCount, new ScalarHandler());
} catch (SQLException e) {
e.printStackTrace();
}