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

📄 groovyresultset.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* $Id: GroovyResultSet.java,v 1.6 2006/04/09 15:16:53 glaforge Exp $ Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. Redistribution and use of this software and associated documentation ("Software"), with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain copyright    statements and notices.  Redistributions must also contain a    copy of this document. 2. Redistributions in binary form must reproduce the    above copyright notice, this list of conditions and the    following disclaimer in the documentation and/or other    materials provided with the distribution. 3. The name "groovy" must not be used to endorse or promote    products derived from this Software without prior written    permission of The Codehaus.  For written permission,    please contact info@codehaus.org. 4. Products derived from this Software may not be called "groovy"    nor may "groovy" appear in their names without prior written    permission of The Codehaus. "groovy" is a registered    trademark of The Codehaus. 5. Due credit should be given to The Codehaus -    http://groovy.codehaus.org/ THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package groovy.sql;import groovy.lang.Closure;import groovy.lang.GroovyObjectSupport;import groovy.lang.MissingPropertyException;import java.math.BigDecimal;import java.sql.Array;import java.sql.Blob;import java.sql.Clob;import java.sql.Ref;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Statement;import java.util.Calendar;import java.util.Iterator;import java.util.Map;/** * Represents an extent of objects * * @author <a href="mailto:james@coredevelopers.net">James Strachan</a> * @author <a href="mailto:ivan_ganza@yahoo.com">Ivan Ganza</a> * @version $Revision: 1.6 $ * @Author Chris Stevenson */public class GroovyResultSet extends GroovyObjectSupport implements ResultSet {    private ResultSet _resultSet;    private boolean updated;    public GroovyResultSet(ResultSet resultSet) {        this._resultSet = resultSet;    }    protected GroovyResultSet() {    }    protected ResultSet getResultSet() throws SQLException {        return _resultSet;    }    public Object getProperty(String property) {        try {            return getResultSet().getObject(property);        }        catch (SQLException e) {            throw new MissingPropertyException(property, GroovyResultSet.class, e);        }    }    public void setProperty(String property, Object newValue) {        try {            getResultSet().updateObject(property, newValue);            updated = true;        }        catch (SQLException e) {            throw new MissingPropertyException(property, GroovyResultSet.class, e);        }    }    /**     * Supports integer based subscript operators for accessing at numbered columns     * starting at zero. Negative indices are supported, they will count from the last column backwards.     *     * @param index is the number of the column to look at starting at 1     * @return     */    public Object getAt(int index) throws SQLException {        index = normalizeIndex(index);        return getResultSet().getObject(index);    }    /**     * Supports integer based subscript operators for updating the values of numbered columns     * starting at zero. Negative indices are supported, they will count from the last column backwards.     *     * @param index is the number of the column to look at starting at 1     * @return     */    public void putAt(int index, Object newValue) throws SQLException {        index = normalizeIndex(index);        getResultSet().updateObject(index, newValue);    }    /**     * Adds a new row to this result set     *     * @param values     */    public void add(Map values) throws SQLException {        getResultSet().moveToInsertRow();        for (Iterator iter = values.entrySet().iterator(); iter.hasNext();) {            Map.Entry entry = (Map.Entry) iter.next();            getResultSet().updateObject(entry.getKey().toString(), entry.getValue());        }        getResultSet().insertRow();    }    /**     * Takes a zero based index and convert it into an SQL based 1 based index.     * A negative index will count backwards from the last column.     *     * @param index     * @return a JDBC index     * @throws SQLException if some exception occurs finding out the column count     */    protected int normalizeIndex(int index) throws SQLException {        if (index < 0) {            int columnCount = getResultSet().getMetaData().getColumnCount();            do {                index += columnCount;            }            while (index < 0);        }        return index + 1;    }    /**     * Call the closure once for each row in the result set.     *     * @param closure     * @throws SQLException     */    public void eachRow(Closure closure) throws SQLException {        while (next()) {            closure.call(this);        }    }    // Implementation of java.sql.getResultSet()    // ------------------------------------------------------------    /**     * Moves the cursor down one row from its current position.     * A <code>getResultSet()</code> cursor is initially positioned     * before the first row; the first call to the method     * <code>next</code> makes the first row the current row; the     * second call makes the second row the current row, and so on.     * <p/>     * <P>If an input stream is open for the current row, a call     * to the method <code>next</code> will     * implicitly close it. A <code>getResultSet()</code> object's     * warning chain is cleared when a new row is read.     *     * @return <code>true</code> if the new current row is valid;     *         <code>false</code> if there are no more rows     * @throws SQLException if a database access error occurs     */    public boolean next() throws SQLException {        if (updated) {            getResultSet().updateRow();            updated = false;        }        return getResultSet().next();    }    /**     * Releases this <code>getResultSet()</code> object's database and     * JDBC resources immediately instead of waiting for     * this to happen when it is automatically closed.     * <p/>     * <P><B>Note:</B> A <code>getResultSet()</code> object     * is automatically closed by the     * <code>Statement</code> object that generated it when     * that <code>Statement</code> object is closed,     * re-executed, or is used to retrieve the next result from a     * sequence of multiple results. A <code>getResultSet()</code> object     * is also automatically closed when it is garbage collected.     *     * @throws SQLException if a database access error occurs     */    public void close() throws SQLException {        getResultSet().close();    }    /**     * Reports whether     * the last column read had a value of SQL <code>NULL</code>.     * Note that you must first call one of the getter methods     * on a column to try to read its value and then call     * the method <code>wasNull</code> to see if the value read was     * SQL <code>NULL</code>.     *     * @return <code>true</code> if the last column value read was SQL     *         <code>NULL</code> and <code>false</code> otherwise     * @throws SQLException if a database access error occurs     */    public boolean wasNull() throws SQLException {        return getResultSet().wasNull();    }    //======================================================================    // Methods for accessing results by column index    //======================================================================    /**     * Retrieves the value of the designated column in the current row     * of this <code>getResultSet()</code> object as     * a <code>String</code> in the Java programming language.     *     * @param columnIndex the first column is 1, the second is 2, ...     * @return the column value; if the value is SQL <code>NULL</code>, the     *         value returned is <code>null</code>     * @throws SQLException if a database access error occurs     */    public String getString(int columnIndex) throws SQLException {        return getResultSet().getString(columnIndex);    }    /**     * Retrieves the value of the designated column in the current row     * of this <code>getResultSet()</code> object as     * a <code>boolean</code> in the Java programming language.     *     * @param columnIndex the first column is 1, the second is 2, ...     * @return the column value; if the value is SQL <code>NULL</code>, the     *         value returned is <code>false</code>     * @throws SQLException if a database access error occurs     */    public boolean getBoolean(int columnIndex) throws SQLException {        return getResultSet().getBoolean(columnIndex);    }    /**     * Retrieves the value of the designated column in the current row     * of this <code>getResultSet()</code> object as     * a <code>byte</code> in the Java programming language.     *     * @param columnIndex the first column is 1, the second is 2, ...     * @return the column value; if the value is SQL <code>NULL</code>, the     *         value returned is <code>0</code>     * @throws SQLException if a database access error occurs     */    public byte getByte(int columnIndex) throws SQLException {        return getResultSet().getByte(columnIndex);    }    /**     * Retrieves the value of the designated column in the current row     * of this <code>getResultSet()</code> object as     * a <code>short</code> in the Java programming language.     *     * @param columnIndex the first column is 1, the second is 2, ...     * @return the column value; if the value is SQL <code>NULL</code>, the     *         value returned is <code>0</code>     * @throws SQLException if a database access error occurs     */    public short getShort(int columnIndex) throws SQLException {        return getResultSet().getShort(columnIndex);    }    /**     * Retrieves the value of the designated column in the current row     * of this <code>getResultSet()</code> object as     * an <code>int</code> in the Java programming language.     *     * @param columnIndex the first column is 1, the second is 2, ...     * @return the column value; if the value is SQL <code>NULL</code>, the     *         value returned is <code>0</code>     * @throws SQLException if a database access error occurs     */    public int getInt(int columnIndex) throws SQLException {        return getResultSet().getInt(columnIndex);    }

⌨️ 快捷键说明

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