📄 preparedstatement.java
字号:
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.continueBatchOnError()) { 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 * * @throws SQLException if a database error occurs. */ public synchronized java.sql.ResultSet executeQuery() throws SQLException { checkClosed(); if ((this.firstCharOfStmt == 'I') || (this.firstCharOfStmt == 'U') || (this.firstCharOfStmt == 'D') || (this.firstCharOfStmt == 'A') || (this.firstCharOfStmt == 'C')) { if (StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "INSERT") || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "UPDATE") || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "DELETE") || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "DROP") || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "CREATE") || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "ALTER")) { throw new SQLException("Can not issue data manipulation statements with executeQuery()", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } } // 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 (connection.getMutex()) { this.batchedGeneratedKeys = null; Buffer sendPacket = fillSendPacket(); if (this.results != null) { this.results.close(); } String oldCatalog = null; if (!this.connection.getCatalog().equals(currentCatalog)) { oldCatalog = this.connection.getCatalog(); this.connection.setCatalog(currentCatalog); } 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 (hasLimitClause) { results = this.connection.execSQL((String) null, maxRows, sendPacket, resultSetConcurrency, createStreamingResultSet(), true, this.currentCatalog); } else { if (maxRows <= 0) { this.connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, this.currentCatalog); } else { this.connection.execSQL("SET OPTION SQL_SELECT_LIMIT=" + maxRows, -1, this.currentCatalog); } this.results = this.connection.execSQL(null, -1, sendPacket, resultSetConcurrency, createStreamingResultSet(), true, this.currentCatalog); if (oldCatalog != null) { this.connection.setCatalog(oldCatalog); } } } else { this.results = this.connection.execSQL(null, -1, sendPacket, resultSetConcurrency, createStreamingResultSet(), true, this.currentCatalog); } if (oldCatalog != null) { this.connection.setCatalog(oldCatalog); } } lastInsertId = this.results.getUpdateID(); nextResults = this.results; this.results.setConnection(connection); this.results.setResultSetType(resultSetType); this.results.setResultSetConcurrency(resultSetConcurrency); this.results.setStatement(this); return (java.sql.ResultSet) 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. * * @throws SQLException if a database access error occurs */ public synchronized int executeUpdate() throws SQLException { this.batchedGeneratedKeys = null; return executeUpdate(parameterValues, parameterStreams, isStream, streamLengths, isNull); } /** * Returns this PreparedStatement represented as a string. * * @return this PreparedStatement represented as a string. * * @throws RuntimeException if an error occurs */ public String toString() { StringBuffer buf = new StringBuffer(); buf.append(super.toString()); buf.append(": "); try { for (int i = 0; i < parameterValues.length; ++i) { if (this.charEncoding != null) { buf.append(new String(staticSqlStrings[i], this.charEncoding)); } else { buf.append(new String(staticSqlStrings[i])); } if ((parameterValues[i] == null) && !isStream[i]) { buf.append("** NOT SPECIFIED **"); } else if (isStream[i]) { buf.append("** STREAM DATA **"); } else { if (this.charConverter != null) { buf.append(this.charConverter.toString( parameterValues[i])); } else { if (this.charEncoding != null) { buf.append(new String(parameterValues[i], this.charEncoding)); } else { buf.append(StringUtils.toAsciiString( parameterValues[i])); } } } } if (this.charEncoding != null) { buf.append(new String( staticSqlStrings[parameterValues.length], this.charEncoding)); } else { buf.append(staticSqlStrings[parameterValues.length]); } } catch (UnsupportedEncodingException uue) { throw new RuntimeException("Unsupported character encoding '" + this.charEncoding + "'"); } 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); } /** * Sets wheather or not this statement should retreive generated keys. * * @param retrieveGeneratedKeys */ protected void setRetrieveGeneratedKeys(boolean retrieveGeneratedKeys) { this.retrieveGeneratedKeys = retrieveGeneratedKeys; } /** * Added to allow batch-updates * * @param batchedParameterStrings string values used in single statement * @param batchedParameterStreams stream values used in single statement * @param batchedIsStream flags for streams used in single statement * @param batchedStreamLengths lengths of streams to be read. * @param batchedIsNull flags for parameters that are null * * @return the update count * * @throws SQLException if a database error occurs * @throws java.sql.SQLException DOCUMENT ME! */ protected synchronized int executeUpdate(byte[][] batchedParameterStrings, InputStream[] batchedParameterStreams, boolean[] batchedIsStream, int[] batchedStreamLengths, boolean[] batchedIsNull) throws SQLException { if (connection.isReadOnly()) { throw new SQLException("Connection is read-only. " + "Queries leading to data modification are not allowed", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } checkClosed(); if ((this.firstCharOfStmt == 'S') && StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "SELECT")) { throw new java.sql.SQLException("Can not issue executeUpdate() for SELECTs", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } ResultSet rs = null; // The checking and changing of catalogs // must happen in sequence, so synchronize // on the same mutex that _conn is using synchronized (connection.getMutex()) { Buffer sendPacket = fillSendPacket(batchedParameterStrings, batchedParameterStreams, batchedIsStream, batchedStreamLengths); String oldCatalog = null; if (!this.connection.getCatalog().equals(currentCatalog)) { oldCatalog = this.connection.getCatalog(); this.connection.setCatalog(currentCatalog); } // // Only apply max_rows to selects // if (this.connection.useMaxRows()) { this.connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, this.currentCatalog); } boolean oldInfoMsgState = false; if (this.retrieveGeneratedKeys) { oldInfoMsgState = this.connection.isReadInfoMsgEnabled(); this.connection.setReadInfoMsgEnabled(true); } rs = this.connection.execSQL(null, -1, sendPacket, resultSetConcurrency, false, false, this.currentCatalog); if (this.retrieveGeneratedKeys) { this.connection.setReadInfoMsgEnabled(oldInfoMsgState); } if (oldCatalog != null) { this.connection.setCatalog(oldCatalog); } } this.results = rs; rs.setFirstCharOfQuery(this.firstCharOfStmt); updateCount = rs.getUpdateCount(); int truncatedUpdateCount = 0; if (updateCount > Integer.MAX_VALUE) { truncatedUpdateCount = Integer.MAX_VALUE; } else { truncatedUpdateCount = (int) updateCount; } lastInsertId = rs.getUpdateID(); this.results = rs; return truncatedUpdateCount; } byte[] getBytes(int parameterIndex) throws SQLException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -