处理结果集中的空值

发布于 2024-11-06 15:06:54 字数 936 浏览 0 评论 0原文

我当前返回了一个结果集,并且在其中一列中,字符串值可能为空(我的意思是根本没有值)。我有一个要实现的条件,如下所示

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

若水微香 2024-11-13 15:06:54

由于该列可能为空
数据库,rs.getString() 将
抛出 NullPointerException()

否。

如果该列存在于所选结果集中(SELECT 查询列),则 rs.getString 不会抛出 NullPointer
对于特定记录,如果数据库中“comumn”的值为空,则必须执行类似的操作 -

String myValue = rs.getString("myColumn");
if (rs.wasNull()) {
    myValue = ""; // set it to empty string as you desire.
}

您可能需要参考 wasNull() 文档 -

From java.sql.ResultSet
boolean wasNull() throws SQLException;

* Reports whether
* the last column read had a value of SQL <code>NULL</code>.
* Note that you must first call one of the getter methods
* on a column to try to read its value and then call
* the method <code>wasNull</code> to see if the value read was
* SQL <code>NULL</code>.
*
* @return <code>true</code> if the last column value read was SQL
*         <code>NULL</code> and <code>false</code> otherwise
* @exception SQLException if a database access error occurs or this method is 
*            called on a closed result set
*/

Since the column may be null in the
database, the rs.getString() will
throw a NullPointerException()

No.

rs.getString will not throw NullPointer 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 -

String myValue = rs.getString("myColumn");
if (rs.wasNull()) {
    myValue = ""; // set it to empty string as you desire.
}

You may want to refer to wasNull() documentation -

From java.sql.ResultSet
boolean wasNull() throws SQLException;

* Reports whether
* the last column read had a value of SQL <code>NULL</code>.
* Note that you must first call one of the getter methods
* on a column to try to read its value and then call
* the method <code>wasNull</code> to see if the value read was
* SQL <code>NULL</code>.
*
* @return <code>true</code> if the last column value read was SQL
*         <code>NULL</code> and <code>false</code> otherwise
* @exception SQLException if a database access error occurs or this method is 
*            called on a closed result set
*/
凉栀 2024-11-13 15:06:54
output = rs.getString("column");// if data is null `output` would be null, so there is no chance of NPE unless `rs` is `null`

if(output == null){// if you fetched null value then initialize output with blank string
  output= "";
}
output = rs.getString("column");// if data is null `output` would be null, so there is no chance of NPE unless `rs` is `null`

if(output == null){// if you fetched null value then initialize output with blank string
  output= "";
}
梦回梦里 2024-11-13 15:06:54

getString() 方法的描述如下:

 the column value; if the value is SQL NULL, the value returned is null

这意味着您的问题不是 String 值为 null,而是其他一些值
对象是,也许是你的 ResultSet 或者你关闭了连接或者其他什么
像这样。提供堆栈跟踪,这会有所帮助。

The description of the getString() method says the following:

 the column value; if the value is SQL NULL, the value returned is null

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.

寄居人 2024-11-13 15:06:54

我能够做到这一点:

String a;
if(rs.getString("column") != null)
{
    a = "Hello world!";
}
else
{
    a = "Bye world!";
}

I was able to do this:

String a;
if(rs.getString("column") != null)
{
    a = "Hello world!";
}
else
{
    a = "Bye world!";
}
却一份温柔 2024-11-13 15:06:54

String 为 null 的可能性很大,但是当您在表中看到值时,ResultSet 却打印出 null,这可能意味着在使用 ResultSet 的值之前连接已关闭。

Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:My_db.db");
String sql = ("select * from cust where cust_id='" + cus + "'");
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
con.close();
System.out.println(rs.getString(1));

即使有值也会打印 null

Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:My_db.db");
String sql = ("select * from cust where cust_id='" + cus + "'");
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
System.out.println(rs.getString(1));
con.close();

如果表中有值,则不会打印 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.

Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:My_db.db");
String sql = ("select * from cust where cust_id='" + cus + "'");
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
con.close();
System.out.println(rs.getString(1));

Would print null even if there are values.

Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:My_db.db");
String sql = ("select * from cust where cust_id='" + cus + "'");
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
System.out.println(rs.getString(1));
con.close();

Wouldn't print null if there are values in the table.

何时共饮酒 2024-11-13 15:06:54

代码应该如下所示

String selectSQL = "SELECT IFNULL(tbl.column, \"\") AS column FROM MySQL_table AS tbl";
Statement st = ...;
Result set rs = st.executeQuery(selectSQL);

The code should be like given below

String selectSQL = "SELECT IFNULL(tbl.column, \"\") AS column FROM MySQL_table AS tbl";
Statement st = ...;
Result set rs = st.executeQuery(selectSQL);
悲喜皆因你 2024-11-13 15:06:54

要在数据库中的字段为空时处理验证,您可以添加以下条件。

String name = (oRs.getString ("name_column"))! = Null? oRs.getString ("name_column"): "";

有了这个,您可以验证字段何时为空并且不标记异常。

To treat validation when a field is null in the database, you could add the following condition.

String name = (oRs.getString ("name_column"))! = Null? oRs.getString ("name_column"): "";

with this you can validate when a field is null and do not mark an exception.

绮筵 2024-11-13 15:06:54

我遇到了同样的问题。但我相信,在 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.

娇俏 2024-11-13 15:06:54

您可以简单地使用 -

rs = st.executeQuery(selectSQL);
output = rs.getString("column");
if(!output.isEmpty()) {
 //
}

在 MySQL 遇到问题时 -

output!=null
output!=""

但是 output.isEmpty() 适用于 rs.getString()。

You can simply use-

rs = st.executeQuery(selectSQL);
output = rs.getString("column");
if(!output.isEmpty()) {
 //
}

In MySQL faced issue with-

output!=null
output!=""

But output.isEmpty() worked for rs.getString().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文