jdbctemplate.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 531 行 · 第 1/2 页

JAVA
531
字号
                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);    }        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 {                        checkConnection();            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++) {                        try {                            row.addElement(rsw.getObject(i + 1));                        }                        catch (SQLException e) {                            row.addElement(null);                            LogUtil.getLog(getClass()).error(StrUtil.trace(e));                        }                    }                    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);    }        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;        try {                        checkConnection();                        String countsql = SQLFilter.getCountSql(sql);                        rs = connection.executeQuery(countsql);            if (rs != null && rs.next()) {                total = rs.getLong(1);            }            if (rs != null) {                rs.close();                rs = null;            }                        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++) {                                        mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i));                }                rs.setFetchSize(pageSize);                int absoluteLocation = pageSize * (curPage - 1) + 1;                                if (rs.absolute(absoluteLocation) == false) {                    return ri;                }                result = new Vector();                ResultWrapper rsw = new ResultWrapper(rs);                do {                    Vector row = new Vector();                    for (int i = 1; i <= colCount; i++)                        row.addElement(rsw.getObject(i));                    result.addElement(row);                    rowCount++;                } while (rsw.next());            }        } 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);    }        public int executeUpdate(String sql) throws SQLException {        int r = 0;        try {                        checkConnection();            r = connection.executeUpdate(sql);        } finally {            if (connection.getAutoCommit()) {                connection.close();                connection = null;            }        }        return r;    }        public int executeUpdate(String sql, Object[] objectParams) throws SQLException {        int r = 0;        PreparedStatement ps = null;        try {                        checkConnection();            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;    }    public void beginTrans() throws SQLException {        connection.beginTrans();    }    public void commit() throws SQLException {                if (connection!=null)            connection.commit();    }    public void rollback() {        connection.rollback();    }        public void close() {        if (connection!=null)            connection.close();    }    public boolean isClosed() {        if (connection==null) {            return true;        }        return connection.isClosed();    }        public void checkConnection() throws SQLException {        if (isClosed()) {            connection = new Connection(connName);        }    }}

⌨️ 快捷键说明

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