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

📄 preparedstatement.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	/** Is this query a LOAD DATA query? */	protected boolean isLoadDataQuery = false;	private boolean[] isNull = null;	private boolean[] isStream = null;	protected int numberOfExecutions = 0;	/** The SQL that was passed in to 'prepare' */	protected String originalSql = null;	/** The number of parameters in this PreparedStatement */	protected int parameterCount;	protected MysqlParameterMetadata parameterMetaData;	private InputStream[] parameterStreams = null;	private byte[][] parameterValues = null;	/**	 * Only used by statement interceptors at the moment to	 * provide introspection of bound values	 */	protected int[] parameterTypes = null;		private ParseInfo parseInfo;	private java.sql.ResultSetMetaData pstmtResultMetaData;	private byte[][] staticSqlStrings = null;	private byte[] streamConvertBuf = new byte[4096];	private int[] streamLengths = null;	private SimpleDateFormat tsdf = null;	/**	 * Are we using a version of MySQL where we can use 'true' boolean values?	 */	protected boolean useTrueBoolean = false;	protected boolean usingAnsiMode;	protected String batchedValuesClause;	/** Where does the statement text actually start? */		private int statementAfterCommentsPos;	/**	 * have we checked whether we can rewrite this statement as a multi-value	 * insert?	 */	private boolean hasCheckedForRewrite = false;	/** Can we actually rewrite this statement as a multi-value insert? */	private boolean canRewrite = false;	private boolean doPingInstead;	private SimpleDateFormat ddf;	private SimpleDateFormat tdf;		private boolean compensateForOnDuplicateKeyUpdate = false;		/**	 * Creates a prepared statement instance -- We need to provide factory-style	 * methods so we can support both JDBC3 (and older) and JDBC4 runtimes,	 * otherwise the class verifier complains when it tries to load JDBC4-only	 * interface classes that are present in JDBC4 method signatures.	 */	protected static PreparedStatement getInstance(ConnectionImpl conn,			String catalog) throws SQLException {		if (!Util.isJdbc4()) {			return new PreparedStatement(conn, catalog);		}		return (PreparedStatement) Util.handleNewInstance(				JDBC_4_PSTMT_2_ARG_CTOR, new Object[] { conn, catalog });	}	/**	 * Creates a prepared statement instance -- We need to provide factory-style	 * methods so we can support both JDBC3 (and older) and JDBC4 runtimes,	 * otherwise the class verifier complains when it tries to load JDBC4-only	 * interface classes that are present in JDBC4 method signatures.	 */	protected static PreparedStatement getInstance(ConnectionImpl conn, String sql,			String catalog) throws SQLException {		if (!Util.isJdbc4()) {			return new PreparedStatement(conn, sql, catalog);		}		return (PreparedStatement) Util.handleNewInstance(				JDBC_4_PSTMT_3_ARG_CTOR, new Object[] { conn, sql, catalog });	}	/**	 * Creates a prepared statement instance -- We need to provide factory-style	 * methods so we can support both JDBC3 (and older) and JDBC4 runtimes,	 * otherwise the class verifier complains when it tries to load JDBC4-only	 * interface classes that are present in JDBC4 method signatures.	 */	protected static PreparedStatement getInstance(ConnectionImpl conn, String sql,			String catalog, ParseInfo cachedParseInfo) throws SQLException {		if (!Util.isJdbc4()) {			return new PreparedStatement(conn, sql, catalog, cachedParseInfo);		}		return (PreparedStatement) Util.handleNewInstance(				JDBC_4_PSTMT_4_ARG_CTOR, new Object[] { conn, sql, catalog,						cachedParseInfo });	}		/**	 * Constructor used by server-side prepared statements	 * 	 * @param conn	 *            the connection that created us	 * @param catalog	 *            the catalog in use when we were created	 * 	 * @throws SQLException	 *             if an error occurs	 */	public PreparedStatement(ConnectionImpl conn, String catalog)			throws SQLException {		super(conn, catalog);				this.compensateForOnDuplicateKeyUpdate = this.connection.getCompensateOnDuplicateKeyUpdateCounts();	}	/**	 * 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(ConnectionImpl 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();				this.compensateForOnDuplicateKeyUpdate = this.connection.getCompensateOnDuplicateKeyUpdateCounts();	}	/**	 * 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(ConnectionImpl 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();				this.compensateForOnDuplicateKeyUpdate = this.connection.getCompensateOnDuplicateKeyUpdateCounts();	}	/**	 * JDBC 2.0 Add a set of parameters to the batch.	 * 	 * @exception SQLException	 *                if a database-access error occurs.	 * 	 * @see StatementImpl#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 {			int realParameterCount = this.parameterCount + getParameterIndexOffset();						for (int i = 0; i < realParameterCount; ++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 + getParameterIndexOffset()],						this.charEncoding));			} else {				buf						.append(StringUtils								.toAsciiString(this.staticSqlStrings[this.parameterCount + getParameterIndexOffset()]));			}		} 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;			this.parameterTypes[i] = Types.NULL;		}	}	/**	 * 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);		}	}		/**	 * Check to see if the statement is safe for read-only slaves after failover.	 * 	 * @return true if safe for read-only.	 * @throws SQLException	 */	protected boolean checkReadOnlySafeStatement() throws SQLException {		return ((!this.connection.isReadOnly()) || (this.firstCharOfStmt == 'S'));	}	/**	 * 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();				ConnectionImpl locallyScopedConn = this.connection;

⌨️ 快捷键说明

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