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

📄 resultsetmetadata.java

📁 开发MySql数据库的最新JDBC驱动。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* 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 java.io.ByteArrayInputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.sql.SQLException;import java.sql.Types;/** * A ResultSetMetaData object can be used to find out about the types and * properties of the columns in a ResultSet *  * @author Mark Matthews * @version $Id: ResultSetMetaData.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews *          Exp $ *  * @see java.sql.ResultSetMetaData */public class ResultSetMetaData implements java.sql.ResultSetMetaData {	private static int clampedGetLength(Field f) {		long fieldLength = f.getLength();		if (fieldLength > Integer.MAX_VALUE) {			fieldLength = Integer.MAX_VALUE;		}		return (int) fieldLength;	}	/**	 * Checks if the SQL Type is a Decimal/Number Type	 * 	 * @param type	 *            SQL Type	 * 	 * @return ...	 */	private static final boolean isDecimalType(int type) {		switch (type) {		case Types.BIT:		case Types.TINYINT:		case Types.SMALLINT:		case Types.INTEGER:		case Types.BIGINT:		case Types.FLOAT:		case Types.REAL:		case Types.DOUBLE:		case Types.NUMERIC:		case Types.DECIMAL:			return true;		}		return false;	}	Field[] fields;	boolean useOldAliasBehavior = false;		/**	 * Initialise for a result with a tuple set and a field descriptor set	 * 	 * @param fields	 *            the array of field descriptors	 */	public ResultSetMetaData(Field[] fields, boolean useOldAliasBehavior) {		this.fields = fields;		this.useOldAliasBehavior = useOldAliasBehavior;	}	/**	 * What's a column's table's catalog name?	 * 	 * @param column	 *            the first column is 1, the second is 2...	 * 	 * @return catalog name, or "" if not applicable	 * 	 * @throws SQLException	 *             if a database access error occurs	 */	public String getCatalogName(int column) throws SQLException {		Field f = getField(column);		String database = f.getDatabaseName();		return (database == null) ? "" : database; //$NON-NLS-1$	}	/**	 * What's the Java character encoding name for the given column?	 * 	 * @param column	 *            the first column is 1, the second is 2, etc.	 * 	 * @return the Java character encoding name for the given column, or null if	 *         no Java character encoding maps to the MySQL character set for	 *         the given column.	 * 	 * @throws SQLException	 *             if an invalid column index is given.	 */	public String getColumnCharacterEncoding(int column) throws SQLException {		String mysqlName = getColumnCharacterSet(column);		String javaName = null;		if (mysqlName != null) {			javaName = CharsetMapping.getJavaEncodingForMysqlEncoding(					mysqlName, null);		}		return javaName;	}	/**	 * What's the MySQL character set name for the given column?	 * 	 * @param column	 *            the first column is 1, the second is 2, etc.	 * 	 * @return the MySQL character set name for the given column	 * 	 * @throws SQLException	 *             if an invalid column index is given.	 */	public String getColumnCharacterSet(int column) throws SQLException {		return getField(column).getCharacterSet();	}	// --------------------------JDBC 2.0-----------------------------------	/**	 * JDBC 2.0	 * 	 * <p>	 * Return the fully qualified name of the Java class whose instances are	 * manufactured if ResultSet.getObject() is called to retrieve a value from	 * the column. ResultSet.getObject() may return a subClass of the class	 * returned by this method.	 * </p>	 * 	 * @param column	 *            the column number to retrieve information for	 * 	 * @return the fully qualified name of the Java class whose instances are	 *         manufactured if ResultSet.getObject() is called to retrieve a	 *         value from the column.	 * 	 * @throws SQLException	 *             if an error occurs	 */	public String getColumnClassName(int column) throws SQLException {		Field f = getField(column);		return getClassNameForJavaType(f.getSQLType(), 				f.isUnsigned(), 				f.getMysqlType(), 				f.isBinary() || f.isBlob(),				f.isOpaqueBinary()); 	}	/**	 * Whats the number of columns in the ResultSet?	 * 	 * @return the number	 * 	 * @throws SQLException	 *             if a database access error occurs	 */	public int getColumnCount() throws SQLException {		return this.fields.length;	}	/**	 * What is the column's normal maximum width in characters?	 * 	 * @param column	 *            the first column is 1, the second is 2, etc.	 * 	 * @return the maximum width	 * 	 * @throws SQLException	 *             if a database access error occurs	 */	public int getColumnDisplaySize(int column) throws SQLException {		Field f = getField(column);		int lengthInBytes = clampedGetLength(f);		return lengthInBytes / f.getMaxBytesPerCharacter();	}	/**	 * What is the suggested column title for use in printouts and displays?	 * 	 * @param column	 *            the first column is 1, the second is 2, etc.	 * 	 * @return the column label	 * 	 * @throws SQLException	 *             if a database access error occurs	 */	public String getColumnLabel(int column) throws SQLException {		if (this.useOldAliasBehavior) {			return getColumnName(column);		}				return getField(column).getColumnLabel();	}	/**	 * What's a column's name?	 * 	 * @param column	 *            the first column is 1, the second is 2, etc.	 * 	 * @return the column name	 * 	 * @throws SQLException	 *             if a databvase access error occurs	 */	public String getColumnName(int column) throws SQLException {		if (this.useOldAliasBehavior) {			return getField(column).getName();		}				String name = getField(column).getNameNoAliases();				if (name != null && name.length() == 0) {			return getField(column).getName();		}				return name;	}	/**	 * What is a column's SQL Type? (java.sql.Type int)	 * 	 * @param column	 *            the first column is 1, the second is 2, etc.	 * 	 * @return the java.sql.Type value	 * 	 * @throws SQLException	 *             if a database access error occurs	 * 	 * @see java.sql.Types	 */	public int getColumnType(int column) throws SQLException {		return getField(column).getSQLType();	}	/**	 * Whats is the column's data source specific type name?	 * 	 * @param column	 *            the first column is 1, the second is 2, etc.	 * 	 * @return the type name	 * 	 * @throws SQLException	 *             if a database access error occurs	 */	public String getColumnTypeName(int column) throws java.sql.SQLException {		Field field = getField(column);		int mysqlType = field.getMysqlType();		int jdbcType = field.getSQLType();		switch (mysqlType) {		case MysqlDefs.FIELD_TYPE_BIT:			return "BIT";		case MysqlDefs.FIELD_TYPE_DECIMAL:		case MysqlDefs.FIELD_TYPE_NEW_DECIMAL:			return field.isUnsigned() ? "DECIMAL UNSIGNED" : "DECIMAL";		case MysqlDefs.FIELD_TYPE_TINY:			return field.isUnsigned() ? "TINYINT UNSIGNED" : "TINYINT";		case MysqlDefs.FIELD_TYPE_SHORT:			return field.isUnsigned() ? "SMALLINT UNSIGNED" : "SMALLINT";		case MysqlDefs.FIELD_TYPE_LONG:			return field.isUnsigned() ? "INTEGER UNSIGNED" : "INTEGER";		case MysqlDefs.FIELD_TYPE_FLOAT:			return field.isUnsigned() ? "FLOAT UNSIGNED" : "FLOAT";		case MysqlDefs.FIELD_TYPE_DOUBLE:			return field.isUnsigned() ? "DOUBLE UNSIGNED" : "DOUBLE";		case MysqlDefs.FIELD_TYPE_NULL:			return "NULL"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_TIMESTAMP:			return "TIMESTAMP"; //$NON-NLS-1$       		case MysqlDefs.FIELD_TYPE_LONGLONG:			return field.isUnsigned() ? "BIGINT UNSIGNED" : "BIGINT";		case MysqlDefs.FIELD_TYPE_INT24:			return field.isUnsigned() ? "MEDIUMINT UNSIGNED" : "MEDIUMINT";		case MysqlDefs.FIELD_TYPE_DATE:			return "DATE"; //$NON-NLS-1$       		case MysqlDefs.FIELD_TYPE_TIME:			return "TIME"; //$NON-NLS-1$       		case MysqlDefs.FIELD_TYPE_DATETIME:			return "DATETIME"; //$NON-NLS-1$      		case MysqlDefs.FIELD_TYPE_TINY_BLOB:			return "TINYBLOB"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:			return "MEDIUMBLOB"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_LONG_BLOB:			return "LONGBLOB"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_BLOB:			if (getField(column).isBinary()) {				return "BLOB";//$NON-NLS-1$			}			return "TEXT";//$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_VARCHAR:			return "VARCHAR"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_VAR_STRING:			if (jdbcType == Types.VARBINARY) {				return "VARBINARY";			}						return "VARCHAR"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_STRING:			if (jdbcType == Types.BINARY) {				return "BINARY";			}						return "CHAR"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_ENUM:			return "ENUM"; //$NON-NLS-1$		case MysqlDefs.FIELD_TYPE_YEAR:			return "YEAR"; // $NON_NLS-1$		case MysqlDefs.FIELD_TYPE_SET:			return "SET"; //$NON-NLS-1$		default:			return "UNKNOWN"; //$NON-NLS-1$		}	}	/**	 * Returns the field instance for the given column index	 * 	 * @param columnIndex	 *            the column number to retrieve a field instance for	 * 	 * @return the field instance for the given column index	 * 	 * @throws SQLException	 *             if an error occurs	 */	protected Field getField(int columnIndex) throws SQLException {		if ((columnIndex < 1) || (columnIndex > this.fields.length)) {			throw SQLError.createSQLException(Messages.getString("ResultSetMetaData.46"), //$NON-NLS-1$					SQLError.SQL_STATE_INVALID_COLUMN_NUMBER);		}		return this.fields[columnIndex - 1];	}	/**

⌨️ 快捷键说明

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