JDBC.SQLServerException:结果集没有当前行 [英] JDBC.SQLServerException: The result set has no current row

查看:625
本文介绍了JDBC.SQLServerException:结果集没有当前行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, a solution I created threw this exception: jdbc.SQLServerException: The result set has no current row on the line marked in the below code.

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进行了核对,因此我知道表中存在该行,但由于某种原因未找到它.下图.

The method above, in the instance the exception was thrown, is taking a genuine siteID (22379) from an Entity object pulled directly from the same table. When using System.out.println(siteID); during this method, it declared that number to still be correct, ie still 22379. I've checked directly with the SQL server by running an identical statement in SQL Server, so I know the row exists in the table, but for some reason it is not being found. Image below.

所以问题是,即使我知道它在那里,ResultsSet rs也没有找到该行.有人对您有帮助吗?

So the problem is, the ResultsSet rs is not finding the row even though I know that it's there. Does anyone have any helpful insights?

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

推荐答案

事实证明这是一个本地错误,但是无论如何我都会发布解决方案,因为这种情况具有一定的教育意义.

This turned out to be a local mistake, but I'll post the solution anyway because this situation has some educational value.

从@Ralph对此答案的评论中了解到/a>,消除不可能"是解决此类问题的好方法.

As I've learned from @Ralph's comment to this answer, eliminating "the impossible" is a good way for such problems.

在避免了siteID错误(通过对其进行硬编码)的风险之后,我们遇到以下情况:

After avoiding the risk of siteID being wrong (by hardcoding it), we have a following situation:

  • 相同的精确查询在一个环境中有效,但在另一个环境中则无效,仅适用于一个特定的SiteID 2184
  • ResultSet只是不起作用来实现这个特定值(我声明,这是不可能的,因为我总是会承担错误,这是不可能的)是在 my 代码中,而不是在语言库中)
  • 如果是这样,数据库必须有所不同
  • the same exact query worked in one environment, but not the other, for only one particular SiteID, 2184
  • it's impossible that ResultSet just doesn't work for this particular value (I claim it is, because I always assume errors are in my code, not in language libraries)
  • if so, the databases must differ

这篇关于JDBC.SQLServerException:结果集没有当前行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆