[英]Handling the null value from a resultset
我目前返回了一个结果集,并且在其中一列中,字符串值可能为空(我的意思是根本没有值)。 我有一个条件来实现如下
rs = st.executeQuery(selectSQL);
output = rs.getString("column");
由于该列在数据库中可能为空,因此当该rs.getString()
空时, 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;
现在,如果列值之一不包含任何值,即空值,我希望output[i]
定义为 N/A。 此问题源于数据库中允许column
字段为 NULL 的事实。 很抱歉告诉你它是一个 NPE,而实际上它是一个SQLException
。
由于该列在数据库中可能为空,因此 rs.getString() 将抛出 NullPointerException()
不。
如果列存在于所选结果集中(SELECT 查询列), rs.getString
将不会抛出NullPointer
对于特定记录,如果 db 中 'comumn 的值为 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
*/
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= "";
}
getString() 方法的描述如下:
the column value; if the value is SQL NULL, the value returned is null
这意味着您的问题不是 String 值是 null,而是其他一些对象,也许是您的 ResultSet 或者您关闭了连接或类似的东西。 提供堆栈跟踪,这会有所帮助。
我能够做到这一点:
String a;
if(rs.getString("column") != null)
{
a = "Hello world!";
}
else
{
a = "Bye world!";
}
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
。
代码应该如下所示
String selectSQL = "SELECT IFNULL(tbl.column, \"\") AS column FROM MySQL_table AS tbl";
Statement st = ...;
Result set rs = st.executeQuery(selectSQL);
要在数据库中的字段为空时处理验证,您可以添加以下条件。
String name = (oRs.getString ("name_column"))! = Null? oRs.getString ("name_column"): "";
有了这个,您可以验证字段何时为空并且不标记异常。
我遇到了同样的问题。 但我相信,在 sql 中处理 null 不是一个好的选择。 这些事情应该在java程序中处理以获得更好的性能。 其次, rs.getString("column") != NULL 也不是一个好的选择,因为您正在比较字符串的引用而不是值。 在检查 null 或 isEmpty() 方法时最好使用 .equals() 方法。 同样,您可以使用空检查,这很好。
你可以简单地使用-
rs = st.executeQuery(selectSQL);
output = rs.getString("column");
if(!output.isEmpty()) {
//
}
在 MySQL 中面临的问题是 -
output!=null
output!=""
但是 output.isEmpty() 为 rs.getString() 工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.