📄 preparedstatement.java
字号:
// // Only apply max_rows to selects // if (this.connection.useMaxRows()) { int rowLimit = -1; if (this.firstCharOfStmt == 'S') { if (this.hasLimitClause) { rowLimit = this.maxRows; } else { if (this.maxRows <= 0) { this.connection.execSQL(this, "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, //$NON-NLS-1$ null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.currentCatalog, true); } else { this.connection.execSQL(this, "SET OPTION SQL_SELECT_LIMIT=" + this.maxRows, -1, //$NON-NLS-1$ null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.currentCatalog, true); } } } else { this.connection.execSQL(this, "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, null, //$NON-NLS-1$ java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.currentCatalog, true); } // Finally, execute the query rs = executeInternal(rowLimit, sendPacket, createStreamingResultSet(), (this.firstCharOfStmt == 'S'), true); } else { rs = executeInternal(-1, sendPacket, createStreamingResultSet(), (this.firstCharOfStmt == 'S'), true); } if (this.retrieveGeneratedKeys) { this.connection.setReadInfoMsgEnabled(oldInfoMsgState); rs.setFirstCharOfQuery('R'); } if (oldCatalog != null) { this.connection.setCatalog(oldCatalog); } this.lastInsertId = rs.getUpdateID(); if (rs != null) { this.results = rs; } } return ((rs != null) && rs.reallyResult()); } /** * JDBC 2.0 Submit a batch of commands to the database for execution. This * method is optional. * * @return an array of update counts containing one element for each * command in the batch. The array is ordered according to the * order in which commands were inserted into the batch * * @exception SQLException if a database-access error occurs, or the driver * does not support batch statements * @throws java.sql.BatchUpdateException DOCUMENT ME! */ public int[] executeBatch() throws SQLException { if (this.connection.isReadOnly()) { throw new SQLException(Messages.getString("PreparedStatement.25") //$NON-NLS-1$ + Messages.getString("PreparedStatement.26"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } try { clearWarnings(); int[] updateCounts = null; if (this.batchedArgs != null) { int nbrCommands = this.batchedArgs.size(); updateCounts = new int[nbrCommands]; for (int i = 0; i < nbrCommands; i++) { updateCounts[i] = -3; } SQLException sqlEx = null; int commandIndex = 0; if (this.retrieveGeneratedKeys) { this.batchedGeneratedKeys = new ArrayList(nbrCommands); } for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) { Object arg = this.batchedArgs.get(commandIndex); if (arg instanceof String) { updateCounts[commandIndex] = executeUpdate((String) arg); } else { BatchParams paramArg = (BatchParams) arg; try { updateCounts[commandIndex] = executeUpdate(paramArg.parameterStrings, paramArg.parameterStreams, paramArg.isStream, paramArg.streamLengths, paramArg.isNull); if (this.retrieveGeneratedKeys) { java.sql.ResultSet rs = null; try { // we don't want to use our version, // because we've altered the behavior of ours to support batch updates // (catch-22) rs = super.getGeneratedKeys(); while (rs.next()) { this.batchedGeneratedKeys.add(new byte[][] { rs.getBytes(1) }); } } finally { if (rs != null) { rs.close(); } } } } catch (SQLException ex) { updateCounts[commandIndex] = EXECUTE_FAILED; if (this.connection.getContinueBatchOnError()) { sqlEx = ex; } else { int[] newUpdateCounts = new int[commandIndex]; System.arraycopy(updateCounts, 0, newUpdateCounts, 0, commandIndex); throw new java.sql.BatchUpdateException(ex .getMessage(), ex.getSQLState(), ex.getErrorCode(), newUpdateCounts); } } } } if (sqlEx != null) { throw new java.sql.BatchUpdateException(sqlEx.getMessage(), sqlEx.getSQLState(), sqlEx.getErrorCode(), updateCounts); } } return (updateCounts != null) ? updateCounts : new int[0]; } finally { clearBatch(); } } /** * A Prepared SQL query is executed and its ResultSet is returned * * @return a ResultSet that contains the data produced by the query - never * null * * @exception SQLException if a database access error occurs */ public synchronized java.sql.ResultSet executeQuery() throws SQLException { checkClosed(); checkForDml(this.originalSql, this.firstCharOfStmt); CachedResultSetMetaData cachedMetadata = null; // We need to execute this all together // So synchronize on the Connection's mutex (because // even queries going through there synchronize // on the same mutex. synchronized (this.connection.getMutex()) { clearWarnings(); this.batchedGeneratedKeys = null; Buffer sendPacket = fillSendPacket(); if (this.results != null) { if (!this.connection.getHoldResultsOpenOverStatementClose()) { this.results.realClose(false); } } String oldCatalog = null; if (!this.connection.getCatalog().equals(this.currentCatalog)) { oldCatalog = this.connection.getCatalog(); this.connection.setCatalog(this.currentCatalog); } // // Check if we have cached metadata for this query... // if (this.connection.getCacheResultSetMetadata()) { cachedMetadata = getCachedMetaData(this.originalSql); } if (this.connection.useMaxRows()) { // If there isn't a limit clause in the SQL // then limit the number of rows to return in // an efficient manner. Only do this if // setMaxRows() hasn't been used on any Statements // generated from the current Connection (saves // a query, and network traffic). if (this.hasLimitClause) { this.results = executeInternal(this.maxRows, sendPacket, createStreamingResultSet(), true, (cachedMetadata == null)); } else { if (this.maxRows <= 0) { this.connection.execSQL(this, "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, null, //$NON-NLS-1$ java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.currentCatalog, true); } else { this.connection.execSQL(this, "SET OPTION SQL_SELECT_LIMIT=" + this.maxRows, -1, null, //$NON-NLS-1$ java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.currentCatalog, true); } this.results = executeInternal(-1, sendPacket, createStreamingResultSet(), true, (cachedMetadata == null)); if (oldCatalog != null) { this.connection.setCatalog(oldCatalog); } } } else { this.results = executeInternal(-1, sendPacket, createStreamingResultSet(), true, (cachedMetadata == null)); } if (oldCatalog != null) { this.connection.setCatalog(oldCatalog); } } this.lastInsertId = this.results.getUpdateID(); if (cachedMetadata != null) { initializeResultsMetadataFromCache(this.originalSql, cachedMetadata, this.results); } else { if (this.connection.getCacheResultSetMetadata()) { initializeResultsMetadataFromCache(this.originalSql, null /* will be created */, this.results); } } return this.results; } /** * Execute a SQL INSERT, UPDATE or DELETE statement. In addition, SQL * statements that return nothing such as SQL DDL statements can be * executed. * * @return either the row count for INSERT, UPDATE or DELETE; or 0 for SQL * statements that return nothing. * * @exception SQLException if a database access error occurs */ public synchronized int executeUpdate() throws SQLException { return executeUpdate(true); } /** * Returns this PreparedStatement represented as a string. * * @return this PreparedStatement represented as a string. */ public String toString() { StringBuffer buf = new StringBuffer(); buf.append(super.toString()); buf.append(": "); //$NON-NLS-1$ buf.append(asSql()); return buf.toString(); } /** * Used by updatable result sets for refreshRow() because the parameter has * already been escaped for updater or inserter prepared statements. * * @param parameterIndex the parameter to set. * @param parameterAsBytes the parameter as a string. * * @throws SQLException if an error occurs */ protected void setBytesNoEscape(int parameterIndex, byte[] parameterAsBytes) throws SQLException { byte[] parameterWithQuotes = new byte[parameterAsBytes.length + 2]; parameterWithQuotes[0] = '\''; System.arraycopy(parameterAsBytes, 0, parameterWithQuotes, 1, parameterAsBytes.length); parameterWithQuotes[parameterAsBytes.length + 1] = '\''; setInternal(parameterIndex, parameterWithQuotes); } protected void setBytesNoEscapeNoQuotes(int parameterIndex, byte[] parameterAsBytes) throws SQLException { setInternal(parameterIndex, parameterAsBytes); } /** * DOCUMENT ME! * * @param retrieveGeneratedKeys */ protected void setRetrieveGeneratedKeys(boolean retrieveGeneratedKeys) { this.retrieveGeneratedKeys = retrieveGeneratedKeys; } protected String asSql() { StringBuffer buf = new StringBuffer(); try { for (int i = 0; i < this.parameterCount; ++i) { if (this.charEncoding != null) { buf.append(new String(this.staticSqlStrings[i], this.charEncoding)); } else { buf.append(new String(this.staticSqlStrings[i])); } if ((this.parameterValues[i] == null) && !this.isStream[i]) { buf.append("** NOT SPECIFIED **"); //$NON-NLS-1$ } else if (this.isStream[i]) { buf.append("** STREAM DATA **"); //$NON-NLS-1$ } else { if (this.charConverter != null) { buf.append(this.charConverter.toString( this.parameterValues[i])); } else { if (this.charEncoding != null) { buf.append(new String(this.parameterValues[i], this.charEncoding));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -