壹個函數本質上壹個返回壹個結果的存儲過程,這個例子示範了怎麽調用有in、out和in/out參數的函數***********************************/
CallableStatement cs;
try {
// 調用壹個沒有參數的函數; 函數返回 a VARCHAR// 預處理callable語句
cs = connection.prepareCall("{? = call myfunc}");// 註冊返回值類型
cs.registerOutParameter(1, i);
// Execute and retrieve the returned valuecs.execute();
String retValue = cs.getString(1);
// 調用有壹個in參數的函數; the function returns a VARCHARcs = connection.prepareCall("{? = call myfuncin(?)}");// Register the type of the return value
cs.registerOutParameter(1, Types.VARCHAR);// Set the value for the IN parameter
cs.setString(2, "a string");
// Execute and retrieve the returned valuecs.execute();
retValue = cs.getString(1);
// 調用有壹個out參數的函數; the function returns a VARCHARcs = connection.prepareCall("{? = call myfuncout(?)}");// Register the types of the return value and OUT parametercs.registerOutParameter(1, Types.VARCHAR);cs.registerOutParameter(2, Types.VARCHAR);// Execute and retrieve the returned valuescs.execute();
retValue = cs.getString(1); // return valueString outParam = cs.getString(2); // OUT parameter// 調用有壹個in/out參數的函數; the function returns a VARCHARcs = connection.prepareCall("{? = call myfuncinout(?)}");// Register the types of the return value and OUT parametercs.registerOutParameter(1, Types.VARCHAR);cs.registerOutParameter(2, Types.VARCHAR);// Set the value for the IN/OUT parametercs.setString(2, "a string");
// Execute and retrieve the returned valuescs.execute();
retValue = cs.getString(1); // return valueoutParam = cs.getString(2); // IN/OUT parameter} catch (SQLException e) {
}