在JAVA中处理结果集中的null值

时间:2011-05-13 11:43:48

标签: java

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

    rs = st.executeQuery(selectSQL);

    output = rs.getString("column");

由于数据库中的列可能为null,因此当为null时, rs.getString()将抛出 NullPointerException() 。如果 column 为null,我希望输出为空字符串,如 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.所有这些问题都源于数据库中允许字段为NULL的事实。对不起家伙告诉你它是NPE而实际上是它的SQLException ...(noob here)。

9 个答案:

答案 0 :(得分:54)

  

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

没有

如果所选结果集中存在列,则

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

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

答案 1 :(得分:16)

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= "";
}

答案 2 :(得分:6)

getString()方法的描述如下:

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

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

答案 3 :(得分:2)

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

答案 4 :(得分:1)

我能够做到这一点:

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

答案 5 :(得分:0)

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

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

使用此方法,您可以验证字段何时为空并且不标记异常。

答案 6 :(得分:0)

代码应如下所示

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

答案 7 :(得分:0)

我遇到了同样的问题。但是我相信,在sql中处理null不是一个好的选择。此类事情应在Java程序中处理,以获得更好的性能。 其次,rs.getString(“ column”)!= NULL也不是一个好选择,因为您正在比较字符串的引用非值。最好在检查null或isEmpty()方法时使用.equals()方法。同样,您可以使用null检查,这很好。

答案 8 :(得分:-1)

您可以简单地使用-

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

在 MySQL 中面临的问题是-

output!=null
output!=""

但是 output.isEmpty() 对 rs.getString() 有效。

相关问题