今天java小生贴出 java调用oracle function 的方法
List<WhiteBlackListInfo> whiteList = new ArrayList<WhiteBlackListInfo>();
List<List<String>> strList = new ArrayList<List<String>>();
Integer dataCount = 0;
Session session = null;
Connection con = null;
CallableStatement cstmt = null;
Transaction tr = null;
try {
session = getHibernateTemplate().getSessionFactory().openSession();
tr = session.beginTransaction();
con = session.connection();
cstmt = con.prepareCall("{? =call GETWHITELISTCOUNTBYSP(?,?,?,?,?,?,?)}");
cstmt.setString(2, startTime);
cstmt.setString(3, endTime);
cstmt.setInt(4, timeType);
cstmt.setInt(5, spID);
cstmt.setInt(6, pageSize);
cstmt.setInt(7, currentPage);
cstmt.registerOutParameter(1, Types.ARRAY, "WHITE_LIST_TYPE_ARRAY");//type
cstmt.registerOutParameter(8, java.sql.Types.INTEGER);
cstmt.executeUpdate();
dataCount = cstmt.getInt(8);
oracle.sql.ARRAY arry = (ARRAY) cstmt.getArray(1);
Array array = (Array) cstmt.getArray(1);
if (arry != null && arry.length() > 0) {
Object[] items = (Object[]) array.getArray();
Object obj;
for (int i = 0; i < items.length; i++) {
obj = items[i];
Struct a = (Struct) obj;
WhiteBlackListInfo wbInfo = new WhiteBlackListInfo();
wbInfo.setSpID(Integer.parseInt(a.getAttributes()[0]
.toString()));
wbInfo.setSpName(String.valueOf(a.getAttributes()[1]));
wbInfo.setSpCode(String.valueOf(a.getAttributes()[2]));
wbInfo.setSpTypeid(Integer.parseInt(a.getAttributes()[3]
.toString()));
wbInfo.setSpStatusid(Integer.parseInt(a.getAttributes()[4]
.toString()));
wbInfo.setWhiteListCount(Integer
.parseInt(a.getAttributes()[5].toString()));
whiteList.add(wbInfo);
}
}
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
log.error(e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
tr.rollback();
log.error(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
log.error("error", e);
} finally {
;
}