JDBC.SQLServerException: The result set has no current row

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.

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

  • 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

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


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

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.

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.

EDIT 2:

Using a PreparedStatement might solve your problem.

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