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

📄 databasemetadata.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	 */	public java.sql.ResultSet getAttributes(String arg0, String arg1,			String arg2, String arg3) throws SQLException {		Field[] fields = new Field[21];		fields[0] = new Field("", "TYPE_CAT", Types.CHAR, 32);		fields[1] = new Field("", "TYPE_SCHEM", Types.CHAR, 32);		fields[2] = new Field("", "TYPE_NAME", Types.CHAR, 32);		fields[3] = new Field("", "ATTR_NAME", Types.CHAR, 32);		fields[4] = new Field("", "DATA_TYPE", Types.SMALLINT, 32);		fields[5] = new Field("", "ATTR_TYPE_NAME", Types.CHAR, 32);		fields[6] = new Field("", "ATTR_SIZE", Types.INTEGER, 32);		fields[7] = new Field("", "DECIMAL_DIGITS", Types.INTEGER, 32);		fields[8] = new Field("", "NUM_PREC_RADIX", Types.INTEGER, 32);		fields[9] = new Field("", "NULLABLE ", Types.INTEGER, 32);		fields[10] = new Field("", "REMARKS", Types.CHAR, 32);		fields[11] = new Field("", "ATTR_DEF", Types.CHAR, 32);		fields[12] = new Field("", "SQL_DATA_TYPE", Types.INTEGER, 32);		fields[13] = new Field("", "SQL_DATETIME_SUB", Types.INTEGER, 32);		fields[14] = new Field("", "CHAR_OCTET_LENGTH", Types.INTEGER, 32);		fields[15] = new Field("", "ORDINAL_POSITION", Types.INTEGER, 32);		fields[16] = new Field("", "IS_NULLABLE", Types.CHAR, 32);		fields[17] = new Field("", "SCOPE_CATALOG", Types.CHAR, 32);		fields[18] = new Field("", "SCOPE_SCHEMA", Types.CHAR, 32);		fields[19] = new Field("", "SCOPE_TABLE", Types.CHAR, 32);		fields[20] = new Field("", "SOURCE_DATA_TYPE", Types.SMALLINT, 32);		return buildResultSet(fields, new ArrayList());	}	/**	 * Get a description of a table's optimal set of columns that uniquely	 * identifies a row. They are ordered by SCOPE.	 * <P>	 * Each column description has the following columns:	 * <OL>	 * <li> <B>SCOPE</B> short => actual scope of result	 * <UL>	 * <li> bestRowTemporary - very temporary, while using row </li>	 * <li> bestRowTransaction - valid for remainder of current transaction	 * </li>	 * <li> bestRowSession - valid for remainder of current session </li>	 * </ul>	 * </li>	 * <li> <B>COLUMN_NAME</B> String => column name </li>	 * <li> <B>DATA_TYPE</B> short => SQL data type from java.sql.Types </li>	 * <li> <B>TYPE_NAME</B> String => Data source dependent type name </li>	 * <li> <B>COLUMN_SIZE</B> int => precision </li>	 * <li> <B>BUFFER_LENGTH</B> int => not used </li>	 * <li> <B>DECIMAL_DIGITS</B> short => scale </li>	 * <li> <B>PSEUDO_COLUMN</B> short => is this a pseudo column like an	 * Oracle ROWID	 * <UL>	 * <li> bestRowUnknown - may or may not be pseudo column </li>	 * <li> bestRowNotPseudo - is NOT a pseudo column </li>	 * <li> bestRowPseudo - is a pseudo column </li>	 * </ul>	 * </li>	 * </ol>	 * </p>	 * 	 * @param catalog	 *            a catalog name; "" retrieves those without a catalog	 * @param schema	 *            a schema name; "" retrieves those without a schema	 * @param table	 *            a table name	 * @param scope	 *            the scope of interest; use same values as SCOPE	 * @param nullable	 *            include columns that are nullable?	 * @return ResultSet each row is a column description	 * @throws SQLException	 *             DOCUMENT ME!	 */	public java.sql.ResultSet getBestRowIdentifier(String catalog,			String schema, final String table, int scope, boolean nullable)			throws SQLException {		if (table == null) {			throw SQLError.createSQLException("Table not specified.",					SQLError.SQL_STATE_ILLEGAL_ARGUMENT);		}		Field[] fields = new Field[8];		fields[0] = new Field("", "SCOPE", Types.SMALLINT, 5);		fields[1] = new Field("", "COLUMN_NAME", Types.CHAR, 32);		fields[2] = new Field("", "DATA_TYPE", Types.SMALLINT, 32);		fields[3] = new Field("", "TYPE_NAME", Types.CHAR, 32);		fields[4] = new Field("", "COLUMN_SIZE", Types.INTEGER, 10);		fields[5] = new Field("", "BUFFER_LENGTH", Types.INTEGER, 10);		fields[6] = new Field("", "DECIMAL_DIGITS", Types.INTEGER, 10);		fields[7] = new Field("", "PSEUDO_COLUMN", Types.SMALLINT, 5);		final ArrayList rows = new ArrayList();		final Statement stmt = this.conn.getMetadataSafeStatement();		try {			new IterateBlock(getCatalogIterator(catalog)) {				void forEach(Object catalogStr) throws SQLException {					ResultSet results = null;					try {						StringBuffer queryBuf = new StringBuffer(								"SHOW COLUMNS FROM ");						queryBuf.append(quotedId);						queryBuf.append(table);						queryBuf.append(quotedId);						queryBuf.append(" FROM ");						queryBuf.append(quotedId);						queryBuf.append(catalogStr.toString());						queryBuf.append(quotedId);						results = stmt.executeQuery(queryBuf.toString());						while (results.next()) {							String keyType = results.getString("Key");							if (keyType != null) {								if (StringUtils.startsWithIgnoreCase(keyType,										"PRI")) {									byte[][] rowVal = new byte[8][];									rowVal[0] = Integer											.toString(													java.sql.DatabaseMetaData.bestRowSession)											.getBytes();									rowVal[1] = results.getBytes("Field");									String type = results.getString("Type");									int size = MysqlIO.getMaxBuf();									int decimals = 0;									/*									 * Parse the Type column from MySQL									 */									if (type.indexOf("enum") != -1) {										String temp = type.substring(type												.indexOf("("), type												.indexOf(")"));										java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(												temp, ",");										int maxLength = 0;										while (tokenizer.hasMoreTokens()) {											maxLength = Math.max(maxLength,													(tokenizer.nextToken()															.length() - 2));										}										size = maxLength;										decimals = 0;										type = "enum";									} else if (type.indexOf("(") != -1) {										if (type.indexOf(",") != -1) {											size = Integer.parseInt(type													.substring(type															.indexOf("(") + 1,															type.indexOf(",")));											decimals = Integer.parseInt(type													.substring(type															.indexOf(",") + 1,															type.indexOf(")")));										} else {											size = Integer.parseInt(type													.substring(type															.indexOf("(") + 1,															type.indexOf(")")));										}										type = type.substring(0, type												.indexOf("("));									}									rowVal[2] = s2b(String.valueOf(MysqlDefs											.mysqlToJavaType(type)));									rowVal[3] = s2b(type);									rowVal[4] = Integer.toString(											size + decimals).getBytes();									rowVal[5] = Integer.toString(											size + decimals).getBytes();									rowVal[6] = Integer.toString(decimals)											.getBytes();									rowVal[7] = Integer											.toString(													java.sql.DatabaseMetaData.bestRowNotPseudo)											.getBytes();									rows.add(new ByteArrayRow(rowVal));								}							}						}					} finally {						if (results != null) {							try {								results.close();							} catch (Exception ex) {								;							}							results = null;						}					}				}			}.doForAll();		} finally {			if (stmt != null) {				stmt.close();			}		}		java.sql.ResultSet results = buildResultSet(fields, rows);		return results;	}	/*	 * * Each row in the ResultSet is a parameter desription or column	 * description with the following fields: <OL> <li> <B>PROCEDURE_CAT</B>	 * String => procedure catalog (may be null) </li> <li> <B>PROCEDURE_SCHEM</B>	 * String => procedure schema (may be null) </li> <li> <B>PROCEDURE_NAME</B>	 * String => procedure name </li> <li> <B>COLUMN_NAME</B> String =>	 * column/parameter name </li> <li> <B>COLUMN_TYPE</B> Short => kind of	 * column/parameter: <UL> <li> procedureColumnUnknown - nobody knows </li>	 * <li> procedureColumnIn - IN parameter </li> <li> procedureColumnInOut -	 * INOUT parameter </li> <li> procedureColumnOut - OUT parameter </li> <li>	 * procedureColumnReturn - procedure return value </li> <li>	 * procedureColumnResult - result column in ResultSet </li> </ul> </li> <li>	 * <B>DATA_TYPE</B> short => SQL type from java.sql.Types </li> <li>	 * <B>TYPE_NAME</B> String => SQL type name </li> <li> <B>PRECISION</B>	 * int => precision </li> <li> <B>LENGTH</B> int => length in bytes of data	 * </li> <li> <B>SCALE</B> short => scale </li> <li> <B>RADIX</B> short =>	 * radix </li> <li> <B>NULLABLE</B> short => can it contain NULL? <UL> <li>	 * procedureNoNulls - does not allow NULL values </li> <li>	 * procedureNullable - allows NULL values </li> <li>	 * procedureNullableUnknown - nullability unknown </li> </ul> </li> <li>	 * <B>REMARKS</B> String => comment describing parameter/column </li> </ol>	 * </p> <P> <B>Note:</B> Some databases may not return the column	 * descriptions for a procedure. Additional columns beyond REMARKS can be	 * defined by the database. </p> @param catalog a catalog name; "" retrieves	 * those without a catalog @param schemaPattern a schema name pattern; ""	 * retrieves those without a schema @param procedureNamePattern a procedure	 * name pattern @param columnNamePattern a column name pattern @return	 * ResultSet each row is a stored procedure parameter or column description	 * @throws SQLException if a database access error occurs	 * 	 * @see #getSearchStringEscape	 */	private void getCallStmtParameterTypes(String catalog, String procName,			String parameterNamePattern, List resultRows) throws SQLException {		getCallStmtParameterTypes(catalog, procName, 				parameterNamePattern, resultRows, false);	}		private void getCallStmtParameterTypes(String catalog, String procName,			String parameterNamePattern, List resultRows, 			boolean forGetFunctionColumns) throws SQLException {		java.sql.Statement paramRetrievalStmt = null;		java.sql.ResultSet paramRetrievalRs = null;		if (parameterNamePattern == null) {			if (this.conn.getNullNamePatternMatchesAll()) {				parameterNamePattern = "%";			} else {				throw SQLError.createSQLException(						"Parameter/Column name pattern can not be NULL or empty.",						SQLError.SQL_STATE_ILLEGAL_ARGUMENT);			}		}		byte[] procNameAsBytes = null;		try {			procNameAsBytes = procName.getBytes("UTF-8");		} catch (UnsupportedEncodingException ueEx) {			procNameAsBytes = s2b(procName);			// Set all fields to connection encoding		}		String quoteChar = getIdentifierQuoteString();		String parameterDef = null;			boolean isProcedureInAnsiMode = false;		String storageDefnDelims = null;		String storageDefnClosures = null;				try {			paramRetrievalStmt = this.conn.getMetadataSafeStatement();						if (this.conn.lowerCaseTableNames() && catalog != null 					&& catalog.length() != 0) {				// Workaround for bug in server wrt. to 				// SHOW CREATE PROCEDURE not respecting				// lower-case table names								String oldCatalog = this.conn.getCatalog();				ResultSet rs = null;								try {					this.conn.setCatalog(catalog);					rs = paramRetrievalStmt.executeQuery("SELECT DATABASE()");					rs.next();										catalog = rs.getString(1);									} finally {										this.conn.setCatalog(oldCatalog);										if (rs != null) {						rs.close();					}				}			}						if (paramRetrievalStmt.getMaxRows() != 0) {				paramRetrievalStmt.setMaxRows(0);			}			int dotIndex = -1;			if (!" ".equals(quoteChar)) {				dotIndex = StringUtils.indexOfIgnoreCaseRespectQuotes(0,						procName, ".", quoteChar.charAt(0), !this.conn								.isNoBackslashEscapesSet());			} else {				dotIndex = procName.indexOf(".");			}			String dbName = null;			if (dotIndex != -1 && (dotIndex + 1) < procName.length()) {				dbName = procName.substring(0, dotIndex);				procName = procName.substring(dotIndex + 1);			} else {				dbName = catalog;			}			StringBuffer procNameBuf = new StringBuffer();			if (dbName != null) {				if (!" ".equals(quoteChar) && !dbName.startsWith(quoteChar)) {					procNameBuf.append(quoteChar);				}				procNameBuf.append(dbName);				if (!" ".equals(quoteChar) && !dbName.startsWith(quoteChar)) {					procNameBuf.append(quoteChar);				}				procNameBuf.append(".");			}			boolean procNameIsNotQuoted = !procName.startsWith(quoteChar);			if (!" ".equals(quoteChar) && procNameIsNotQuoted) {				procNameBuf.append(quoteChar);			}			procNameBuf.append(procName);			if (!" ".equals(quoteChar) && procNameIsNotQuoted) {				procNameBuf.append(quoteChar);			}			boolean parsingFunction = false;			try {				paramRetrievalRs = paramRetrievalStmt						.executeQuery("SHOW CREATE PROCEDURE "								+ procNameBuf.toString());				parsingFunction = false;			} catch (SQLException sqlEx) {				paramRetrievalRs = paramRetrievalStmt						.executeQuery("SHOW CREATE FUNCTION "								+ procNameBuf.toString());				parsingFunction = true;			}			if (paramRetrievalRs.next()) {				String procedureDef = parsingFunction ? paramRetrievalRs						.getString("Create Function") : paramRetrievalRs						.getString("Create Procedure");										if (procedureDef == null || procedureDef.length() == 0) {					throw SQLError.createSQLException("User does not have access to metadata required to determine " +							"stored procedure parameter types. If rights can not be granted, configure connection with \"noAccessToProcedureBodies=true\" " +							"to have driver generate parameters that represent INOUT strings irregardless of actual parameter types.",							SQLError.SQL_STATE_GENERAL_ERROR);						}				try {					String sqlMode = paramRetrievalRs.getString("sql_mode");										if (StringUtils.indexOfIgnoreCase(sqlMode, "ANSI") != -1) {						isProcedureInAnsiMode = true;					}				} catch (SQLException sqlEx) {					// doesn't exist				}

⌨️ 快捷键说明

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