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

📄 field.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright  2002-2004 MySQL AB, 2008 Sun Microsystems 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 java.io.UnsupportedEncodingException;import java.sql.SQLException;import java.sql.Types;import java.util.regex.PatternSyntaxException;/** * Field is a class used to describe fields in a ResultSet * * @author Mark Matthews * @version $Id$ */public class Field {	private static final int AUTO_INCREMENT_FLAG = 512;	private static final int NO_CHARSET_INFO = -1;	private byte[] buffer;	private int charsetIndex = 0;	private String charsetName = null;	private int colDecimals;	private short colFlag;	private String collationName = null;	private ConnectionImpl connection = null;	private String databaseName = null;	private int databaseNameLength = -1;	// database name info	private int databaseNameStart = -1;	private int defaultValueLength = -1;	// default value info - from COM_LIST_FIELDS execution	private int defaultValueStart = -1;	private String fullName = null;	private String fullOriginalName = null;	private boolean isImplicitTempTable = false;	private long length; // Internal length of the field;	private int mysqlType = -1; // the MySQL type	private String name; // The Field name	private int nameLength;	private int nameStart;	private String originalColumnName = null;	private int originalColumnNameLength = -1;	// column name info (before aliasing)	private int originalColumnNameStart = -1;	private String originalTableName = null;	private int originalTableNameLength = -1;	// table name info (before aliasing)	private int originalTableNameStart = -1;	private int precisionAdjustFactor = 0;	private int sqlType = -1; // the java.sql.Type	private String tableName; // The Name of the Table	private int tableNameLength;	private int tableNameStart;	private boolean useOldNameMetadata = false;	private boolean isSingleBit;	private int maxBytesPerChar;	/**	 * Constructor used when communicating with 4.1 and newer servers	 */	Field(ConnectionImpl conn, byte[] buffer, int databaseNameStart,			int databaseNameLength, int tableNameStart, int tableNameLength,			int originalTableNameStart, int originalTableNameLength,			int nameStart, int nameLength, int originalColumnNameStart,			int originalColumnNameLength, long length, int mysqlType,			short colFlag, int colDecimals, int defaultValueStart,			int defaultValueLength, int charsetIndex) throws SQLException {		this.connection = conn;		this.buffer = buffer;		this.nameStart = nameStart;		this.nameLength = nameLength;		this.tableNameStart = tableNameStart;		this.tableNameLength = tableNameLength;		this.length = length;		this.colFlag = colFlag;		this.colDecimals = colDecimals;		this.mysqlType = mysqlType;		// 4.1 field info...		this.databaseNameStart = databaseNameStart;		this.databaseNameLength = databaseNameLength;		this.originalTableNameStart = originalTableNameStart;		this.originalTableNameLength = originalTableNameLength;		this.originalColumnNameStart = originalColumnNameStart;		this.originalColumnNameLength = originalColumnNameLength;		this.defaultValueStart = defaultValueStart;		this.defaultValueLength = defaultValueLength;		// If we're not running 4.1 or newer, use the connection's		// charset		this.charsetIndex = charsetIndex;		// Map MySqlTypes to java.sql Types		this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType);        checkForImplicitTemporaryTable();		// Re-map to 'real' blob type, if we're a BLOB		if (this.mysqlType == MysqlDefs.FIELD_TYPE_BLOB) {		    boolean isFromFunction = this.originalTableNameLength == 0;		    if (this.connection != null && this.connection.getBlobsAreStrings() ||		            (this.connection.getFunctionsNeverReturnBlobs() && isFromFunction)) {		        this.sqlType = Types.VARCHAR;		        this.mysqlType = MysqlDefs.FIELD_TYPE_VARCHAR;		    } else if (this.charsetIndex == 63 ||					!this.connection.versionMeetsMinimum(4, 1, 0)) {				if (this.connection.getUseBlobToStoreUTF8OutsideBMP() 						&& shouldSetupForUtf8StringInBlob()) {					setupForUtf8StringInBlob();				} else {					setBlobTypeBasedOnLength();					this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType);				}			} else {				// *TEXT masquerading as blob				this.mysqlType = MysqlDefs.FIELD_TYPE_VAR_STRING;				this.sqlType = Types.LONGVARCHAR;			}		}		if (this.sqlType == Types.TINYINT && this.length == 1				&& this.connection.getTinyInt1isBit()) {			// Adjust for pseudo-boolean			if (conn.getTinyInt1isBit()) {				if (conn.getTransformedBitIsBoolean()) {					this.sqlType = Types.BOOLEAN;				} else {					this.sqlType = Types.BIT;				}			}		}		if (!isNativeNumericType() && !isNativeDateTimeType()) {			this.charsetName = this.connection				.getCharsetNameForIndex(this.charsetIndex);			// Handle VARBINARY/BINARY (server doesn't have a different type			// for this			boolean isBinary = isBinary();			if (this.connection.versionMeetsMinimum(4, 1, 0) &&					this.mysqlType == MysqlDefs.FIELD_TYPE_VAR_STRING &&					isBinary &&					this.charsetIndex == 63) {				if (this.isOpaqueBinary()) {					this.sqlType = Types.VARBINARY;				}			}			if (this.connection.versionMeetsMinimum(4, 1, 0) &&					this.mysqlType == MysqlDefs.FIELD_TYPE_STRING &&					isBinary && this.charsetIndex == 63) {				//				// Okay, this is a hack, but there's currently no way				// to easily distinguish something like DATE_FORMAT( ..)				// from the "BINARY" column type, other than looking				// at the original column name.				//				if (isOpaqueBinary() && !this.connection.getBlobsAreStrings()) {					this.sqlType = Types.BINARY;				}			}			if (this.mysqlType == MysqlDefs.FIELD_TYPE_BIT) {				this.isSingleBit = (this.length == 0);				if (this.connection != null && (this.connection.versionMeetsMinimum(5, 0, 21) ||						this.connection.versionMeetsMinimum(5, 1, 10)) && this.length == 1) {					this.isSingleBit = true;				}				if (this.isSingleBit) {					this.sqlType = Types.BIT;				} else {					this.sqlType = Types.VARBINARY;					this.colFlag |= 128; // we need to pretend this is a full					this.colFlag |= 16; // binary blob					isBinary = true;				}			}			//			// Handle TEXT type (special case), Fix proposed by Peter McKeown			//			if ((this.sqlType == java.sql.Types.LONGVARBINARY) && !isBinary) {				this.sqlType = java.sql.Types.LONGVARCHAR;			} else if ((this.sqlType == java.sql.Types.VARBINARY) && !isBinary) {				this.sqlType = java.sql.Types.VARCHAR;			}		} else {			this.charsetName = "US-ASCII";		}		//		// Handle odd values for 'M' for floating point/decimal numbers		//		if (!isUnsigned()) {			switch (this.mysqlType) {			case MysqlDefs.FIELD_TYPE_DECIMAL:			case MysqlDefs.FIELD_TYPE_NEW_DECIMAL:				this.precisionAdjustFactor = -1;				break;			case MysqlDefs.FIELD_TYPE_DOUBLE:			case MysqlDefs.FIELD_TYPE_FLOAT:				this.precisionAdjustFactor = 1;				break;			}		} else {			switch (this.mysqlType) {			case MysqlDefs.FIELD_TYPE_DOUBLE:			case MysqlDefs.FIELD_TYPE_FLOAT:				this.precisionAdjustFactor = 1;				break;			}		}	}	private boolean shouldSetupForUtf8StringInBlob() throws SQLException {		String includePattern = this.connection				.getUtf8OutsideBmpIncludedColumnNamePattern();		String excludePattern = this.connection				.getUtf8OutsideBmpExcludedColumnNamePattern();		if (excludePattern != null				&& !StringUtils.isEmptyOrWhitespaceOnly(excludePattern)) {			try {				if (getOriginalName().matches(excludePattern)) {					if (includePattern != null							&& !StringUtils.isEmptyOrWhitespaceOnly(includePattern)) {						try {							if (getOriginalName().matches(includePattern)) {								return true;							}						} catch (PatternSyntaxException pse) {							SQLException sqlEx = SQLError									.createSQLException(											"Illegal regex specified for \"utf8OutsideBmpIncludedColumnNamePattern\"",											SQLError.SQL_STATE_ILLEGAL_ARGUMENT);							if (!this.connection.getParanoid()) {								sqlEx.initCause(pse);							}							throw sqlEx;						}					}										return false;				}			} catch (PatternSyntaxException pse) {				SQLException sqlEx = SQLError						.createSQLException(								"Illegal regex specified for \"utf8OutsideBmpExcludedColumnNamePattern\"",								SQLError.SQL_STATE_ILLEGAL_ARGUMENT);				if (!this.connection.getParanoid()) {					sqlEx.initCause(pse);				}				throw sqlEx;			}		}		return true;	}	private void setupForUtf8StringInBlob() {		if (this.length == MysqlDefs.LENGTH_TINYBLOB || this.length == MysqlDefs.LENGTH_BLOB) {			this.mysqlType = MysqlDefs.FIELD_TYPE_VARCHAR;			this.sqlType = Types.VARCHAR;		}  else {			this.mysqlType = MysqlDefs.FIELD_TYPE_VAR_STRING;			this.sqlType = Types.LONGVARCHAR;		}				this.charsetIndex = 33;		}	/**	 * Constructor used when communicating with pre 4.1 servers	 */	Field(ConnectionImpl conn, byte[] buffer, int nameStart, int nameLength,			int tableNameStart, int tableNameLength, int length, int mysqlType,			short colFlag, int colDecimals) throws SQLException {		this(conn, buffer, -1, -1, tableNameStart, tableNameLength, -1, -1,				nameStart, nameLength, -1, -1, length, mysqlType, colFlag,				colDecimals, -1, -1, NO_CHARSET_INFO);	}	/**	 * Constructor used by DatabaseMetaData methods.	 */	Field(String tableName, String columnName, int jdbcType, int length) {		this.tableName = tableName;		this.name = columnName;		this.length = length;		this.sqlType = jdbcType;		this.colFlag = 0;		this.colDecimals = 0;	}		/**	 * Used by prepared statements to re-use result set data conversion methods	 * when generating bound parmeter retrieval instance for statement	 * interceptors.	 * 	 * @param tableName	 *            not used	 * @param columnName	 *            not used	 * @param charsetIndex	 *            the MySQL collation/character set index	 * @param jdbcType	 *            from java.sql.Types	 * @param length	 *            length in characters or bytes (for BINARY data).	 */	Field(String tableName, String columnName, int charsetIndex, int jdbcType,			int length) {		this.tableName = tableName;		this.name = columnName;		this.length = length;		this.sqlType = jdbcType;		this.colFlag = 0;		this.colDecimals = 0;		this.charsetIndex = charsetIndex;	}		private void checkForImplicitTemporaryTable() {		this.isImplicitTempTable = this.tableNameLength > 5				&& this.buffer[tableNameStart] == (byte) '#'				&& this.buffer[tableNameStart + 1] == (byte) 's'				&& this.buffer[tableNameStart + 2] == (byte) 'q'				&& this.buffer[tableNameStart + 3] == (byte) 'l'				&& this.buffer[tableNameStart + 4] == (byte) '_';	}	/**	 * Returns the character set (if known) for this field.	 *	 * @return the character set	 */	public String getCharacterSet() throws SQLException {		return this.charsetName;	}	public void setCharacterSet(String javaEncodingName) throws SQLException {		this.charsetName = javaEncodingName;		this.charsetIndex = CharsetMapping				.getCharsetIndexForMysqlEncodingName(javaEncodingName);	}		public synchronized String getCollation() throws SQLException {		if (this.collationName == null) {			if (this.connection != null) {				if (this.connection.versionMeetsMinimum(4, 1, 0)) {					if (this.connection.getUseDynamicCharsetInfo()) {						java.sql.DatabaseMetaData dbmd = this.connection								.getMetaData();						String quotedIdStr = dbmd.getIdentifierQuoteString();						if (" ".equals(quotedIdStr)) { //$NON-NLS-1$							quotedIdStr = ""; //$NON-NLS-1$						}						String csCatalogName = getDatabaseName();						String csTableName = getOriginalTableName();						String csColumnName = getOriginalName();						if (csCatalogName != null && csCatalogName.length() != 0								&& csTableName != null && csTableName.length() != 0								&& csColumnName != null								&& csColumnName.length() != 0) {							StringBuffer queryBuf = new StringBuffer(csCatalogName									.length()									+ csTableName.length() + 28);							queryBuf.append("SHOW FULL COLUMNS FROM "); //$NON-NLS-1$							queryBuf.append(quotedIdStr);							queryBuf.append(csCatalogName);							queryBuf.append(quotedIdStr);							queryBuf.append("."); //$NON-NLS-1$							queryBuf.append(quotedIdStr);							queryBuf.append(csTableName);							queryBuf.append(quotedIdStr);							java.sql.Statement collationStmt = null;							java.sql.ResultSet collationRs = null;							try {								collationStmt = this.connection.createStatement();								collationRs = collationStmt.executeQuery(queryBuf										.toString());								while (collationRs.next()) {									if (csColumnName.equals(collationRs											.getString("Field"))) { //$NON-NLS-1$										this.collationName = collationRs												.getString("Collation"); //$NON-NLS-1$										break;									}								}							} finally {								if (collationRs != null) {									collationRs.close();									collationRs = null;								}								if (collationStmt != null) {									collationStmt.close();									collationStmt = null;								}							}						}					} else {						this.collationName = CharsetMapping.INDEX_TO_COLLATION[charsetIndex];					}				}			}		}		return this.collationName;	}		public String getColumnLabel() throws SQLException {		return getName(); // column name if not aliased, alias if used	}	/**	 * DOCUMENT ME!	 *	 * @return DOCUMENT ME!	 */	public String getDatabaseName() throws SQLException {		if ((this.databaseName == null) && (this.databaseNameStart != -1)				&& (this.databaseNameLength != -1)) {			this.databaseName = getStringFromBytes(this.databaseNameStart,					this.databaseNameLength);		}		return this.databaseName;	}	int getDecimals() {		return this.colDecimals;	}

⌨️ 快捷键说明

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