• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > MySQL的JDBC判断查询结果是否为空以及获取查询结果行数的方法_MySQL

MySQL的JDBC判断查询结果是否为空以及获取查询结果行数的方法_MySQL

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了next,ResultSet,the,SQLException,row等相关知识,希望本文的分享对您有所帮助

判断查询结果是否为空

在JDBC中没有方法hasNext去判断是否有下一条数据,但是我们可以使用next方法来代替。 看next方法的官方解释:
  • boolean next()
          throws 
    Moves the cursor forward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

    When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown. If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw an SQLException on a subsequent call to next.

    If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.

    Returns:
    true if the new current row is valid; false if there are no more rows
    Throws:
    SQLException - if a database access error occurs or this method is called on a closed result set
    翻译如下: boolean next() throws SQLException 将当前行从上一行移到下一行。一个 ResultSet的当前行最初指向第一行查询结果前。当第一次调用next的时候,当前行将会指向第一行查询结果。第二次调用就会指向第二行查询结果,等等。 当调用next方法返回false的时候,当前行当前行指向最后一行查询结果之后。这时候,任何ResultSet 的请求当前行的方法调用都会导致SQLException 被抛出。但如果查询的结果设置为TYPE_FORWARD_ONLY,next方法在这时候根据实现厂商的不同,可能会返回false也坑能会抛出SQLException 异常 的警告将会被清楚。
    关于的next的开始和结束,可以用下面的图来解释: 0->1->2->3->4->0 中间的1, 2, 3, 4是查询结果 ^ ^ 开始 结束
    判断JDBC查询结果是否为空的正确姿势:
    Statement statement = conn.createStatement();
    ResultSet res = statement.executeQuery(selectSql);
    if (!res.next()) {
        //res is null
    } else {
        // res is not null
    }

    获取查询结果的行数

    JDBC并没有直接提供获取查询结果总行数的方法给我们调用,为此我们需要使用间接的手段来执行:
    第一种方法:
    ResultSet res = ...使用某种方法获取查询结果
    int nRow = 0;
    while(res.next()) {
        ++nRow;
    }
    res.beforeFirst();
    // 其他代码不变

    第二种方法:
    ResultSet res = ...使用某种方法获取查询结果
    res.last();
    final int nRow = res.getRow();
    res.beforeFirst();
    // 其他代码不变
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MySQL的JDBC判断查询结果是否为空以及获取查询结果行数的方法_MySQL
  • 【设置字符集】Win7 64位系统安装MySQL5.5.21图解教程_MySQL

相关文章

  • 2017-05-11MySQL timestamp自动更新时间分享
  • 2018-12-05目前用到的两个分页存储过程代码
  • 2018-12-05Oracle之序列(主键自增)
  • 2018-12-05关于PostgreSQL 版本识别 的详解
  • 2018-12-05MySQL中的全文本搜索简介
  • 2018-12-05Oracle Connect to Idle Instance解决方法
  • 2018-12-05如何使用mysql完成excel中的数据生成
  • 2017-05-11linux下备份MYSQL数据库的方法
  • 2018-12-05sql 分组查询问题
  • 2018-12-05sql2000挂起无法安装的问题的解决方法

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • Mysql中Insert into xxx on duplicate key update问题
    • MySQL索引的详解及实例教程
    • MySQL数据库的备份与维护详解
    • Cont()与Where().Count()有时性能差别如此之大!
    • Mysql下载安装、部署与图形化详细操作教程_MySQL
    • 在VB.NET应用中使用MySQL的方法
    • 财政年度表之建表约束
    • 在Win下mysql备份恢复命令
    • Mysql之EXPLAIN显示using filesort介绍
    • centos7下和linux下安装mysql有什么区别

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有