利用JDBC导出数据库并生成静态导航界面文件

对于一些应用场景,需要查看数据库的表,字段信息,表注释,字段注释等信息,但又不想将数据库的用户名密码告知,即可导出一份数据库来查看信息。

一,准备

要连接的数据库信息,用户名,密码,连接地址等

获取到相关数据库的驱动包,在此以连接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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供Java代码示例,实现JDBC连接MySQL数据库,获取数据并生成Txt输出。 首先,您需要确保已经安装了MySQL数据库并创建了一张表,用于存储您需要获取的数据。假设您的表名为`user`,包含两个字段`id`和`name`。 接下来,您需要在Java项目中导入MySQL JDBC驱动程序的jar包,以便连接MySQL数据库。 示例代码如下: ```java import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.sql.*; public class JDBCExample { // JDBC连接的URL,其中`test`为数据库名,`root`为用户名,`password`为密码,根据实际情况修改 private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; private static final String USER = "root"; private static final String PASSWORD = "123456"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 注册JDBC驱动程序 Class.forName("com.mysql.cj.jdbc.Driver"); // 打开连接 conn = DriverManager.getConnection(URL, USER, PASSWORD); // 执行查询 stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM user"); // 写入数据 BufferedWriter writer = new BufferedWriter(new FileWriter("data.txt")); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); writer.write("id: " + id + ", name: " + name + "\n"); } writer.close(); System.out.println("数据写入成功!"); } catch (ClassNotFoundException | SQLException | IOException e) { e.printStackTrace(); } finally { // 关闭连接 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上面的代码中,我们首先注册了MySQL JDBC驱动程序,然后打开了一个连接。接着,我们执行了一个查询语句,将结果集写入到`data.txt`文件中,并在控制台输出成功信息。最后,我们关闭了连接。 您需要根据实际情况修改`URL`、`USER`和`PASSWORD`参数,以及查询语句和写入数据的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值