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

📄 jtdsstatement.java

📁 第三方的SQL Server and Sybase的jdbc dirver,速度更快
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        if (batchValues == null || batchValues.size() == 0) {            return new int[0];        }        int size = batchValues.size();        int executeSize = connection.getBatchSize();        executeSize = (executeSize == 0) ? Integer.MAX_VALUE : executeSize;        SQLException sqlEx = null;        ArrayList counts = new ArrayList(size);        try {            tds.startBatch();            for (int i = 0; i < size;) {                Object value = batchValues.get(i);                ++i;                // Execute batch now if max size reached or end of batch                boolean executeNow = (i % executeSize == 0) || i == size;                if (value instanceof String) {                    tds.executeSQL((String)value, null, null, true, 0, -1, -1,                            executeNow);                } else {                    executeBatchOther(value, executeNow);                }                // If the batch has been sent, process the results                if (executeNow) {                    sqlEx = tds.getBatchCounts(counts, sqlEx);                    // If a serious error or a MS server error then we stop                    // execution now as count is too small. Sybase continues,                    // flagging failed updates in the count array.                    if (sqlEx != null && counts.size() != i) {                        break;                    }                }            }            //            // Copy the update counts into the int array            //            int updateCounts[] = new int[counts.size()];            for (int i = 0; i < updateCounts.length; i++) {                updateCounts[i] = ((Integer) counts.get(i)).intValue();            }            //            // See if we should return an exception            //            if (sqlEx != null) {                BatchUpdateException batchEx =                        new BatchUpdateException(sqlEx.getMessage(),                                                 sqlEx.getSQLState(),                                                 sqlEx.getErrorCode(),                                                 updateCounts);                // Chain any other exceptions                batchEx.setNextException(sqlEx.getNextException());                throw batchEx;            }            return updateCounts;        } catch (BatchUpdateException ex) {            // If it's a BatchUpdateException let it go            throw ex;        } catch (SQLException ex) {            // An SQLException can only occur while sending the batch            // (getBatchCounts() doesn't throw SQLExceptions), so we have to            // end the batch and return the partial results            // FIXME What should we send here to flush out the batch?            // Come to think of it, is there any circumstance under which this            // could actually happen without the connection getting closed?            // No counts will have been returned either as last packet will not            // have been sent.            throw new BatchUpdateException(ex.getMessage(), ex.getSQLState(),                    ex.getErrorCode(), new int[0]);        } finally {            clearBatch();        }    }    public void setFetchDirection(int direction) throws SQLException {        checkOpen();        switch (direction) {        case ResultSet.FETCH_UNKNOWN:        case ResultSet.FETCH_REVERSE:        case ResultSet.FETCH_FORWARD:            this.fetchDirection = direction;            break;        default:            throw new SQLException(                    Messages.get("error.generic.badoption",                            Integer.toString(direction),                            "direction"),                    "24000");        }    }    public void setFetchSize(int rows) throws SQLException {        checkOpen();        if (rows < 0 || (maxRows > 0 && rows > maxRows)) {            throw new SQLException(                Messages.get("error.generic.optltzero", "setFetchSize"),                    "HY092");        }        this.fetchSize = rows;    }    public void setMaxFieldSize(int max) throws SQLException {        checkOpen();        if (max < 0) {            throw new SQLException(                Messages.get("error.generic.optltzero", "setMaxFieldSize"),                    "HY092");        }        maxFieldSize = max;    }    public void setMaxRows(int max) throws SQLException {        checkOpen();        if (max < 0) {            throw new SQLException(                Messages.get("error.generic.optltzero", "setMaxRows"),                    "HY092");        }        this.maxRows = max;    }    public void setQueryTimeout(int seconds) throws SQLException {        checkOpen();        if (seconds < 0) {            throw new SQLException(                Messages.get("error.generic.optltzero", "setQueryTimeout"),                    "HY092");        }        this.queryTimeout = seconds;    }    public boolean getMoreResults(int current) throws SQLException {        checkOpen();        switch (current) {            case CLOSE_ALL_RESULTS:                updateCount = -1;                closeAllResultSets();                break;            case CLOSE_CURRENT_RESULT:                updateCount = -1;                closeCurrentResultSet();                break;            case KEEP_CURRENT_RESULT:                updateCount = -1;                // If there is an open result set it is transferred to                // the list of open result sets. For JtdsResultSet                // result sets we cache the remaining data. For CachedResultSet                // result sets the data is already cached.                if (openResultSets == null) {                    openResultSets = new ArrayList();                }                if (currentResult instanceof MSCursorResultSet                        || currentResult instanceof CachedResultSet) {                    // NB. Due to restrictions on the way API cursors are                    // created, MSCursorResultSet can never be followed by                    // any other result sets, update counts or return variables.                    openResultSets.add(currentResult);                } else if (currentResult != null) {                    currentResult.cacheResultSetRows();                    openResultSets.add(currentResult);                }                currentResult = null;                break;            default:                throw new SQLException(                        Messages.get("error.generic.badoption",                                Integer.toString(current),                                "current"),                        "HY092");        }        // Check for server side errors        messages.checkErrors();        // Dequeue any results        if (!resultQueue.isEmpty() || processResults(false, false)) {            Object nextResult = resultQueue.removeFirst();            // Next result is an update count            if (nextResult instanceof Integer) {                updateCount = ((Integer) nextResult).intValue();                return false;            }            // Next result is a ResultSet. Set currentResult and remove it.            currentResult = (JtdsResultSet) nextResult;            return true;        } else {            return false;        }    }    public void setEscapeProcessing(boolean enable) throws SQLException {        checkOpen();        this.escapeProcessing = enable;    }    public int executeUpdate(String sql) throws SQLException {        return executeUpdate(sql, NO_GENERATED_KEYS);    }    public void addBatch(String sql) throws SQLException {        checkOpen();        if (sql == null) {            throw new NullPointerException();        }        if (batchValues == null) {            batchValues = new ArrayList();        }        if (escapeProcessing) {            String tmp[] = SQLParser.parse(sql, null, connection, false);            if (tmp[1].length() != 0) {                throw new SQLException(                        Messages.get("error.statement.badsql"), "07000");            }            sql = tmp[0];        }        batchValues.add(sql);    }    public void setCursorName(String name) throws SQLException {        checkOpen();        this.cursorName = name;        if (name != null) {            // Reset statement type to JDBC 1 default.            this.resultSetType = ResultSet.TYPE_FORWARD_ONLY;            this.fetchSize = 1; // Needed for positioned updates        }    }    public boolean execute(String sql) throws SQLException {        return executeImpl(sql, NO_GENERATED_KEYS, false);    }    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {        executeImpl(sql, autoGeneratedKeys, true);        int res = getUpdateCount();        return res == -1 ? 0 : res;    }    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {        return executeImpl(sql, autoGeneratedKeys, false);    }    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {        checkOpen();        if (columnIndexes == null) {            throw new SQLException(                Messages.get("error.generic.nullparam", "executeUpdate"),"HY092");        } else if (columnIndexes.length != 1) {            throw new SQLException(                Messages.get("error.generic.needcolindex", "executeUpdate"),"HY092");        }        return executeUpdate(sql, RETURN_GENERATED_KEYS);    }    public boolean execute(String sql, int[] columnIndexes) throws SQLException {        checkOpen();        if (columnIndexes == null) {            throw new SQLException(                Messages.get("error.generic.nullparam", "execute"),"HY092");        } else if (columnIndexes.length != 1) {            throw new SQLException(                Messages.get("error.generic.needcolindex", "execute"),"HY092");        }        return executeImpl(sql, RETURN_GENERATED_KEYS, false);    }    public Connection getConnection() throws SQLException {        checkOpen();        return this.connection;    }    public ResultSet getGeneratedKeys() throws SQLException {        checkOpen();        if (genKeyResultSet == null) {            String colNames[] = {"ID"};            int    colTypes[] = {Types.INTEGER};            //            // Return an empty result set            //            CachedResultSet rs = new CachedResultSet(this, colNames, colTypes);            rs.setConcurrency(ResultSet.CONCUR_READ_ONLY);            genKeyResultSet = rs;        }        return genKeyResultSet;    }    public ResultSet getResultSet() throws SQLException {        checkOpen();        //        if (currentResult instanceof MSCursorResultSet ||            currentResult instanceof CachedResultSet) {            return currentResult;        }        //        // See if we are returning a forward read only resultset        //        if (currentResult == null ||            (resultSetType == ResultSet.TYPE_FORWARD_ONLY &&             resultSetConcurrency == ResultSet.CONCUR_READ_ONLY)) {            return currentResult;        }        //        // OK Now create a CachedResultSet based on the existng result set.        //        currentResult = new CachedResultSet(currentResult, true);        return currentResult;    }    public SQLWarning getWarnings() throws SQLException {        checkOpen();        return messages.getWarnings();    }    public int executeUpdate(String sql, String[] columnNames) throws SQLException {        checkOpen();        if (columnNames == null) {            throw new SQLException(                Messages.get("error.generic.nullparam", "executeUpdate"),"HY092");        } else if (columnNames.length != 1) {            throw new SQLException(                Messages.get("error.generic.needcolname", "executeUpdate"),"HY092");        }        return executeUpdate(sql, RETURN_GENERATED_KEYS);    }    public boolean execute(String sql, String[] columnNames) throws SQLException {        if (columnNames == null) {            throw new SQLException(                Messages.get("error.generic.nullparam", "execute"),"HY092");        } else if (columnNames.length != 1) {            throw new SQLException(                Messages.get("error.generic.needcolname", "execute"),"HY092");        }        return executeImpl(sql, RETURN_GENERATED_KEYS, false);    }    public ResultSet executeQuery(String sql) throws SQLException {        checkOpen();        initialize();        if (sql == null || sql.length() == 0) {            throw new SQLException(Messages.get("error.generic.nosql"), "HY000");        }        if (escapeProcessing) {            String tmp[] = SQLParser.parse(sql, null, connection, false);            if (tmp[1].length() != 0) {                throw new SQLException(                    Messages.get("error.statement.badsql"), "07000");            }            sql = tmp[0];        }        return this.executeSQLQuery(sql, null, null, useCursor(false, null));    }}

⌨️ 快捷键说明

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