因此,我创建的解决方案引发了此异常:jdbc.SQLServerException:结果集在下面的代码中标记的行上没有当前行.
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服务器,所以我知道该行存在于表中,但由于某种原因它没找到.图片如下.
所以问题是,即使我知道它在那里,结果集rs也找不到该行.有没有人有任何有用的见解?
澄清:为了清楚,我知道ResultsSet不包含任何行,这就是我得到异常的原因.我也知道将rs.next()放入if语句将防止异常(如注释中所述).让我感到困惑的是,结果集不包含任何行,即使有一个ID被解析为它的行可验证地存在,因为我已经直接用SQL服务器检查了它.