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

博客作者遇到一个Java程序在尝试从SQL Server数据库中获取特定SiteID的图片时抛出异常。问题在于结果集没有找到匹配的行,尽管直接在SQL Server中执行相同的查询能正确返回结果。经过检查,发现不是siteID错误,也不是结果集的next()方法问题,而是数据库之间的差异导致的问题。解决方案是通过硬编码siteID来排除变量错误,并进一步调查不同环境间数据库的差异。
摘要由CSDN通过智能技术生成

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

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.

NzOyT.png

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

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值