📄 preparedstatement.java
字号:
/* Copyright (C) 2002-2004 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.mysql.jdbc;import com.mysql.jdbc.profiler.ProfilerEvent;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectOutputStream;import java.io.Reader;import java.io.StringReader;import java.io.UnsupportedEncodingException;import java.math.BigDecimal;import java.net.URL;import java.sql.Array;import java.sql.Clob;import java.sql.ParameterMetaData;import java.sql.Ref;import java.sql.SQLException;import java.sql.Time;import java.sql.Timestamp;import java.sql.Types;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Locale;import java.util.TimeZone;/** * A SQL Statement is pre-compiled and stored in a PreparedStatement object. * This object can then be used to efficiently execute this statement multiple * times. * * <p> * <B>Note:</B> The setXXX methods for setting IN parameter values must specify * types that are compatible with the defined SQL type of the input parameter. * For instance, if the IN parameter has SQL type Integer, then setInt should * be used. * </p> * * <p> * If arbitrary parameter type conversions are required, then the setObject * method should be used with a target SQL type. * </p> * * @author Mark Matthews * @version $Id: PreparedStatement.java,v 1.27.4.58 2005/02/14 16:44:04 mmatthews Exp $ * * @see java.sql.ResultSet * @see java.sql.PreparedStatement */public class PreparedStatement extends com.mysql.jdbc.Statement implements java.sql.PreparedStatement { protected ArrayList batchedGeneratedKeys = null; /** The SQL that was passed in to 'prepare' */ protected String originalSql = null; /** Does the SQL for this statement contain a 'limit' clause? */ protected boolean hasLimitClause = false; /** Is this query a LOAD DATA query? */ protected boolean isLoadDataQuery = false; protected boolean retrieveGeneratedKeys = false; /** * Are we using a version of MySQL where we can use 'true' boolean values? */ protected boolean useTrueBoolean = false; /** * What is the first character of the prepared statement (used to check for * SELECT vs. INSERT/UPDATE/DELETE) */ protected char firstCharOfStmt = 0; protected int numberOfExecutions = 0; /** The number of parameters in this PreparedStatement */ protected int parameterCount; private java.sql.DatabaseMetaData dbmd = null; private ParseInfo parseInfo; private java.sql.ResultSetMetaData pstmtResultMetaData; private SimpleDateFormat tsdf = null; private boolean[] isNull = null; private boolean[] isStream = null; private InputStream[] parameterStreams = null; private byte[][] parameterValues = null; private byte[][] staticSqlStrings = null; private byte[] streamConvertBuf = new byte[4096]; private int[] streamLengths = null; private boolean usingAnsiMode; private final static byte[] HEX_DIGITS = new byte[] { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' }; /** * 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 new SQLException(Messages.getString("PreparedStatement.0"), //$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 = 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 new SQLException(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(); } /** * 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 */ protected PreparedStatement(Connection conn, String catalog) throws SQLException { super(conn, catalog); } /** * JDBC 2.0 Set an Array parameter. * * @param i the first parameter is 1, the second is 2, ... * @param x an object representing an SQL array * * @throws SQLException because this method is not implemented. * @throws NotImplemented DOCUMENT ME! */ public void setArray(int i, Array x) throws SQLException { throw new NotImplemented(); } /** * When a very large ASCII value is input to a LONGVARCHAR parameter, it * may be more practical to send it via a java.io.InputStream. JDBC will * read the data from the stream as needed, until it reaches end-of-file. * The JDBC driver will do any necessary conversion from ASCII to the * database char format. * * <P> * <B>Note:</B> This stream object can either be a standard Java stream * object or your own subclass that implements the standard interface. * </p> * * @param parameterIndex the first parameter is 1... * @param x the parameter value * @param length the number of bytes in the stream * * @exception SQLException if a database access error occurs */ public synchronized void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.VARCHAR); } else { setBinaryStream(parameterIndex, x, length); } } /** * Set a parameter to a java.math.BigDecimal value. The driver converts * this to a SQL NUMERIC value when it sends it to the database. * * @param parameterIndex the first parameter is 1... * @param x the parameter value * * @exception SQLException if a database access error occurs */ public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.DECIMAL); } else { setInternal(parameterIndex, StringUtils.fixDecimalExponent(StringUtils.consistentToString(x))); } } /** * When a very large binary value is input to a LONGVARBINARY parameter, it * may be more practical to send it via a java.io.InputStream. JDBC will * read the data from the stream as needed, until it reaches end-of-file. * * <P> * <B>Note:</B> This stream object can either be a standard Java stream * object or your own subclass that implements the standard interface. * </p> * * @param parameterIndex the first parameter is 1... * @param x the parameter value * @param length the number of bytes to read from the stream (ignored) * * @throws SQLException if a database access error occurs */ public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { if ((parameterIndex < 1) || (parameterIndex > this.staticSqlStrings.length)) { throw new SQLException(Messages.getString("PreparedStatement.2") //$NON-NLS-1$ + parameterIndex + Messages.getString("PreparedStatement.3") + this.staticSqlStrings.length + Messages.getString("PreparedStatement.4"), //$NON-NLS-1$ //$NON-NLS-2$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.parameterStreams[parameterIndex - 1] = x; this.isStream[parameterIndex - 1] = true; this.streamLengths[parameterIndex - 1] = length; this.isNull[parameterIndex - 1] = false; } } /** * JDBC 2.0 Set a BLOB parameter. * * @param i the first parameter is 1, the second is 2, ... * @param x an object representing a BLOB * * @throws SQLException if a database error occurs */ public void setBlob(int i, java.sql.Blob x) throws SQLException { if (x == null) { setNull(i, Types.BLOB); } else { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); bytesOut.write('\''); escapeblockFast(x.getBytes(1, (int) x.length()), bytesOut, (int) x.length()); bytesOut.write('\''); setInternal(i, bytesOut.toByteArray()); } } /** * Set a parameter to a Java boolean value. The driver converts this to a * SQL BIT value when it sends it to the database. * * @param parameterIndex the first parameter is 1... * @param x the parameter value * * @throws SQLException if a database access error occurs */ public void setBoolean(int parameterIndex, boolean x) throws SQLException { if (this.useTrueBoolean) { setInternal(parameterIndex, x ? "'1'" : "'0'"); //$NON-NLS-1$ //$NON-NLS-2$ } else { setInternal(parameterIndex, x ? "'t'" : "'f'"); //$NON-NLS-1$ //$NON-NLS-2$ } } /** * Set a parameter to a Java byte value. The driver converts this to a SQL * TINYINT value when it sends it to the database. * * @param parameterIndex the first parameter is 1... * @param x the parameter value * * @exception SQLException if a database access error occurs */ public void setByte(int parameterIndex, byte x) throws SQLException { setInternal(parameterIndex, String.valueOf(x)); } /** * Set a parameter to a Java array of bytes. The driver converts this to a * SQL VARBINARY or LONGVARBINARY (depending on the argument's size * relative to the driver's limits on VARBINARYs) when it sends it to the * database. * * @param parameterIndex the first parameter is 1... * @param x the parameter value * * @exception SQLException if a database access error occurs */ public void setBytes(int parameterIndex, byte[] x) throws SQLException { setBytes(parameterIndex, x, true, true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -