处理结果集中的空值
我当前返回了一个结果集,并且在其中一列中,字符串值可能为空(我的意思是根本没有值)。我有一个要实现的条件,如下所示
rs = st.executeQuery(selectSQL);
output = rs.getString("column");
由于数据库中的列可能为空,因此当列为空时,rs.getString()
将抛出NullPointerException
。如果列为空,我希望输出为空字符串,如 output = "";
。我也无法检查 if(rs.getString("column) != null
。我该如何解决这种情况?
我真正的问题:
try {
rs = st.executeQuery(sql);
int i = 0;
while (rs.next()) {
output[i] = rs.getString(column);
// column field in the database contains multiple results, but sometimes
// may be null
i++;
}
} catch (SQLException e) {
e.printStackTrace();
// other than tracing the exception i want to fill the array too
}
return output;
现在,如果其中一个列值不包含任何值,即null,我希望将 output[i]
定义为 N/A。这个问题源于数据库中允许 column
字段为 NULL 的事实。它是一个 NPE,而实际上它是一个 SQLException
。
I currently have a result set returned, and in one of the columns the string value may be null (I mean no values at all). I have a condition to implement like following
rs = st.executeQuery(selectSQL);
output = rs.getString("column");
Since the column may be null in the database, the rs.getString()
will throw a NullPointerException
when the column is null. If column is null, I want the output to be an empty string like output = "";
. I can't check if(rs.getString("column) != null
either. How can I tackle this situation?
My real problem:
try {
rs = st.executeQuery(sql);
int i = 0;
while (rs.next()) {
output[i] = rs.getString(column);
// column field in the database contains multiple results, but sometimes
// may be null
i++;
}
} catch (SQLException e) {
e.printStackTrace();
// other than tracing the exception i want to fill the array too
}
return output;
Now, if one of the column values contains no value, i.e. null, I want output[i]
defined as N/A. This problem stems from the fact that the column
field is NULL allowed in the database. And sorry for telling you that it's a NPE, while in fact it's a SQLException
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
否。
如果该列存在于所选结果集中(SELECT 查询列),则
rs.getString
不会抛出NullPointer
对于特定记录,如果数据库中“comumn”的值为空,则必须执行类似的操作 -
您可能需要参考
wasNull()
文档 -No.
rs.getString
will not throwNullPointer
if the column is present in the selected result set (SELECT query columns)For a particular record if value for the 'comumn is null in db, you must do something like this -
You may want to refer to
wasNull()
documentation -getString() 方法的描述如下:
这意味着您的问题不是 String 值为 null,而是其他一些值
对象是,也许是你的 ResultSet 或者你关闭了连接或者其他什么
像这样。提供堆栈跟踪,这会有所帮助。
The description of the getString() method says the following:
That means your problem is not that the String value is null, rather some other
object is, perhaps your ResultSet or maybe you closed the connection or something
like this. Provide the stack trace, that would help.
我能够做到这一点:
I was able to do this:
String 为 null 的可能性很大,但是当您在表中看到值时,ResultSet 却打印出 null,这可能意味着在使用 ResultSet 的值之前连接已关闭。
即使有值也会打印
null
。如果表中有值,则不会打印
null
。The String being null is a very good chance, but when you see values in your table, yet a null is printed by the ResultSet, it might mean that the connection was closed before the value of ResultSet was used.
Would print
null
even if there are values.Wouldn't print
null
if there are values in the table.代码应该如下所示
The code should be like given below
要在数据库中的字段为空时处理验证,您可以添加以下条件。
有了这个,您可以验证字段何时为空并且不标记异常。
To treat validation when a field is null in the database, you could add the following condition.
with this you can validate when a field is null and do not mark an exception.
我遇到了同样的问题。但我相信,在 sql 中处理 null 不是一个好的选择。这些事情应该在java程序中处理以获得更好的性能。
其次, rs.getString("column") != NULL 也不是一个好的选择,因为您正在比较字符串的引用而不是值。在检查 null 或 isEmpty() 方法时最好使用 .equals() 方法。同样,有了这个你可以使用空检查,那很好。
I came across with the same issue. But I believe , handling null in the sql is not a good option. such things should be handled in java program for better performance.
secondly , rs.getString("column") != NULL is also not a good option as you are comparing string's reference not value. better to use .equals() method while checking null or isEmpty() method. Again, with this you can use null check, that is fine.
您可以简单地使用 -
在 MySQL 遇到问题时 -
但是 output.isEmpty() 适用于 rs.getString()。
You can simply use-
In MySQL faced issue with-
But output.isEmpty() worked for rs.getString().