获取ResultSet行数时需要使用可回滚的游标,自定义方法,获取行数后要回滚,获取ResultSet列数时通过ResultSetMetaData类的getColumnCount方法即可获得。
示例代码:
public
class
Test
...
{
public static void main(String args[]) ...{
DBConnect conn = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
String sql = "select * from customers";
conn = new DBConnect();
conn.setPstmt(sql);
rs = conn.executeQuery(sql);
rsmd = rs.getMetaData();
System.out.println("行数=" + GetNumRows(rs));
system.out.println("列数=" + rsmd.getColumnCount());
}
private int GetNumRows(ResultSet rs) throws Exception ...{
//通过改方法获取结果集的行数
int result = 0;
if (rs.last()) ...{
result = rs.getRow();
rs.beforeFirst();//光标回滚
}
return result;
}
}
public static void main(String args[]) ...{
DBConnect conn = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
String sql = "select * from customers";
conn = new DBConnect();
conn.setPstmt(sql);
rs = conn.executeQuery(sql);
rsmd = rs.getMetaData();
System.out.println("行数=" + GetNumRows(rs));
system.out.println("列数=" + rsmd.getColumnCount());
}
private int GetNumRows(ResultSet rs) throws Exception ...{
//通过改方法获取结果集的行数
int result = 0;
if (rs.last()) ...{
result = rs.getRow();
rs.beforeFirst();//光标回滚
}
return result;
}
}
GetNumRows方法为获取行数的方法(参数为ResultSet)