JAVA的RESULTSET中如何判断一条记录的一个字段的值为NULL?类似.NET的
rs("aaa")==DBNull.Value
rs("aaa")==DBNull.Value
解决方案 »
- 有这样一个需求,邦忙啊!
- 我导入一个很大的项目,不知道为什么总出错,在很多JAVA文件中居然:The import java.awt cannot be resolved
- 初学提问,关于SOCKET和GUI结合
- 问个基础问题
- 笨笨的程序求救....!!
- Jbuilder怎样生成.exe文件?
- 急,基础问题,求解
- 窗口坐标转屏幕坐标的问题?
- 请教:怎么在Jtable中实现校验?请高手们指点,谢谢,急~~~(在线跪求答案,55555~~)
- 如何连接别人机器上的MySQL?
- 怎样将byte数组中的元素当作16进制的数字看待?
- 我在Eclipse 新建立了一个可视类,继承自JFrame,但不能出现可视化界面
java的变量有primitive(原始)类型和非原始类型。
原始类型包括boolean,byte,char,double,float,int,long,short.
这些如果ResultSet里的COLUMN为NULL的话,赋予初始值,呵呵。这个具体初始值是什么我就不1说 了
其他为非原始类型,例如:String, time, date, timestamp.....
这个如果为NULL的话,取得的value也是NULL。
可以用rs.getXXX("columnName")==null判断。
你先rs.getXXX("columnName")==null判断。如果不是的话再用
rs.warNull()来判断,为什么不直接用这个,请看API。
wasNull
public boolean wasNull()
throws SQLExceptionReports whether the last column read had a value of SQL NULL. 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 wasNull to see if the value read was SQL NULL. Returns:
true if the last column value read was SQL NULL and false otherwise
Throws:
SQLException - if a database access error occurs
rs = stmt.executeQuery("select id,type_id,prod_name from product where id=1");
//此次假设type_id列为Null值
System.out.println("id=" + rs.getInt("id");
System.out.println("type_id=" + rs.getInt("type_id");
if (rs.wasNull()) {
System.out.println("type_id was null!";
}
如果type_id列为Null,用rs.getInt读出来的值为0,但是rs.wasNull()==true
如果type_id列为0,用rs.getInt读出来的值也为0.但是rs.wasNull()==false