rs返回值的问题:getInt方法返回的是数值“0”的解决办法
2017-03-20 18:56
615 查看
问题描述及原因:
rs.getInt方法,若数据库中记录的数值为null,getInt返回的是数值“0”,而不是null!
等均为:“若数据库中记录的数值为null,返回数值“0”,而不是null”!
因此,
执行后,category的isLeaf属性不可能为NULL
如想让category的isLeaf属性有NULL值,就需要其它处理了
jdk中的相关解释:
int java.sql.ResultSet.getInt(String columnLabel) throws SQLException
Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.
Parameters:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is 0
Throws:
SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
解决方案:
我个人是加上判断,代码如下:
通过获取对象来首先判断其是否为空,然后再用对应的getXXX方法得到具体的值。
rs.getInt方法,若数据库中记录的数值为null,getInt返回的是数值“0”,而不是null!
long java.sql.ResultSet.getLong(String columnLabel) int java.sql.ResultSet.getInt(String columnLabel)
等均为:“若数据库中记录的数值为null,返回数值“0”,而不是null”!
因此,
“category.setIsLeaf(rs.getInt("IS_LEAF"));”
执行后,category的isLeaf属性不可能为NULL
如想让category的isLeaf属性有NULL值,就需要其它处理了
jdk中的相关解释:
int java.sql.ResultSet.getInt(String columnLabel) throws SQLException
Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.
Parameters:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is 0
Throws:
SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
解决方案:
我个人是加上判断,代码如下:
ResultSet rs = statement.executeQuery("select Id from table"); Integer id = 0; if (rs.getObject("Id") != null) { id = rs.getInt("Id"); } else { id = null; }
通过获取对象来首先判断其是否为空,然后再用对应的getXXX方法得到具体的值。
相关文章推荐
- rs返回值的问题:JDBC ResultSet#getInt方法, 若数据库中记录的数值为null,getInt返回的是数值“0”,而不是null!
- 解决 HttpURLConnection类中getContentLength()方法返回-1问题
- VC8下多线程环境中AfxGetMainWnd()返回空指针问题及解决方法
- 解决关于request.getremoteaddr()方法返回的内容是ipv6的问题
- jQuery.get、jQuery.getJSON、jQuery.post无法返回JSON问题的解决方法
- chrome 中调用 getSVGDocument() 方法返回null 解决办法
- Java调用Document.getElementById方法返回null的解决办法
- jquery 中的post和get方法同步问题 解决办法
- HibernateDaoSupport的getHibernateTemplate.find()方法带参查询返回结果是nul问题解决
- 安装IE8在控制面板里面删除之后进不去桌面,提示找不到-IESetting.dll 解决办法(解决IE8卸载不了的问题,返回IE7 ,返回IE6),从IE8回到IE7的方法.
- Java调用Document.getElementById方法返回null的解决办法
- java 中JFinal getModel方法和数据库使用出现问题解决办法
- jQuery.get、jQuery.getJSON、jQuery.post无法返回JSON问题的解决方法
- Java调用Document.getElementById方法返回null的解决办法
- java 中JFinal getModel方法和数据库使用出现问题解决办法
- rs返回值的问题:JDBC ResultSet#getInt方法
- phpExcel getActiveSheet方法返回null解决办法
- VC8下多线程环境中AfxGetMainWnd()返回空指针问题及解决方法
- VC2008下多线程环境中AfxGetMainWnd()返回空指针问题及解决方法
- 使用dwr同步调用导致无法获取js方法的返回结果问题的解决办法