首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

运用有限多例模式管理数据库结构信息

2013-03-13 
使用有限多例模式管理数据库结构信息/*** 应当在此,还要初始化字段Field及各表的键值信息 注意事项: 使用R

使用有限多例模式管理数据库结构信息
/** * 应当在此,还要初始化字段Field及各表的键值信息 注意事项: 使用ResultSet rs = dm.getColumns(catalog, * null, tableName, null);/使用derby数据库时,大小写是敏感的, 因此 * * @param tableName的值,只能通过dm原型获取的值,这样造成了大小写问题,在运算过程中,不能转换大小写,只能在最后统一转换成小写, */ synchronized private void initTableMap(String tableName) throws SQLException { Table table = getTable(tableName.toLowerCase()); if (table == null) { table = new Table(); } table.setName(tableName.toLowerCase()); Set fieldSet = new java.util.LinkedHashSet(); Set keySet = new java.util.LinkedHashSet(); if (con != null) { ResultSet rs = dm.getColumns(catalog, null, tableName, null);//获取表中所有字段 //参考ResultSet rs = dm.getProcedureColumns(catalog, catalog, driverName, tableName);//? Map<String, Field> field_map = new LinkedHashMap(); while (rs.next()) { String name = rs.getString("COLUMN_NAME");//参数值可参考dm.getColumns(catalog, null, tableName, null)的帮助文档 fieldSet.add(lowerCase(name)); Field f = new Field(); f.setName(lowerCase(name)); //DATA_TYPE int => SQL type from java.sql.Types /* Each column description has the following columns: TABLE_CAT String => table catalog (may be null) TABLE_SCHEM String => table schema (may be null) TABLE_NAME String => table name COLUMN_NAME String => column name DATA_TYPE int => SQL type from java.sql.Types TYPE_NAME String => Data source dependent type name, for a UDT the type name is fully qualified COLUMN_SIZE int => column size. BUFFER_LENGTH is not used. DECIMAL_DIGITS int => the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable. NUM_PREC_RADIX int => Radix (typically either 10 or 2) NULLABLE int => is NULL allowed. columnNoNulls - might not allow NULL values columnNullable - definitely allows NULL values columnNullableUnknown - nullability unknown REMARKS String => comment describing column (may be null) COLUMN_DEF String => default value for the column, which should be interpreted as a string when the value is enclosed in single quotes (may be null) SQL_DATA_TYPE int => unused SQL_DATETIME_SUB int => unused CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in the column ORDINAL_POSITION int => index of column in table (starting at 1) IS_NULLABLE String => ISO rules are used to determine the nullability for a column. YES --- if the column can include NULLs NO --- if the column cannot include NULLs empty string --- if the nullability for the column is unknown SCOPE_CATALOG String => catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF) SCOPE_SCHEMA String => schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF) SCOPE_TABLE String => table name that this the scope of a reference attribute (null if the DATA_TYPE isn't REF) SOURCE_DATA_TYPE short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types (null if DATA_TYPE isn't DISTINCT or user-generated REF) IS_AUTOINCREMENT String => Indicates whether this column is auto incremented YES --- if the column is auto incremented NO --- if the column is not auto incremented empty string --- if it cannot be determined whether the column is auto incremented IS_GENERATEDCOLUMN String => Indicates whether this is a generated column YES --- if this a generated column NO --- if this not a generated column empty string --- if it cannot be determined whether this is a generated column */ String dataType = rs.getString("DATA_TYPE"); f.setSqlType(new Integer(dataType).intValue());//如:java.sql.Types.INTEGER String type = rs.getString("TYPE_NAME");//如:BIGINT f.setTypeName(lowerCase(type)); String position = rs.getString("ORDINAL_POSITION");//在表中的位置 f.setPosition(position); String size = rs.getString("COLUMN_SIZE");//用户定义的字段长度 f.setSize(size); String bufferLength = rs.getString("BUFFER_LENGTH");//字段缓冲区大小 f.setBufferLength(bufferLength); String decimal = rs.getString("DECIMAL_DIGITS");//精度 f.setDecimal(decimal); String defaultValue = rs.getString("COLUMN_DEF"); f.setDefaultValue(defaultValue); String remark = rs.getString("REMARKS"); f.setRemark(remark); String nullable = rs.getString("NULLABLE");//取值0||1,1允许空值,0不允许空值 if ("0".equals(nullable)) { f.setNullable(false); } if ("1".equals(nullable)) { f.setNullable(true); } field_map.put(name.toLowerCase(), f); } table.setFieldMap(field_map);//字段名:Field对象的映射表; //获取字段名数组 Object[] o = fieldSet.toArray(); String[] fields = new String[o.length]; for (int i = 0; i < o.length; i++) { fields[i] = ((String) o[i]).toLowerCase(); } table.setFields(fields); //主键部分,开始 ResultSet rsk = dm.getPrimaryKeys(catalog, null, tableName); //均通过新版SQL Server和MySQL的jdbc驱动的测试,返回所有主键 //ResultSet rsk = dm.getPrimaryKeys(catalog, "%", tableName);//早期版本的MySQL jdbc驱动程序中通过测试,返回所有主键 // while (rsk.next()) { String name = rsk.getString("COLUMN_NAME");//主键名 keySet.add(lowerCase(name.toLowerCase()));// } Object[] k = keySet.toArray(); String[] keys = new String[k.length]; for (int i = 0; i < k.length; i++) { keys[i] = (String) k[i]; field_map.get(keys[i]).setPrimarykey(true);//通过mssql、mysql、derby } table.setKeys(keys); //主键部分,结束 ///给Field属性typeClassName赋值 String squeryFieldTypeClassName = "select * from " + tableName.toLowerCase() + " where " + table.getFields()[0] + " is null"; if (table.getKeys().length > 0) { squeryFieldTypeClassName = "select * from " + tableName.toLowerCase() + " where " + table.getKeys()[0] + " is null"; } Statement stmt0 = con.createStatement(); ResultSet rscname = stmt0.executeQuery(squeryFieldTypeClassName); ResultSetMetaData rsmd = rscname.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String fieldNmae = rsmd.getColumnName(i); field_map.get(fieldNmae.toLowerCase()).setTypeClassName(rsmd.getColumnClassName(i));//通过mssql、mysql、derby } stmt0.close(); } tableMap.put(tableName.toLowerCase(), table);//初始化Table }

在基于HashMap关系数据映射技术的JadePool的实现中,Db族类通过有限多例模式动态提取和管理数据库结构信息,是JadePool的大管家。这是HRM产品与ORM产品最根本的区别所在。是JadePool之所以做到高效、简洁、智能化的重要前提。

顺便说一下DbAccess负责提取管理非事务型数据库的数据库结构信息。

使用DbCenter和DbAccess的重要区别在于数据库是否支持事务,在事务型的数据库中,可以通过ResultSet rsk = dm.getPrimaryKeys(catalog, null, tableName);语句提取主键信息,而非事务的数据库目前不支持这种操作。目前JadePool将非事务型数据库的表的第一个字段当做主键对待。


如何实例化DbCenter?

ProcessVO是JadePool的核心类,主要用于实现数据库的CRUD或DML操作。目前,该类有三个构造函数

    1、ProcessVO();它将实例化DbCenter中的defaultDb实例
            2、ProcessVO(Connection con);它将实例化DbCenter中的userDb实例
            3、ProcessVO(Connection con, int connectionType);它将根据connectionType的值,如:根据cn.jadepool.sql.DbConnectionType.USING_DB_01实例化db_01,根据cn.jadepool.sql.DbConnectionType.USING_DB_02实例化db_02。

例如:

ProcessVO pvo=new ProcessVO();

Db db=pvo.getDb();

......

此时,用户可以通过db来访问数据库的结构信息、表的结构信息以及字段的结构信息。

如果您对JadePool感兴趣,可以通过以下网址下载jadepool-1.0-GBK

http://download.csdn.net/detail/wj800/5109413

http://www.jadepool.cn/down?id=1000







热点排行