对于一些应用场景,需要查看数据库的表,字段信息,表注释,字段注释等信息,但又不想将数据库的用户名密码告知,即可导出一份数据库来查看信息。
一,准备
要连接的数据库信息,用户名,密码,连接地址等
获取到相关数据库的驱动包,在此以连接mysql 为例。
二,开发
利用JDBC 来连接数据库
关于DatabaseMetaData类,参考传送门。
DatabaseMetaData类是java.sql包中的类,利用它可以获取我们连接到的数据库的结构、存储等很多信息。如:
1、数据库与用户,数据库标识符以及函数与存储过程。
2、数据库限制。
3、数据库支持不支持的功能。
4、架构、编目、表、列和视图等。
通过调用DatabaseMetaData的各种方法,程序可以动态的了解一个数据库。由于这个类中的方法非常的多那么就介绍几个常用的方法来给大家参考。
(1) DatabaseMetaData实例的获取
Connection conn = DriverManager.getConnection(……);
DatabaseMetaData dbmd = Conn.getMetaData();
创建了这个实例,就可以使用它的方法来获取数据库得信息。主要使用如下的方法:
(2) 获得当前数据库以及驱动的信息
dbmd.getDatabaseProductName():用以获得当前数据库是什么数据库。比如oracle,access等。返回的是字符串。
dbmd.getDatabaseProductVersion():获得数据库的版本。返回的字符串。
dbmd.getDriverVersion():获得驱动程序的版本。返回字符串。
dbmd.getTypeInfo() :获得当前数据库的类型信息
(3) 获得当前数据库中表的信息
dbmd.getTables(String catalog,String schema,String tableName,String[] types),
这个方法带有四个参数,它们表示的含义如下:
String catalog:要获得表所在的编目。"“”"意味着没有任何编目,Null表示所有编目。
String schema:要获得表所在的模式。"“”"意味着没有任何模式,Null表示所有模式。
String tableName:指出要返回表名与该参数匹配的那些表,
String types:一个指出返回何种表的数组。
可能的数组项是:"TABLE"、"VIEW"、"SYSTEM TABLE", "GLOBAL TEMPORARY","LOCAL TEMPORARY","ALIAS","SYSNONYM"。
通过getTables()方法返回的结果集中的每个表都有下面是10字段的描述信息,而且只有10个。通常我们用到的也就是标红的几个字段。而且在结果集中直接使用下面字段前面的序号即可获取字段值。
1.TABLE_CAT (String) => 表所在的编目(可能为空)
2.TABLE_SCHEM (String) => 表所在的模式(可能为空)
3.TABLE_NAME (String) => 表的名称
4.TABLE_TYPE (String) => 表的类型。
典型的有 "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
5.REMARKS (String) => 解释性的备注
6.TYPE_CAT (String) =>编目类型(may be null)
7.TYPE_SCHEM (String) => 模式类型(may be null)
8.TYPE_NAME (String) => 类型名称(may be null)
9.SELF_REFERENCING_COL_NAME (String) => name of the designated "identifier" column of a typed table (may be null)
10.REF_GENERATION (String) => specifies how values in SELF_REFERENCING_COL_NAME are created.
它的值有:"SYSTEM"、"USER"、"DERIVED",也可能为空。
(4)获得某个表的列信息
dbmd.getColumns(String catalog,String schama,String tablename,String columnPattern,
通过getColumns()方法返回的结果集中的每一列都有下面是23个字段段的描述信息,而且只有23个。通常我们用到的也就是标红的字段。而且在结果集中直接使用下面字段前面的序号即可获取字段值。
1.TABLE_CAT String => table catalog (may be null)
2.TABLE_SCHEM String => table schema (may be null)
3.TABLE_NAME String => table name (表名称)
4.COLUMN_NAME String => column name(列名)
5.DATA_TYPE int => SQL type from java.sql.Types(列的数据类型)
6.TYPE_NAME String => Data source dependent type name, for a UDT the type name is fully qualified
7.COLUMN_SIZE int => column size.
8.BUFFER_LENGTH is not used.
9.DECIMAL_DIGITS int => the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable.
10.NUM_PREC_RADIX int => Radix (typically either 10 or 2)
11.NULLABLE int => is NULL allowed.
12.REMARKS String => comment describing column (may be null)
13.COLUMN_DEF String => default value for the column, (may be null)
14.SQL_DATA_TYPE int => unused
15.SQL_DATETIME_SUB int => unused
16.CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in the column
17.ORDINAL_POSITION int => index of column in table (starting at 1)
18.IS_NULLABLE String => ISO rules are used to determine the nullability for a column.
19.SCOPE_CATLOG String => catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
20.SCOPE_SCHEMA String => schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
21.SCOPE_TABLE String => table name that this the scope of a reference attribure (null if the DATA_TYPE isn't REF)
22.SOURCE_DATA_TYPE short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types
23.IS_AUTOINCREMENT String => Indicates whether this column is auto incremented
(5)获得表的关键字信息
dbmd.getPrimaryKeys(String catalog, String schema, String table),
通过getPrimaryKeys方法返回的结果集中的每一列都有下面是6个字段段的描述信息,而且只有6个。通常我们用到的也就是标红的字段。而且在结果集中直接使用下面字段前面的序号即可获取字段值。
1.TABLE_CAT String => table catalog (may be null)
2.TABLE_SCHEM String => table schema (may be null)
3.TABLE_NAME String => table name
4.COLUMN_NAME String => column name
5.KEY_SEQ short => sequence number within primary key
6.PK_NAME String => primary key name (may be null)
这两个方法中的参数的含义和上面的介绍的是相同的。凡是pattern的都是可以用通配符匹配的。getColums()返回的是结果集,这个结果集包括了列的所有信息,类型,名称,可否为空等。getPrimaryKey()则是返回了某个表的关键字的结果集。
(6)获取指定表的外键信息
dbmd.getExportedKeys(String catalog, String schema, String table)
通过getPrimaryKeys方法返回的结果集中的每一列都有下面是11个字段段的描述信息,而且只有11个。通常我们用到的也就是标红的字段。而且在结果集中直接使用下面字段前面的序号即可获取字段值。
1.PKTABLE_CAT String => primary key table catalog (may be null)
2.PKTABLE_SCHEM String => primary key table schema (may be null)
3.PKTABLE_NAME String => primary key table name
4.PKCOLUMN_NAME String => primary key column name
5.FKTABLE_CAT String => foreign key table catalog (may be null) being exported (may be null)
6.FKTABLE_SCHEM String => foreign key table schema (may be null) being exported (may be null)
7.FKTABLE_NAME String => foreign key table name being exported
8.FKCOLUMN_NAME String => foreign key column name being exported
9.KEY_SEQ short => sequence number within foreign key
10.UPDATE_RULE short => What happens to foreign key when primary is updated:
11.DELETE_RULE short => What happens to the foreign key when primary is deleted.
(7)反向设计表
通过getTables(),getColumns(),getPrimaryKeys()就可以完成表的反向设计了。主要步骤如下:
1、通过getTables()获得数据库中表的信息。
2、对于每个表使用,getColumns(),getPrimaryKeys()获得相应的列名,类型,限制条件,关键字等。
3、通过1,2获得信息可以生成相应的建表的SQL语句。
通过上述三步完成反向设计表的过程。
三、编辑生成的界面导航
在另一篇文章有写到http://blog.csdn.net/sizhezhongnian/article/details/78521952 或者在下面获取资源能够看到界面的设计
四、遇到的问题。
在获取mysql表注释的时候,发现获取不到,经查证,useInformationSchema 这个属性默认是false的,需要设置为true才可以。
https://bugs.mysql.com/bug.php?id=65213
所以获取的mysql的链接要变成
jdbc:mysql://127.0.0.1:3306/information_schema?useInformationSchema=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&tinyInt1isBit=false
五、资源
码云:https://gitee.com/cjn93/dbTool.git
csdn资源:http://download.csdn.net/download/sizhezhongnian/10137226