简体   繁体   English

如何在Java中检查JDBC表中的某些列是否为空

[英]How to check if some column in JDBC table is null in Java

I need to check if a field in my table is empty (null) and if so, do a specific task; 我需要检查表中的字段是否为空(空),如果是,则执行特定任务; if not, do something different. 如果没有,做一些不同的事情。 I have tried rs.wasNull() and got the error "wasNull() called with no data retrieved". 我尝试了rs.wasNull()并收到错误“ wasNull(),未检索到任何数据”。 Are there any other methods to do this? 还有其他方法吗?

在调用rs.wasNull()之前,您首先需要从rs获取值,例如使用rs.getInt("col1") rs.wasNull()

Another method? 另一种方法? rs.wasNull() is the best way. rs.wasNull()是最好的方法。 But if you insist... Depending on the implementation of the JDBC driver (varies according to the vendor database), you can do the following: 但是,如果您坚持...根据JDBC驱动程序的实现(根据供应商数据库的不同而不同),可以执行以下操作:

Object decimalDigits = rs.getObject("DECIMAL_DIGITS");
if (decimalDigits == null) {
    // do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM