poolmanpreparedstatement.java

来自「Java Database connection pool」· Java 代码 · 共 299 行

JAVA
299
字号
/* *  PoolMan Java Object Pooling and Caching Library *  Copyright (C) 1999-2001 The Code Studio * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  The full license is located at the root of this distribution *  in the LICENSE file. */package com.codestudio.sql;// Code Studio Libraryimport com.codestudio.util.ObjectPool;import java.io.InputStream;import java.io.Reader;import java.math.BigDecimal;import java.sql.Array;import java.sql.Blob;import java.sql.Clob;import java.sql.PreparedStatement;import java.sql.Ref;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Time;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Calendar;/** * A PreparedStatement that is aware of its Connection and resources. *<p> * It encapsulates a true driver-specific Statement that handles * all the necessary JDBC methods by delegation. */public class PoolManPreparedStatement        extends PoolManStatement        implements PreparedStatement {    private PreparedStatement statement;    public PoolManPreparedStatement(PoolManConnection pcon, PreparedStatement s, ObjectPool p) {        super(pcon, s, p);        this.statement = s;    }    public PoolManPreparedStatement(PoolManConnection pcon, PreparedStatement s, String rawSQL, ObjectPool p) {        this(pcon, s, p);        this.sqlString = rawSQL;    }    public void close() throws SQLException {        clean();        mypool.returnPooledStatement(this);        poolmanCon.removeOpenStatement(this);        this.closed = true;    }    public PreparedStatement getNativePreparedStatement() {        return this.statement;    }    public ResultSet executeQuery() throws SQLException {        ArrayList rowlist = new ArrayList();        ResultSet r = this.statement.executeQuery();        // determine whether we need a PoolMan ResultSet Impl        if (mypool.isUsingNativeResults()) {            openres.put(r, null);            lastRS = r;            return r;        }        ResultSetMetaData rmeta = PoolManResultSetMetaData.getCopy(r.getMetaData());        try {            int cols = rmeta.getColumnCount();            String[] tableNames = new String[cols];            for (int i = 0; i < cols; i++) {                // Some irritating drivers don't supply a  table name!!                String tableName = null;                try {                    tableName = rmeta.getTableName(i);                    if (tableName.trim().length() == 0)                        tableName = null;                } catch (Exception e) {                    tableName = null;                }                // try to fabricate it if this happens to be one of those drivers.                if (tableName == null)                    tableName = fabricateTableName(this.sqlString, i);                tableNames[i] = tableName;            }            while (r.next()) {                ArrayList row = new ArrayList(1);                for (int i = 1; i <= cols; i++) {                    Result data = new Result(tableNames[i - 1], rmeta.getColumnLabel(i),                                             r.getObject(i), rmeta.getColumnType(i));                    row.add(data);                }                rowlist.add(row);            }        } catch (Exception e) {            throw new SQLException(e.getMessage());        }        finally {            if (null != r) {                try {                    r.close();                } catch (Exception ee) {                }            }        }        ResultSet finalRes = new PoolManResultSet(this, rowlist, rmeta,                                                  this.resultSetType, this.resultSetConcurrency);        openres.put(finalRes, null);        lastRS = finalRes;        return finalRes;    }    public int executeUpdate() throws SQLException {        return this.statement.executeUpdate();    }    public boolean execute() throws SQLException {        return this.statement.execute();    }    // JDBC 2.0    public void addBatch() throws SQLException {        if (mypool.isUsingNativeResults())            this.statement.addBatch();        else            throw new UnsupportedOperationException("Operation available only when using native results " +                                                    "(native results can be set to true in poolman.xml)");    }    public void clearParameters() throws SQLException {        this.statement.clearParameters();    }    public ResultSetMetaData getMetaData() throws SQLException {        ResultSet r = executeQuery();        return PoolManResultSetMetaData.getCopy(r.getMetaData());    }    // JDBC 2.0    public void setArray(int i, Array x) throws SQLException {        this.statement.setArray(i, x);    }    public void setAsciiStream(int i, InputStream x, int length) throws SQLException {        this.statement.setAsciiStream(i, x, length);    }    public void setBigDecimal(int i, BigDecimal x) throws SQLException {        this.statement.setBigDecimal(i, x);    }    public void setBinaryStream(int i, InputStream x, int length) throws SQLException {        this.statement.setBinaryStream(i, x, length);    }    public void setBlob(int i, Blob x) throws SQLException {        this.statement.setBlob(i, x);    }    public void setBoolean(int i, boolean x) throws SQLException {        this.statement.setBoolean(i, x);    }    public void setByte(int i, byte b) throws SQLException {        this.statement.setByte(i, b);    }    public void setBytes(int i, byte[] x) throws SQLException {        this.statement.setBytes(i, x);    }    public void setCharacterStream(int i, Reader reader, int length) throws SQLException {        this.statement.setCharacterStream(i, reader, length);    }    public void setClob(int i, Clob clob) throws SQLException {        this.statement.setClob(i, clob);    }    public void setDate(int i, java.sql.Date d) throws SQLException {        this.statement.setDate(i, d);    }    public void setDate(int i, java.sql.Date d, Calendar cal) throws SQLException {        this.statement.setDate(i, d, cal);    }    public void setDouble(int i, double d) throws SQLException {        this.statement.setDouble(i, d);    }    public void setFloat(int i, float f) throws SQLException {        this.statement.setFloat(i, f);    }    public void setInt(int i, int x) throws SQLException {        this.statement.setInt(i, x);    }    public void setLong(int i, long l) throws SQLException {        this.statement.setLong(i, l);    }    public void setNull(int i, int sqlType) throws SQLException {        this.statement.setNull(i, sqlType);    }    public void setNull(int i, int sqlType, String typeName) throws SQLException {        this.statement.setNull(i, sqlType, typeName);    }    public void setObject(int i, Object o) throws SQLException {        this.statement.setObject(i, o);    }    public void setObject(int i, Object o, int targetType) throws SQLException {        this.statement.setObject(i, o, targetType);    }    public void setObject(int i, Object o, int targetType, int scale) throws SQLException {        this.statement.setObject(i, o, targetType, scale);    }    public void setRef(int i, Ref ref) throws SQLException {        this.statement.setRef(i, ref);    }    public void setShort(int i, short s) throws SQLException {        this.statement.setShort(i, s);    }    public void setString(int i, String s) throws SQLException {        this.statement.setString(i, s);    }    public void setTime(int i, Time t) throws SQLException {        this.statement.setTime(i, t);    }    public void setTime(int i, Time t, Calendar cal) throws SQLException {        this.statement.setTime(i, t, cal);    }    public void setTimestamp(int i, Timestamp t) throws SQLException {        this.statement.setTimestamp(i, t);    }    public void setTimestamp(int i, Timestamp t, Calendar cal) throws SQLException {        this.statement.setTimestamp(i, t, cal);    }    public void setUnicodeStream(int i, InputStream x, int length) throws SQLException {        this.statement.setUnicodeStream(i, x, length);    }
    /* OBJECT METHODS */

    public String toString() {        return "PoolManPreparedStatement-[UnderlyingStatement:" + this.statement.toString() + "]";    }}

⌨️ 快捷键说明

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