?? preparedstatement.java
字號:
* the catalog in use when we were created * * @throws SQLException * if an error occurs */ protected PreparedStatement(Connection conn, String catalog) throws SQLException { super(conn, catalog); } /** * Constructor for the PreparedStatement class. * * @param conn * the connection creating this statement * @param sql * the SQL for this statement * @param catalog * the catalog/database this statement should be issued against * * @throws SQLException * if a database error occurs. */ public PreparedStatement(Connection conn, String sql, String catalog) throws SQLException { super(conn, catalog); if (sql == null) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.0"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.originalSql = sql; if (this.originalSql.startsWith(PING_MARKER)) { this.doPingInstead = true; } else { this.doPingInstead = false; } this.dbmd = this.connection.getMetaData(); this.useTrueBoolean = this.connection.versionMeetsMinimum(3, 21, 23); this.parseInfo = new ParseInfo(sql, this.connection, this.dbmd, this.charEncoding, this.charConverter); initializeFromParseInfo(); } /** * Creates a new PreparedStatement object. * * @param conn * the connection creating this statement * @param sql * the SQL for this statement * @param catalog * the catalog/database this statement should be issued against * @param cachedParseInfo * already created parseInfo. * * @throws SQLException * DOCUMENT ME! */ public PreparedStatement(Connection conn, String sql, String catalog, ParseInfo cachedParseInfo) throws SQLException { super(conn, catalog); if (sql == null) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.1"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.originalSql = sql; this.dbmd = this.connection.getMetaData(); this.useTrueBoolean = this.connection.versionMeetsMinimum(3, 21, 23); this.parseInfo = cachedParseInfo; this.usingAnsiMode = !this.connection.useAnsiQuotedIdentifiers(); initializeFromParseInfo(); } /** * JDBC 2.0 Add a set of parameters to the batch. * * @exception SQLException * if a database-access error occurs. * * @see Statement#addBatch */ public void addBatch() throws SQLException { if (this.batchedArgs == null) { this.batchedArgs = new ArrayList(); } this.batchedArgs.add(new BatchParams(this.parameterValues, this.parameterStreams, this.isStream, this.streamLengths, this.isNull)); } public synchronized void addBatch(String sql) throws SQLException { this.batchHasPlainStatements = true; super.addBatch(sql); } protected String asSql() throws SQLException { return asSql(false); } protected String asSql(boolean quoteStreamsAndUnknowns) throws SQLException { if (this.isClosed) { return "statement has been closed, no further internal information available"; } 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]) { if (quoteStreamsAndUnknowns) { buf.append("'"); } buf.append("** NOT SPECIFIED **"); //$NON-NLS-1$ if (quoteStreamsAndUnknowns) { buf.append("'"); } } else if (this.isStream[i]) { if (quoteStreamsAndUnknowns) { buf.append("'"); } buf.append("** STREAM DATA **"); //$NON-NLS-1$ if (quoteStreamsAndUnknowns) { buf.append("'"); } } 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)); } else { buf.append(StringUtils .toAsciiString(this.parameterValues[i])); } } } } if (this.charEncoding != null) { buf.append(new String( this.staticSqlStrings[this.parameterCount], this.charEncoding)); } else { buf .append(StringUtils .toAsciiString(this.staticSqlStrings[this.parameterCount])); } } catch (UnsupportedEncodingException uue) { throw new RuntimeException(Messages .getString("PreparedStatement.32") //$NON-NLS-1$ + this.charEncoding + Messages.getString("PreparedStatement.33")); //$NON-NLS-1$ } return buf.toString(); } public synchronized void clearBatch() throws SQLException { this.batchHasPlainStatements = false; super.clearBatch(); } /** * In general, parameter values remain in force for repeated used of a * Statement. Setting a parameter value automatically clears its previous * value. However, in some cases, it is useful to immediately release the * resources used by the current parameter values; this can be done by * calling clearParameters * * @exception SQLException * if a database access error occurs */ public synchronized void clearParameters() throws SQLException { checkClosed(); for (int i = 0; i < this.parameterValues.length; i++) { this.parameterValues[i] = null; this.parameterStreams[i] = null; this.isStream[i] = false; this.isNull[i] = false; } } /** * Closes this prepared statement and releases all resources. * * @throws SQLException * if database error occurs. */ public synchronized void close() throws SQLException { realClose(true, true); } private final void escapeblockFast(byte[] buf, Buffer packet, int size) throws SQLException { int lastwritten = 0; for (int i = 0; i < size; i++) { byte b = buf[i]; if (b == '\0') { // write stuff not yet written if (i > lastwritten) { packet.writeBytesNoNull(buf, lastwritten, i - lastwritten); } // write escape packet.writeByte((byte) '\\'); packet.writeByte((byte) '0'); lastwritten = i + 1; } else { if ((b == '\\') || (b == '\'') || (!this.usingAnsiMode && b == '"')) { // write stuff not yet written if (i > lastwritten) { packet.writeBytesNoNull(buf, lastwritten, i - lastwritten); } // write escape packet.writeByte((byte) '\\'); lastwritten = i; // not i+1 as b wasn't written. } } } // write out remaining stuff from buffer if (lastwritten < size) { packet.writeBytesNoNull(buf, lastwritten, size - lastwritten); } } private final void escapeblockFast(byte[] buf, ByteArrayOutputStream bytesOut, int size) { int lastwritten = 0; for (int i = 0; i < size; i++) { byte b = buf[i]; if (b == '\0') { // write stuff not yet written if (i > lastwritten) { bytesOut.write(buf, lastwritten, i - lastwritten); } // write escape bytesOut.write('\\'); bytesOut.write('0'); lastwritten = i + 1; } else { if ((b == '\\') || (b == '\'') || (!this.usingAnsiMode && b == '"')) { // write stuff not yet written if (i > lastwritten) { bytesOut.write(buf, lastwritten, i - lastwritten); } // write escape bytesOut.write('\\'); lastwritten = i; // not i+1 as b wasn't written. } } } // write out remaining stuff from buffer if (lastwritten < size) { bytesOut.write(buf, lastwritten, size - lastwritten); } } /** * Some prepared statements return multiple results; the execute method * handles these complex statements as well as the simpler form of * statements handled by executeQuery and executeUpdate * * @return true if the next result is a ResultSet; false if it is an update * count or there are no more results * * @exception SQLException * if a database access error occurs */ public boolean execute() throws SQLException { checkClosed(); Connection locallyScopedConn = this.connection; if (locallyScopedConn.isReadOnly() && (this.firstCharOfStmt != 'S')) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.20") //$NON-NLS-1$ + Messages.getString("PreparedStatement.21"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } ResultSet rs = null; CachedResultSetMetaData cachedMetadata = null; synchronized (locallyScopedConn.getMutex()) { clearWarnings(); this.batchedGeneratedKeys = null; Buffer sendPacket = fillSendPacket(); String oldCatalog = null; if (!locallyScopedConn.getCatalog().equals(this.currentCatalog)) { oldCatalog = locallyScopedConn.getCatalog(); locallyScopedConn.setCatalog(this.currentCatalog); } // // Check if we have cached metadata for this query... // if (locallyScopedConn.getCacheResultSetMetadata()) { cachedMetadata = locallyScopedConn.getCachedMetaData(this.originalSql); } Field[] metadataFromCache = null; if (cachedMetadata != null) { metadataFromCache = cachedMetadata.fields; } boolean oldInfoMsgState = false; if (this.retrieveGeneratedKeys) { oldInfoMsgState = locallyScopedConn.isReadInfoMsgEnabled(); locallyScopedConn.setReadInfoMsgEnabled(true); } // 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). // // Only apply max_rows to selects // if (locallyScopedConn.useMaxRows()) { int rowLimit = -1; if (this.firstCharOfStmt == 'S') { if (this.hasLimitClause) { rowLimit = this.maxRows; } else { if (this.maxRows <= 0) { locallyScopedConn.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, this.currentCatalog, true); } else { locallyScopedConn .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, this.currentCatalog, true); } } } else { locallyScopedConn.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, this.currentCatalog, true); } // Finally, execute the query rs = executeInternal(rowLimit, sendPacket, createStreamingResultSet(), (this.firstCharOfStmt == 'S'), true, metadataFromCache, false); } else { rs = executeInternal(-1, sendPacket, createStreamingResultSet(), (this.firstCharOfStmt == 'S'), true, metadataFromCache, false); } if (cachedMetadata != null) { locallyScopedConn.initializeResultsMetadataFromCache(this.originalSql, cachedMetadata, this.results); } else { if (rs.reallyResult() && locallyScopedConn.getCacheResultSetMetadata()) { locallyScopedConn.initializeResultsMetadataFromCache(this.originalSql, null /* will be created */, rs); } } if (this.retrieveGeneratedKeys) { locallyScopedConn.setReadInfoMsgEnabled(oldInfoMsgState); rs.setFirstCharOfQuery(this.firstCharOfStmt); } if (oldCatalog != null) { locallyScopedConn.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 { checkClosed(); 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); } synchronized (this.connection.getMutex()) { if (this.batchedArgs == null || this.batchedArgs.size() == 0) { return new int[0]; } try { clearWarnings(); if (!this.batchHasPlainStatements && this.connection.getRewriteBatchedStatements()) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -