因此,我创建的解决方案引发了此异常: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检查了它。