[英]What is causing Java program to treat the two ResultSet.getString() methods differently?
If "FirstName" and index 1 refer to the same column in a query, and rs
is a ResultSet
, why would 如果“ FirstName”和索引1引用查询中的同一列,而
rs
是ResultSet
,为什么会这样
if(rs.getString("FirstName")!=null) fullName = rs.getString("FirstName");
and 和
if(rs.getString(1)!=null) fullName = rs.getString(1);
behave differently? 表现不同?
According to the Java docs, using either the columnIndex
or the columnLabel
both return the value of the designated column. 根据Java文档,使用
columnIndex
或columnLabel
都返回指定列的值。
I'm running into a weird problem when I use the columnIndex
instead of the columnLabel
, for some reason, after the jsp has been running for a few hours/days then if(rs.getString(1)!=null)
evaluates to true even when the String is null
. 当我使用
columnIndex
而不是columnLabel
,由于某种原因,在jsp运行了几个小时/天之后,我遇到了一个奇怪的问题,那就是if(rs.getString(1)!=null)
计算为true即使String为null
。 When I use the columnLabel
instead, the problem doesn't happen. 当我改用
columnLabel
,不会发生此问题。 Can anyone tell me why this might be happening? 谁能告诉我为什么会这样吗?
The program runs in Tomcat. 该程序在Tomcat中运行。 We updated Tomcat, the JVM, and our database driver on the same day, and this started happening.
我们在同一天更新了Tomcat,JVM和数据库驱动程序,并且这种情况开始发生。 If we reload the app in Tomcat, it starts working correctly, at least for a few hours.
如果我们在Tomcat中重新加载该应用程序,则它至少在几个小时内就可以正常运行。 We switched back to the old database driver and the problem continues, so we have ruled out the database driver.
我们切换回旧的数据库驱动程序,问题仍然存在,因此我们排除了数据库驱动程序。
rs.getString(1)
返回在执行您的SQL查询的第一列中的数据(由preparedStatement
),它不需要的FirstName
,则需要交叉检查,在您的SQL查询preparedStatement
如下:
SELECT FirstName from X;//you might add where conditions
From your comment: 根据您的评论:
it just starts returning the String "null" instead of the actual null value.
它只是开始返回字符串“ null”,而不是实际的null值。 So the fullName becomes the String "null".
因此,fullName变为字符串“ null”。
In other words the problem is different from what is stated in your question. 换句话说,问题与您的问题不同。
null
and "null"
aren't the same thing, so there is no reason for rs.getString(1) == null
to return true
if the actual value is "null"
. null
和"null"
不是同一件事,因此,如果实际值为"null"
,则没有理由让rs.getString(1) == null
返回true
。
I would say you probably have "null"
in your database somewhere. 我想说您的数据库中某处可能有
"null"
。 Check. 校验。 Or else you are using
String.valueOf(null)
somewhere in your code, possibly indirectly, eg via println(null)
, which calls it, so the output is "null"
. 否则,您可能在代码的某个地方使用
String.valueOf(null)
,可能是间接地使用它,例如通过调用它的println(null)
,因此输出为"null"
。
We have solved it. 我们已经解决了。 We decided to try a different SQL driver (we were using a Microsoft driver before, we switched to JTDS) and all problems have gone away.
我们决定尝试使用其他SQL驱动程序(之前使用的是Microsoft驱动程序,后来切换到JTDS),所有问题都消失了。
So I'm guessing the Microsoft driver was returning the string "null" when it should have been returning null
. 所以我猜想微软驱动程序应该在返回
null
时返回字符串“ null”。
So to answer the question, the ResultSet.getString()
method was probably behaving correctly all along, but the SQL driver was returning bad data. 因此,为了回答这个问题,
ResultSet.getString()
方法可能一直表现正确,但是SQL驱动程序返回了错误的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.