简体   繁体   English

是什么导致Java程序不同地对待两个ResultSet.getString()方法?

[英]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引用查询中的同一列,而rsResultSet ,为什么会这样

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文档,使用columnIndexcolumnLabel都返回指定列的值。

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? 谁能告诉我为什么会这样吗?

Extra background info 额外的背景信息

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.

相关问题 如何在Java中将Resultset.getString()与String进行比较? - How to compare Resultset.getString() to String in Java? resultSet.getString(“ TABLE_CAT”)-什么是TABLE_CAT? - resultSet.getString(“TABLE_CAT”) - what is TABLE_CAT? resultSet.getString() 为空值返回什么 - What does resultSet.getString() return for null value 如果我通过传递blob使用resultet.getString()会发生什么? - What could happen if I use resultset.getString() by passing a blob? Java ResultSet.getString()的Date字段显示00:00:00.0 - Java ResultSet.getString() for Date field displaying 00:00:00.0 需要更改ResultSet.getString(Value) - 有条件 - Need to change ResultSet.getString(Value)-— conditionally ResultSet.getString(1)引发java.sql.SQLException:当前光标位置处的无效操作 - ResultSet.getString(1) throws java.sql.SQLException: Invalid operation at current cursor position Oracle ResultSet.getString(“ colname”)返回字符串“ null”而不是java null - Oracle ResultSet.getString(“colname”) returning the string “null” instead of java null JDBC ResultSet.getString ResultSet异常的开始/结束之前/之后 - JDBC ResultSet.getString Before/After Start/End of ResultSet Exception 使用ResultSet.getString()或ResultSet.getTimestamp()以获得时间戳更好吗? - Is it better to use ResultSet.getString() or ResultSet.getTimestamp() to get a timestamp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM