因此,我创建的解决方案引发了此异常: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;
}
上面的方法,在抛出异常的实例中,从直接从同一个表中提取的实体对象中获取真实的 siteID (22379)。使用时System.out.println(siteID);
在这个方法中,它声明该数字仍然是正确的,即仍然是22379。我通过在SQL Server中运行相同的语句直接与SQL Server进行检查,所以我知道该行存在于表中,但由于某种原因它没有被发现。下图。
所以问题是,结果集rs
即使我知道它在那里,也没有找到该行。有人有任何有用的见解吗?
澄清:需要明确的是,我知道 ResultsSet 不包含任何行,这就是我收到异常的原因。我还知道将 rs.next() 放入 if 语句中将防止出现异常(如注释中所述)。令我困惑的是,事实上 ResultsSet 不包含任何行,尽管 ID 被解析的行确实存在,因为我直接使用 SQL Server 检查了它。
事实证明这是一个本地错误,但无论如何我都会发布解决方案,因为这种情况具有一定的教育价值。
正如我从 @Ralph 的评论中了解到的这个答案 https://stackoverflow.com/questions/20161896/spring-mvc-pagination-and-request-methods/20162675#20162675消除“不可能”是解决此类问题的好方法。
规避风险后siteID
错误的(通过硬编码),我们有以下情况:
- the 完全相同的查询在一种环境中工作,但不在另一种环境中工作,仅针对一个特定的环境
SiteID
, 2184
- 这是不可能的
ResultSet
just 不起作用 for 这个特定的值 (I claim是的,因为我总是假设错误在my代码,不在语言库中)
- 如果是这样,数据库必须不同
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)