2017-03-03 24 views
1

我正在研究SpringJdbc代碼,注意到的是,當彈簧想要提取輸出參數的結果,它直接使用類型不安全方法getObject(int index)提取輸出參數VS結果Spring的JdbcTemplate設置

Object out = cs.getObject(sqlColIndex);

但是從結果集提取結果

,他們wrote more code嘗試找出確切類型:

public static Object getResultSetValue(ResultSet rs, int index, Class<?> requiredType) throws SQLException { 
    if (requiredType == null) { 
     return getResultSetValue(rs, index); 
    } 

    Object value; 

    // Explicitly extract typed value, as far as possible. 
    if (String.class == requiredType) { 
     return rs.getString(index); 
    } 
    else if (boolean.class == requiredType || Boolean.class == requiredType) { 
     value = rs.getBoolean(index); 
    } 
    else if (byte.class == requiredType || Byte.class == requiredType) { 
...... 
..... 

這背後的原因是什麼?

回答

0

經過搜索,我意識到這一點;

可能是因爲缺少Metadata對象提供了有關參數名稱的信息。

由於傳遞要註冊到存儲過程的輸出參數名稱是可選的(您可以通過索引註冊輸出參數),並且可調用語句返回的ParameterMetaData對象不會返回輸出參數的名稱。

春天;但是請填寫參數名稱的映射(它會要求用戶在使用其自己的API創建輸出參數時設置)以及參數值。

相關問題