【Java 调用Oracle 存储过程】

用java编写调用带返回结果集存储过程:
    [java]
    /**
    * 调用带返回结果集存储过程
    *
    * @param procName
    * @param param
    * @return
    * @throws SQLException
    * @throws NoFreeConnectionException
    */
    public DataSource execuceProc(String procName, String[] param) throws SQLException, NoFreeConnectionException {
    Connection con = null;
    CallableStatement getResults = null;
    ResultSet rs = null;
    StringBuffer build = new StringBuffer();
    build.append("{ call ")。append(procName);
    build.append("(");
    if (param != null && param.length > 0) {
    for (int i = 0; i < param.length; i++) {
    build.append("'")。append(param[i])。append("',");
    }
    }
    build.append("?) }");
    try {
    con = ConnectDBBean.getConnection(ReadWriteDBPool.readPool);
    getResults = con.prepareCall(build.toString(), ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    getResults.registerOutParameter(1, OracleTypes.CURSOR);
    getResults.execute();
    rs = (ResultSet) getResults.getObject(1);
    this.rsToDataSource(rs);
    } finally {
    try {
    if (rs != null) {
    rs.close();
    }
    if (getResults != null) {
    getResults.close();
    }
    } catch (SQLException e) {
    log.error(e);
    }
    ConnectDBBean.closeConnection(ReadWriteDBPool.readPool, con);
    }
    return new DataSourceType();
    <STRONG>    }</STRONG>
    /**
    * 调用带返回结果集存储过程
    *
    * @param procName
    * @param param
    * @return
    * @throws SQLException
    * @throws NoFreeConnectionException
    */
    public DataSource execuceProc(String procName, String[] param) throws SQLException, NoFreeConnectionException {
    Connection con = null;
    CallableStatement getResults = null;
    ResultSet rs = null;
    StringBuffer build = new StringBuffer();
    build.append("{ call ")。append(procName);
    build.append("(");
    if (param != null && param.length > 0) {
    for (int i = 0; i < param.length; i++) {
    build.append("'")。append(param[i])。append("',");
    }
    }
    build.append("?) }");
    try {
    con = ConnectDBBean.getConnection(ReadWriteDBPool.readPool);
    getResults = con.prepareCall(build.toString(), ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    getResults.registerOutParameter(1, OracleTypes.CURSOR);
    getResults.execute();
    rs = (ResultSet) getResults.getObject(1);
    this.rsToDataSource(rs);
    } finally {
    try {
    if (rs != null) {
    rs.close();
    }
    if (getResults != null) {
    getResults.close();
    }
    } catch (SQLException e) {
    log.error(e);
    }
    ConnectDBBean.closeConnection(ReadWriteDBPool.readPool, con);
    }
    return new DataSourceType();
    }

posted @ 2013-09-17 09:20  豆豆逗逗  阅读(160)  评论(0编辑  收藏  举报