1、首先你得在數據庫中寫一個存儲過程
例如:
P_DZK_NETRES(V_DZID IN NUMBER,V_DZTYPE IN NUMBER ,V_ZYBM IN VARCHAR2,V_DZ IN VARCHAR2,V_RENUM OUT NUMBER)
調用代碼如下:
/**
* 描述: 執行存過和函數,paramValues與paramClass的長度必須是一致的,對java的幾種基本數據類型通用
* @param func 存過調用字符串:func(?,?,?,?)
* @param paramValues 要傳的參數值,要保證類型正確,以字符串數組的形式
* @param paramClass 要傳的參數值的類型的一個示例,根據這個判斷這個參數是什麼類型的,例如:Object[] paramClass = {1L,1,"1","1"};
* @param returnIndex 輸出參的位置
* @return 存過輸出參的值
* @throws JCtnException
*/
public String executeCallable(String func, String[] paramValues,
Object[] paramClass, int returnIndex) throws JCtnException{
if(paramValues.length != paramClass.length) {
throw new JCtnException("存過參數值與參數類型的個數不一致!");
}
String res = "";
CallableStatement cstmt = null;
TransactionUtil transaction = tranLocal.get();
Connection conn = null;
try {
String sql = "{call " + func + "}";
if(transaction == null){//未使用事務
conn = poolHashMap.get(this.poolUrl).getConnection();
}else{//使用事務
conn = transaction.getConn();
}
cstmt = conn.prepareCall(sql);
int index = 1;
boolean flag = false;
for (int i = 1; i <= paramValues.length; i++) {
index = i;
if(i == returnIndex) {//第returnIndex個位置為輸出參數,出參為中間的某一個
flag = true;
}
if(flag == true) {
index ++;
}
try {
if(paramClass[i-1] instanceof Integer) {
cstmt.setInt(index, Integer.valueOf(paramValues[i-1]));
} else if(paramClass[i-1] instanceof String) {
cstmt.setString(index, paramValues[i-1]);
} else if(paramClass[i-1] instanceof Boolean) {
cstmt.setBoolean(index, Boolean.valueOf(paramValues[i-1]));
} else if(paramClass[i-1] instanceof Double) {
cstmt.setDouble(index, Double.valueOf(paramValues[i-1]));
} else if(paramClass[i-1] instanceof Float) {
cstmt.setFloat(index, Float.valueOf(paramValues[i-1]));
} else if(paramClass[i-1] instanceof Long) {
cstmt.setLong(index, Long.valueOf(paramValues[i-1]));
} else if(paramClass[i-1] instanceof Byte) {
cstmt.setByte(index, Byte.valueOf(paramValues[i-1]));
}
} catch (Exception e) {
throw new JCtnException(e);
}
}
//設置出參
cstmt.registerOutParameter(returnIndex, Types.VARCHAR);
cstmt.execute();
res = cstmt.getString(returnIndex);
} catch (HibernateException e) {
throw new JCtnException(e);
} catch (SQLException e) {
throw new JCtnException(e);
} finally {
try {
if (cstmt != null) {
cstmt.close();
}
} catch (SQLException e) {
throw new JCtnException(e);
}
}
return res;
}
相關閱讀: Java中用JDBC調用Oracle存儲過程和函數 http://www.linuxidc.com/Linux/2013-09/89922.htm
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2013-09/89923p2.htm