最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-09 09:20:01
文档

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

MySQL的JDBC判断查询结果是否为空以及获取查询结果行数的方法_MySQL:判断查询结果是否为空 在JDBC中没有方法hasNext去判断是否有下一条数据,但是我们可以使用next方法来代替。 看next方法的官方解释: boolean next() throws Moves the cursor forward one row from its current posit
推荐度:
导读MySQL的JDBC判断查询结果是否为空以及获取查询结果行数的方法_MySQL:判断查询结果是否为空 在JDBC中没有方法hasNext去判断是否有下一条数据,但是我们可以使用next方法来代替。 看next方法的官方解释: boolean next() throws Moves the cursor forward one row from its current posit

判断查询结果是否为空

在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();
    // 其他代码不变
  • 声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文档

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

    MySQL的JDBC判断查询结果是否为空以及获取查询结果行数的方法_MySQL:判断查询结果是否为空 在JDBC中没有方法hasNext去判断是否有下一条数据,但是我们可以使用next方法来代替。 看next方法的官方解释: boolean next() throws Moves the cursor forward one row from its current posit
    推荐度:
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top