我有一个
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方法.
谢谢!