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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20900819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:08:12  来源:igfitidea点击:

JDBC.SQLServerException: The result set has no current row

javaexceptionjpajdbcresultset

提问by

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

所以,我创建的一个解决方案抛出了这个异常: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;
}

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.

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

enter image description here

在此处输入图片说明

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

所以问题是,rs即使我知道它在那里,结果集也没有找到行。有没有人有任何有用的见解?

Clarification: Just to be clear, I know that the ResultsSet contains no rows and that is why I am getting the exception. I also know that putting the rs.next() into the if statement will prevent the exception (as already stated in the comments). What is puzzling me is that the fact the ResultsSet contains no rows even though a row with the ID being parsed to it verifiably does exists because I have checked it directly with the SQL server.

澄清:澄清一下,我知道 ResultsSet 不包含任何行,这就是我收到异常的原因。我也知道将 rs.next() 放入 if 语句将防止异常(如评论中所述)。令我感到困惑的是,ResultSet 不包含任何行的事实,即使 ID 被解析为它的行确实存在,因为我已经直接使用 SQL 服务器检查了它。

采纳答案by Micha? Rybak

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

结果证明这是一个本地错误,但无论如何我都会发布解决方案,因为这种情况具有一定的教育价值。

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

正如我从@Ralph 对此答案的评论中了解到的,消除“不可能”是解决此类问题的好方法。

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

在避免siteID出错的风险后(通过硬编码),我们有以下情况:

  • the same exact queryworked in one environment, but not the other, for only one particular SiteID, 2184
  • it's impossible that ResultSetjust doesn't workfor this particular value(I claimit is, because I always assume errors are in mycode, not in language libraries)
  • if so, the databases must differ
  • 完全相同的查询在一个环境中工作,而不是其他,只为一个特定的SiteID,2184
  • 这是不可能的ResultSet只是不工作这个特定值(我要求它是,因为我总是认为错误是在的代码,而不是在语言库)
  • 如果是这样,数据库必须不同

回答by NickJ

The most likely explanation is that your ResultSet contains no rows. Have you checked that?

最可能的解释是您的 ResultSet 不包含任何行。你检查过吗?

If that's the case, rs.next()will return false, but you are not checking the return value any more. Put rs.next()back into the if block, it was OK in there.

如果是这种情况,rs.next()将返回 false,但您不再检查返回值。把rs.next()放回如果块,这是确定在那里。

You can make sure by:

您可以通过以下方式确保:

if (rs.next()) {

  if(rs.getBytes("SitePicture")!=null){ 
     byte ba[] = rs.getBytes("SitePicture");            
     return new sun.misc.BASE64Encoder().encodeBuffer(ba);
  }

} else {

   System.out.println("No rows returned");

}

EDIT:

编辑:

what column type is siteID? Your method takes an int, but your SQL wraps it in quotes, as if it were a string.

siteID 是什么列类型?您的方法接受一个 int,但您的 SQL 将它用引号括起来,就好像它是一个字符串一样。

EDIT 2:

编辑2:

Using a PreparedStatement might solve your problem.

使用 PreparedStatement 可能会解决您的问题。

PreparedStatement ps = conn.prepareStatement("SELECT SitePicture FROM SiteTable WHERE SiteID = ?");
ps.setInt(1, siteId);
ResultSet rs = ps.executeQuery();

回答by sathish p

Adding result statements inside while loop helped in my case. while(rs.next) { rs.getString("your column name"); }

在 while 循环中添加结果语句对我有帮助。 while(rs.next) { rs.getString("your column name"); }

回答by pramod_maddu

You may not get the result without checking whether the result has entries. For that use

如果不检查结果是否有条目,您可能无法获得结果。为了那个用途

while(rs.next()){
  rs.getString("column name");
}

and try it. It worked fine for me.

并尝试一下。它对我来说很好。

Thanks

谢谢