首页 java数据 (JDBC)中ResultSet的获取条数

(JDBC)中ResultSet的获取条数

Class.forName(“com.mysql.jdbc.Driver”); String url = “jdbc:m…

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/yq1012";
Connection conn = DriverManager.getConnection(url, "yq1012", "yq1012");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from yq1012");

以上是ResultSet 的记录获取。

想要获取其条数

有以下几种方法

 

 

第一种:
ResultSet rs = st.executeQuery("select * from yq1012");
rs.last(); // 将光标移动到最后一行
int rowCount = rs.getRow(); // 得到当前行号,即结果集记录数

注意:如果还要用结果集,就把指针再移到初始化的位置  rs.beforeFirst();

 

 

第二种:利用循环ResultSet的元素来获得ResultSet的总行数
ResultSet rs = st.executeQuery("select * from yq1012");
int rowCount = 0;
while(rs.next()) {
    rowCount++;
}

 

第三种:利用SQL语句来查询
String sql = "select count(*) rec from (select * from yq1012) ww";
ResultSet rs = st.executeQuery(sql);
int rowCount = 0;
while (rs.next()) {
    rowCount = rs.getInt("rec");
}

 

一般我都是用 sql语句来查询的。。

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

为您推荐

mysql  优化提升

mysql 优化提升

desc  分别使用DESCRIBE和DESC查看表tb dept1和表tb emp1的表结构。 查看表详细结构语句SH...
navicat 能打开root链接不知道密码直接修改mysql 密码

navicat 能打开root链接不知道密码直接修改mysql 密码

找到系统自带 mysql 数据库   update user set password= password(&...
Cannot access aliyunmaven ( xxx ) in offline mode and the artifact

Cannot access aliyunmaven ( xxx ) in offline mode and the artifact

Cannot access aliyunmaven ( xxx ) in offline mode and the ar...
mysql 基础入门

mysql 基础入门

第一章 SQL的介绍 1.1什么是sql SQL:Structure Query Language。(结构化查询语言),...
You can’t specify target table ‘caiji_data_meiwen’ for update in FROM clause

You can’t specify target table ‘caiji_data_meiwen’ for update in FROM clause

  mysql 中不能自己 引用本身 要再加一层 不行 DELETE from `caiji_data_mei...
返回顶部