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

📄 sql.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * $Id: Sql.java,v 1.21 2006/05/30 17:27:29 blackdrag 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.GString;import java.security.AccessController;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.sql.Types;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.LinkedHashMap;import java.util.Properties;import java.util.logging.Level;import java.util.logging.Logger;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.sql.DataSource;/** * Represents an extent of objects * * @author Chris Stevenson * @author <a href="mailto:james@coredevelopers.net">James Strachan </a> * @version $Revision: 1.21 $ */public class Sql {    protected Logger log = Logger.getLogger(getClass().getName());    private DataSource dataSource;    private Connection useConnection;    /** lets only warn of using deprecated methods once */    private boolean warned;    // store the last row count for executeUpdate    int updateCount = 0;    /** allows a closure to be used to configure the statement before its use */    private Closure configureStatement;    /**     * A helper method which creates a new Sql instance from a JDBC connection     * URL     *     * @param url     * @return a new Sql instance with a connection     */    public static Sql newInstance(String url) throws SQLException {        Connection connection = DriverManager.getConnection(url);        return new Sql(connection);    }    /**     * A helper method which creates a new Sql instance from a JDBC connection     * URL     *     * @param url     * @return a new Sql instance with a connection     */    public static Sql newInstance(String url, Properties properties) throws SQLException {        Connection connection = DriverManager.getConnection(url, properties);        return new Sql(connection);    }    /**     * A helper method which creates a new Sql instance from a JDBC connection     * URL and driver class name     *     * @param url     * @return a new Sql instance with a connection     */    public static Sql newInstance(String url, Properties properties, String driverClassName) throws SQLException, ClassNotFoundException {        loadDriver(driverClassName);        return newInstance(url, properties);    }    /**     * A helper method which creates a new Sql instance from a JDBC connection     * URL, username and password     *     * @param url     * @return a new Sql instance with a connection     */    public static Sql newInstance(String url, String user, String password) throws SQLException {        Connection connection = DriverManager.getConnection(url, user, password);        return new Sql(connection);    }    /**     * A helper method which creates a new Sql instance from a JDBC connection     * URL, username, password and driver class name     *     * @param url     * @return a new Sql instance with a connection     */    public static Sql newInstance(String url, String user, String password, String driverClassName) throws SQLException,            ClassNotFoundException {        loadDriver(driverClassName);        return newInstance(url, user, password);    }    /**     * A helper method which creates a new Sql instance from a JDBC connection     * URL and driver class name     *     * @param url     * @param driverClassName     *            the class name of the driver     * @return a new Sql instance with a connection     */    public static Sql newInstance(String url, String driverClassName) throws SQLException, ClassNotFoundException {        loadDriver(driverClassName);        return newInstance(url);    }    /**     * Attempts to load the JDBC driver on the thread, current or system class     * loaders     *     * @param driverClassName     * @throws ClassNotFoundException     */    public static void loadDriver(String driverClassName) throws ClassNotFoundException {        // lets try the thread context class loader first        // lets try to use the system class loader        try {            Class.forName(driverClassName);        }        catch (ClassNotFoundException e) {            try {                Thread.currentThread().getContextClassLoader().loadClass(driverClassName);            }            catch (ClassNotFoundException e2) {                // now lets try the classloader which loaded us                try {                    Sql.class.getClassLoader().loadClass(driverClassName);                }                catch (ClassNotFoundException e3) {                    throw e;                }            }        }    }    public static final OutParameter ARRAY         = new OutParameter(){ public int getType() { return Types.ARRAY; }};    public static final OutParameter BIGINT        = new OutParameter(){ public int getType() { return Types.BIGINT; }};    public static final OutParameter BINARY        = new OutParameter(){ public int getType() { return Types.BINARY; }};    public static final OutParameter BIT           = new OutParameter(){ public int getType() { return Types.BIT; }};    public static final OutParameter BLOB          = new OutParameter(){ public int getType() { return Types.BLOB; }};    public static final OutParameter BOOLEAN       = new OutParameter(){ public int getType() { return Types.BOOLEAN; }};    public static final OutParameter CHAR          = new OutParameter(){ public int getType() { return Types.CHAR; }};    public static final OutParameter CLOB          = new OutParameter(){ public int getType() { return Types.CLOB; }};    public static final OutParameter DATALINK      = new OutParameter(){ public int getType() { return Types.DATALINK; }};    public static final OutParameter DATE          = new OutParameter(){ public int getType() { return Types.DATE; }};    public static final OutParameter DECIMAL       = new OutParameter(){ public int getType() { return Types.DECIMAL; }};    public static final OutParameter DISTINCT      = new OutParameter(){ public int getType() { return Types.DISTINCT; }};    public static final OutParameter DOUBLE        = new OutParameter(){ public int getType() { return Types.DOUBLE; }};    public static final OutParameter FLOAT         = new OutParameter(){ public int getType() { return Types.FLOAT; }};    public static final OutParameter INTEGER       = new OutParameter(){ public int getType() { return Types.INTEGER; }};    public static final OutParameter JAVA_OBJECT   = new OutParameter(){ public int getType() { return Types.JAVA_OBJECT; }};    public static final OutParameter LONGVARBINARY = new OutParameter(){ public int getType() { return Types.LONGVARBINARY; }};    public static final OutParameter LONGVARCHAR   = new OutParameter(){ public int getType() { return Types.LONGVARCHAR; }};    public static final OutParameter NULL          = new OutParameter(){ public int getType() { return Types.NULL; }};    public static final OutParameter NUMERIC       = new OutParameter(){ public int getType() { return Types.NUMERIC; }};    public static final OutParameter OTHER         = new OutParameter(){ public int getType() { return Types.OTHER; }};    public static final OutParameter REAL          = new OutParameter(){ public int getType() { return Types.REAL; }};    public static final OutParameter REF           = new OutParameter(){ public int getType() { return Types.REF; }};    public static final OutParameter SMALLINT      = new OutParameter(){ public int getType() { return Types.SMALLINT; }};    public static final OutParameter STRUCT        = new OutParameter(){ public int getType() { return Types.STRUCT; }};    public static final OutParameter TIME          = new OutParameter(){ public int getType() { return Types.TIME; }};    public static final OutParameter TIMESTAMP     = new OutParameter(){ public int getType() { return Types.TIMESTAMP; }};    public static final OutParameter TINYINT       = new OutParameter(){ public int getType() { return Types.TINYINT; }};    public static final OutParameter VARBINARY     = new OutParameter(){ public int getType() { return Types.VARBINARY; }};    public static final OutParameter VARCHAR       = new OutParameter(){ public int getType() { return Types.VARCHAR; }};    public static InParameter ARRAY(Object value) { return in(Types.ARRAY, value); }    public static InParameter BIGINT(Object value) { return in(Types.BIGINT, value); }    public static InParameter BINARY(Object value) { return in(Types.BINARY, value); }    public static InParameter BIT(Object value) { return in(Types.BIT, value); }    public static InParameter BLOB(Object value) { return in(Types.BLOB, value); }    public static InParameter BOOLEAN(Object value) { return in(Types.BOOLEAN, value); }    public static InParameter CHAR(Object value) { return in(Types.CHAR, value); }    public static InParameter CLOB(Object value) { return in(Types.CLOB, value); }    public static InParameter DATALINK(Object value) { return in(Types.DATALINK, value); }    public static InParameter DATE(Object value) { return in(Types.DATE, value); }    public static InParameter DECIMAL(Object value) { return in(Types.DECIMAL, value); }    public static InParameter DISTINCT(Object value) { return in(Types.DISTINCT, value); }    public static InParameter DOUBLE(Object value) { return in(Types.DOUBLE, value); }    public static InParameter FLOAT(Object value) { return in(Types.FLOAT, value); }    public static InParameter INTEGER(Object value) { return in(Types.INTEGER, value); }    public static InParameter JAVA_OBJECT(Object value) { return in(Types.JAVA_OBJECT, value); }    public static InParameter LONGVARBINARY(Object value) { return in(Types.LONGVARBINARY, value); }    public static InParameter LONGVARCHAR(Object value) { return in(Types.LONGVARCHAR, value); }    public static InParameter NULL(Object value) { return in(Types.NULL, value); }    public static InParameter NUMERIC(Object value) { return in(Types.NUMERIC, value); }    public static InParameter OTHER(Object value) { return in(Types.OTHER, value); }    public static InParameter REAL(Object value) { return in(Types.REAL, value); }    public static InParameter REF(Object value) { return in(Types.REF, value); }    public static InParameter SMALLINT(Object value) { return in(Types.SMALLINT, value); }    public static InParameter STRUCT(Object value) { return in(Types.STRUCT, value); }    public static InParameter TIME(Object value) { return in(Types.TIME, value); }    public static InParameter TIMESTAMP(Object value) { return in(Types.TIMESTAMP, value); }    public static InParameter TINYINT(Object value) { return in(Types.TINYINT, value); }    public static InParameter VARBINARY(Object value) { return in(Types.VARBINARY, value); }    public static InParameter VARCHAR(Object value) { return in(Types.VARCHAR, value); }    /**     * Create a new InParameter     * @param type the JDBC data type     * @param value the object value     * @return an InParameter     */    public static InParameter in(final int type, final Object value) {        return new InParameter() {            public int getType() {                return type;            }            public Object getValue() {                return value;            }        };    }        /**     * Create a new OutParameter     * @param type the JDBC data type.     * @return an OutParameter     */    public static OutParameter out(final int type){        return new OutParameter(){            public int getType() {                return type;            }        };    }        /**     * Create an inout parameter using this in parameter.     * @param in     * @return     */    public static InOutParameter inout(final InParameter in){        return new InOutParameter(){            public int getType() {                return in.getType();            }            public Object getValue() {                return in.getValue();            }                    };    }        /**     * Create a new ResultSetOutParameter     * @param type the JDBC data type.     * @return a ResultSetOutParameter     */    public static ResultSetOutParameter resultSet(final int type){        return new ResultSetOutParameter(){            public int getType() {                return type;            }        };    }            /**     * Creates a variable to be expanded in the Sql string rather     * than representing an sql parameter.     * @param object

⌨️ 快捷键说明

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