java resultset wasnull_Java Spring – RowMapper ResultSet – 整数/空值

我有一个

Java SE 8 Spring 4.1.6-RELEASE应用程序,我正在实现

org.springframework.jdbc.core.RowMapper接口,我对其

T mapRow(ResultSet rs, int rowNum)方法中传递的

java.sql.ResultSet接口有一些疑问.

当我检查ResultSet类时,我看到一堆方法来获取列值:

╔══════════════╦═════════════════════╦════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗

║ Return Type ║ Method ║ Return (javadoc, se 8) ║

╠══════════════╬═════════════════════╬════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣

║ String ║ getString ║ the column value; if the value is SQL NULL, the value returned is null ║

║ boolean ║ getBoolean ║ the column value; if the value is SQL NULL, the value returned is false ║

║ byte ║ getByte ║ the column value; if the value is SQL NULL, the value returned is 0 ║

║ short ║ getShort ║ the column value; if the value is SQL NULL, the value returned is 0 ║

║ int ║ getInt ║ the column value; if the value is SQL NULL, the value returned is 0 ║

║ long ║ getLong ║ the column value; if the value is SQL NULL, the value returned is 0 ║

║ float ║ getFloat ║ the column value; if the value is SQL NULL, the value returned is 0 ║

║ double ║ getDouble ║ the column value; if the value is SQL NULL, the value returned is 0 ║

║ BigDecimal ║ getBigDecimal ║ the column value; if the value is SQL NULL, the value returned is null ║

║ byte[] ║ getBytes ║ the column value; if the value is SQL NULL, the value returned is null ║

║ Date ║ getDate ║ the column value; if the value is SQL NULL, the value returned is null ║

║ Time ║ getTime ║ the column value; if the value is SQL NULL, the value returned is null ║

║ Timestamp ║ getTimestamp ║ the column value; if the value is SQL NULL, the value returned is null ║

║ InputStream ║ getAsciiStream ║ a Java input stream that delivers the database column value as a stream of one-byte ASCII characters; if the value is SQL NULL, the value returned is null ║

║ Reader ║ getCharacterStream ║ a java.io.Reader object that contains the column value; if the value is SQL NULL, the value returned is null in the Java programming language ║

║ InputStream ║ getBinaryStream ║ a Java input stream that delivers the database column value as a stream of uninterpreted bytes; if the value is SQL NULL, the value returned is null ║

║ T ║ getObject ║ an instance of type holding the column value ║

╚══════════════╩═════════════════════╩════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

通常的期望/做法是:

rs.getObject("COLUMN_NAME", Boolean.class);

rs.getObject("COLUMN_NAME", Byte.class);

rs.getObject("COLUMN_NAME", Short.class);

rs.getObject("COLUMN_NAME", Integer.class);

rs.getObject("COLUMN_NAME", Long.class);

对于所有原始类型?因为其他所有内容都为SQL NULL实例返回null.

如果是这样,当有类型的Object方法存在时,为不同类型设置所有方法的重点是什么?

另外,每种方法的优缺点是什么?

>使用getInt(String columnLabel):

Integer resultingActionId = rs.getInt("RESULTING_ACTION_ID");

if (rs.wasNull) {

resultingActionId = null

}

>使用getObject(String columnLabel)并强制转换为Integer:

Integer resultingActionId = (Integer) rs.getObject("RESULTING_ACTION_ID");

>使用getObject(String columnLabel,Class type):

Integer resultingActionId = rs.getObject("RESULTING_ACTION_ID", Integer.class);

例如,我注意到org.springframework.jdbc.core.JdbcTemplate曾经有queryForLong,queryForInt等方法,用于从单行查询中获取单个值,并将它们全部替换为有类型的queryForObject方法.

谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值