JDBC.SQLServerException:结果集没有当前行

 手机用户2602937685 发布于 2023-02-06 19:15

因此,我创建的解决方案引发了此异常:jdbc.SQLServerException: The result set has no current row在以下代码中标记的行上。

public String get64BitEncodedImageBySiteID(int siteID){
    try {           
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection(url, userName, password);

        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery( "SELECT SitePicture FROM SiteTable WHERE SiteID ="+siteID );

        rs.next();
        // The above line has since been moved to the if statement below where you can see it commented out,
        // which prevents the exception from occuring but still doesn't fix the fact that the row is not being found. 

        if(/*rs.next() &&*/ rs.getBytes("SitePicture")!=null){ // EXCEPTION THROWN HERE!
            byte ba[] = rs.getBytes("SitePicture");            
            return new sun.misc.BASE64Encoder().encodeBuffer(ba);
        }
        else {return null;}
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

在抛出异常的情况下,上述方法是从直接从同一表中提取的Entity对象中获取真实的siteID(22379)。System.out.println(siteID);在此方法中使用时,它声明该数字仍然正确,即仍然为22379。我已经通过在SQL Server中运行相同的语句直接与SQL Server进行了检查,因此我知道表中存在该行,但是对于某些行找不到它的原因。下图。

所以问题是,rs即使我知道该行也没有找到该行。有人对您有帮助吗?

澄清:为了清楚起见,我知道ResultsSet不包含任何行,这就是为什么我得到异常的原因。我也知道将rs.next()放入if语句将防止异常(如注释中所述)。令我感到困惑的是,即使确实存在ID被可解析地解析的行,ResultsSet也不包含任何行,因为我已经直接通过SQL Server检查了它。

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有