服务器之家

服务器之家 > 正文

java调用oracle分页存储过程示例

时间:2019-11-12 14:21     来源/作者:java教程网

1.分页

复制代码代码如下:


package org.zh.basic;

 

/**
 * 页面类
 * 
 * @author keven
 * 
 */
public class PageInfo {

    // 定义
    private String p_tableName; // -表名
    private String p_strWhere; // --查询条件
    private String p_orderColumn; // --排序的列
    private String p_orderStyle; // --排序方式
    private int p_curPage; // --当前页
    private int p_pageSize; // --每页显示记录条数
    private int p_totalRecords; // --总记录数
    private int p_totalPages; // --总页数

    // / <summary>
    // / 定义函数
    // / </summary>
    public PageInfo() {
    }

    public PageInfo(String p_tableName, String p_strWhere,
            String p_orderColumn, String p_orderStyle, int p_curPage,
            int p_pageSize, int p_totalRecords, int p_totalPages) {
        this.p_tableName = p_tableName;
        this.p_strWhere = p_strWhere;
        this.p_orderColumn = p_orderColumn;
        this.p_orderStyle = p_orderStyle;
        this.p_curPage = p_curPage;
        this.p_pageSize = p_pageSize;
        this.p_totalRecords = p_totalRecords;
        this.p_totalPages = p_totalPages;
    }

    public String getP_tableName() {
        return p_tableName;
    }

    public void setP_tableName(String pTableName) {
        p_tableName = pTableName;
    }

    public String getP_strWhere() {
        return p_strWhere;
    }

    public void setP_strWhere(String pStrWhere) {
        p_strWhere = pStrWhere;
    }

    public String getP_orderColumn() {
        return p_orderColumn;
    }

    public void setP_orderColumn(String pOrderColumn) {
        p_orderColumn = pOrderColumn;
    }

    public String getP_orderStyle() {
        return p_orderStyle;
    }

    public void setP_orderStyle(String pOrderStyle) {
        p_orderStyle = pOrderStyle;
    }

    public int getP_curPage() {
        return p_curPage;
    }

    public void setP_curPage(int pCurPage) {
        p_curPage = pCurPage;
    }

    public int getP_pageSize() {
        return p_pageSize;
    }

    public void setP_pageSize(int pPageSize) {
        p_pageSize = pPageSize;
    }

    public int getP_totalRecords() {
        return p_totalRecords;
    }

    public void setP_totalRecords(int pTotalRecords) {
        p_totalRecords = pTotalRecords;
    }

    public int getP_totalPages() {
        return p_totalPages;
    }

    public void setP_totalPages(int pTotalPages) {
        p_totalPages = pTotalPages;
    }

}

 

2 调用

 

复制代码代码如下:


package org.zh.sys.server;

 

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import org.hibernate.Session;
import org.zh.basic.PageInfo;
import org.zh.dao.HibernateSessionFactory;
import oracle.jdbc.driver.OracleTypes;
import oracle.jdbc.OracleCallableStatement;


public class GeneratePage {

    public GeneratePage() {

    }

    public static ArrayList Prc_Page(PageInfo page) {
        ArrayList list = new ArrayList();
        Map mp;
        Session s = null;
        Connection conn = null;
        ResultSet rs = null;
        CallableStatement proc = null;
        try {
            s = HibernateSessionFactory.getSession();
            conn = s.connection();
            proc = conn.prepareCall("{call pages.prc_page(?,?,?,?,?,?,?,?,?)}");
            proc.setString(1, page.getP_tableName());
            proc.setString(2, page.getP_strWhere());
            proc.setString(3, page.getP_orderColumn());
            proc.setString(4, page.getP_orderStyle());
            proc.setInt(5, page.getP_curPage());
            proc.setInt(6, page.getP_pageSize());
            proc.registerOutParameter(7, OracleTypes.NUMBER);
            proc.registerOutParameter(8, OracleTypes.NUMBER);
            proc.registerOutParameter(9, OracleTypes.CURSOR);
            proc.execute();
            // page.setP_totalRecords(proc.getInt("p_totalRecords"));
            // page.setP_totalPages(proc.getInt("p_totalPages"));
            // list = (ArrayList) proc.getObject("v_cur");
            page.setP_totalRecords(proc.getInt(7));
            page.setP_totalPages(proc.getInt(8));
            rs = ((OracleCallableStatement) proc).getCursor(9); // 得到输出结果集参数
            ResultSetMetaData rsmd = rs.getMetaData();
            int numberOfColumns = rsmd.getColumnCount();
            while (rs.next()) {
                mp = new HashMap(numberOfColumns);
                for (int r = 1; r < numberOfColumns; r++) {
                    mp.put(rsmd.getColumnName(r), rs.getObject(r));
                }
                list.add(mp);
            }
            return list;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return list;
        } catch (Exception ex2) {
            ex2.printStackTrace();
            return list;
        } finally {
            try {
                if (proc != null) {
                    proc.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
        }
    }
}

 

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
返回顶部