public int getCount() {
int count = 0;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select count(*) from table_name";
try {
con=this.getConnection();
if (con!=null) {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
count=rs.getInt(1);
System.out.println("count:"+count);
}
}
} catch (Exception e) {
// TODO: handle exception
}
return count;
}
---------------------
还可以这样:
select count(1) as numb from ...
rs.getInt("numb");
是这样子的
select count(*) from [table]
rs.next();
int count =rs.getInt(1);
就可以了
Statement stmt = con.createStatement();
ResultSet rs=null;
rs=stmt.executeQuery(sql);
if(rs.next())
count=rs.getInt(1);
不然你取的数据集是无效的,因为rs开始是指向第一组数据之前的