⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbctemplate.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (rs == null) {
                return ri;
            } else {
                // 取得列名信息
                ResultSetMetaData rm = rs.getMetaData();
                colCount = rm.getColumnCount();
                for (int i = 1; i <= colCount; i++) {
                    //System.out.println(rm.getColumnName(i));
                    mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
                }

                rs.setFetchSize(pageSize);

                int absoluteLocation = pageSize * (curPage - 1) + 1;
                //System.out.println("绝对定位于: " + absoluteLocation);
                if (rs.absolute(absoluteLocation) == false) {
                    return ri;
                }

                result = new Vector();

                ResultWrapper rsw = new ResultWrapper(rs);
                do {
                    Vector row = new Vector();
                    for (int i = 0; i < colCount; i++)
                        row.addElement(rsw.getObject(i + 1));
                    result.addElement(row);
                    rowCount++;
                } while (rsw.next());
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {}
                rs = null;
            }
            if (ps!=null) {
                try {
                    ps.close();
                }
                catch (Exception e) {}
                ps = null;
            }
            if (connection.getAutoCommit()) {
                connection.close();
                connection = null;
            }
        }
        return new ResultIterator(result, mapIndex, total);
    }

    /**
     * 执行查询,取出全部信息置于ResultIterator中
     * @param sql String
     * @return Vector
     */
    public ResultIterator executeQuery(String sql, Object[] objectParams) throws
            SQLException {
        ResultIterator ri = new ResultIterator();

        rowCount = 0;
        colCount = 0;

        ResultSet rs = null;
        Vector result = null;
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            fillPreparedStatement(ps, objectParams);
            rs = connection.executePreQuery();
            if (rs == null) {
                return ri;
            } else {
                // 取得列名信息
                ResultSetMetaData rm = rs.getMetaData();
                colCount = rm.getColumnCount();
                for (int i = 1; i <= colCount; i++) {
                    mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
                }

                result = new Vector();

                ResultWrapper rsw = new ResultWrapper(rs);
                while (rsw.next()) {
                    Vector row = new Vector();
                    for (int i = 0; i < colCount; i++)
                        row.addElement(rsw.getObject(i + 1));
                    result.addElement(row);
                    rowCount++;
                }
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (ps!=null) {
                ps.close();
                ps = null;
            }
            if (connection.getAutoCommit()) {
                connection.close();
                connection = null;
            }
        }
        return new ResultIterator(result, mapIndex);
    }

    /**
     * 分页操作,将ResultSet的信息保存在Vector中,以利用Iterator模式
     * @param sql String sql查询语句
     * @param curPage int 当前页
     * @param pageSize int 页的记录条数
     * @return ResultIterator
     */
    public ResultIterator executeQuery(String sql, int curPage, int pageSize) throws
            SQLException {
        ResultIterator ri = new ResultIterator();

        this.curPage = curPage;
        this.pageSize = pageSize;

        rowCount = 0;
        colCount = 0;

        ResultSet rs = null;
        Vector result = null;
        boolean isException = false;
        try {
            // 取得总记录条数
            String countsql = SQLFilter.getCountSql(sql);
            // logger.debug(countsql);
            rs = connection.executeQuery(countsql);
            if (rs != null && rs.next()) {
                total = rs.getLong(1);
            }
            if (rs != null) {
                rs.close();
                rs = null;
            }

            // 防止受到攻击时,curPage被置为很大,或者很小
            int totalpages = (int) Math.ceil((double) total / pageSize);
            if (curPage > totalpages)
                curPage = totalpages;
            if (curPage <= 0)
                curPage = 1;

            if (total != 0)
                connection.setMaxRows(curPage * pageSize); //尽量减少内存的使用

            rs = connection.executeQuery(sql);
            if (rs == null) {
                return ri;
            } else {
                // 取得列名信息
                ResultSetMetaData rm = rs.getMetaData();
                colCount = rm.getColumnCount();
                for (int i = 1; i <= colCount; i++) {
                    //System.out.println(rm.getColumnName(i));
                    mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));
                }

                rs.setFetchSize(pageSize);

                int absoluteLocation = pageSize * (curPage - 1) + 1;
                //System.out.println("绝对定位于: " + absoluteLocation);
                if (rs.absolute(absoluteLocation) == false) {
                    return ri;
                }

                result = new Vector();

                ResultWrapper rsw = new ResultWrapper(rs);
                do {
                    Vector row = new Vector();
                    for (int i = 0; i < colCount; i++)
                        row.addElement(rsw.getObject(i + 1));
                    result.addElement(row);
                    rowCount++;
                } while (rsw.next());
            }
        } catch (SQLException e) {
            isException = true;
            throw e;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {}
                rs = null;
            }
            if (connection.getAutoCommit()) {
                connection.close();
                connection = null;
            }
        }
        return new ResultIterator(result, mapIndex, total);
    }

    /**
     * 执行更新
     * @param sql String
     * @return int
     * @throws SQLException
     */
    public int executeUpdate(String sql) throws SQLException {
        int r = 0;
        boolean isException = false;
        try {
            r = connection.executeUpdate(sql);
        } catch (SQLException e) {
            isException = true;
            throw e;
        } finally {
            if (connection.getAutoCommit()) {
                connection.close();
                connection = null;
            }
        }
        return r;
    }

    /**
     * 执行更新
     * @param sql String
     * @param objectParams Object[]
     * @return int
     * @throws SQLException
     */
    public int executeUpdate(String sql, Object[] objectParams) throws SQLException {
        int r = 0;
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            fillPreparedStatement(ps, objectParams);
            r = connection.executePreUpdate();
        }
        finally {
            if (ps!=null) {
                try {
                    ps.close();
                }
                catch (Exception e) {}
                ps = null;
            }
            if (connection.getAutoCommit()) {
                connection.close();
                connection = null;
            }
        }
        return r;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -