在JAVA中处理结果集中的null值 [英] Handling the null value from a resultset in JAVA

查看:1071
本文介绍了在JAVA中处理结果集中的null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I currently have a resultset 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");

由于数据库中的列可能为null,因此 rs.getString()为null时,em>将抛出 NullPointerException()。如果 column 为null,我希望输出为空字符串,如 output =; 我无法检查 if(rs.getString(column) != null 。我怎样才能解决这种情况?

Since the column may be null in the database, the rs.getString() will throw a NullPointerException() when column is null. If column is null, I want output to be an empty string like output = ""; I can't check if(rs.getString("column) != null either. How can I tackle this situation?

我的真正问题:

 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,我想要输出[i定义为N / A.所有这个问题都源于数据库中允许字段为NULL的事实。抱歉,我告诉你我事实上它是一个NPE,而它的SQLException ...(noob here)。

Now if one of the column values contains no value i.e. null i want output[i] defined as N/A. All this problem stems from the fact that the column field is NULL allowed in the database. And sorry guys for telling you that its a NPE while in fact its SQLException...(noob here).

推荐答案


由于
数据库中的列可能为null,因此rs.getString ()将
抛出NullPointerException()

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

否。

rs.getString 如果所选结果集中存在列(SELECT查询列),则不会抛出 NullPointer
对于特定记录,如果'comumn的值在db中为null,则必须执行以下操作 -

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.
}

您可能想要参考 wasNull() documentation -

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
*/

这篇关于在JAVA中处理结果集中的null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆