【Java】导出Mysql表表结构与注释数据字典

需求:
把mysql中所有表的字段名、数据类型、长度、注释整理成csv,做成数据字典。

import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;/*** @Desc: <p>** </p>* @Author: A Real Man* @Time: 2024/4/12 11:00*/
public class MysqlTest {public static void main(String[] args) throws SQLException {export("testdb");}public static void export(String dbName) throws SQLException {Connection connection = DriverManager.getConnection("jdbc:mysql://ip:port/testdb?serverTimezone=GMT%2B8&characterEncoding=utf-8","username", "password");try {String fgf = "分隔符";DatabaseMetaData metaData = connection.getMetaData();ResultSet tables = metaData.getTables(dbName, null, null, new String[] { "TABLE" });StringBuilder cc = new StringBuilder();while (tables.next()) {String tableName = tables.getString("TABLE_NAME");if(tableName.startsWith("不需要的表名")){continue;}System.out.println("Table: " + tableName);ResultSet columns = metaData.getColumns(null, null, tableName, null);while (columns.next()) {String columnName = columns.getString("COLUMN_NAME");String dataType = columns.getString("TYPE_NAME");int size = columns.getInt("COLUMN_SIZE");String remarks = columns.getString("REMARKS");int digit = columns.getInt("DECIMAL_DIGITS");cc.append(dbName).append(fgf).append(tableName).append(fgf).append(columnName).append(fgf).append(dataType).append(fgf).append(size).append(fgf).append(digit).append(fgf).append(remarks).append("\n");}columns.close();}tables.close();FileTool.newFile("D:/app/mysql/" + dbName + ".txt", cc.toString());} catch (IOException e) {e.printStackTrace();} finally {rconnection.close();}}
}

columns.getString(“REMARKS”) 如果你还需要取别的内容,可以打开java.sql.DatabaseMetaData源码,找到getColumns()方法的注释,里面记录了KEY;

    /*** Retrieves a description of table columns available in* the specified catalog.** <P>Only column descriptions matching the catalog, schema, table* and column name criteria are returned.  They are ordered by* <code>TABLE_CAT</code>,<code>TABLE_SCHEM</code>,* <code>TABLE_NAME</code>, and <code>ORDINAL_POSITION</code>.** <P>Each column description has the following columns:*  <OL>*  <LI><B>TABLE_CAT</B> String {@code =>} table catalog (may be <code>null</code>)*  <LI><B>TABLE_SCHEM</B> String {@code =>} table schema (may be <code>null</code>)*  <LI><B>TABLE_NAME</B> String {@code =>} table name*  <LI><B>COLUMN_NAME</B> String {@code =>} column name*  <LI><B>DATA_TYPE</B> int {@code =>} SQL type from java.sql.Types*  <LI><B>TYPE_NAME</B> String {@code =>} Data source dependent type name,*  for a UDT the type name is fully qualified*  <LI><B>COLUMN_SIZE</B> int {@code =>} column size.*  <LI><B>BUFFER_LENGTH</B> is not used.*  <LI><B>DECIMAL_DIGITS</B> int {@code =>} the number of fractional digits. Null is returned for data types where* DECIMAL_DIGITS is not applicable.*  <LI><B>NUM_PREC_RADIX</B> int {@code =>} Radix (typically either 10 or 2)*  <LI><B>NULLABLE</B> int {@code =>} is NULL allowed.*      <UL>*      <LI> columnNoNulls - might not allow <code>NULL</code> values*      <LI> columnNullable - definitely allows <code>NULL</code> values*      <LI> columnNullableUnknown - nullability unknown*      </UL>*  <LI><B>REMARKS</B> String {@code =>} comment describing column (may be <code>null</code>)*  <LI><B>COLUMN_DEF</B> String {@code =>} default value for the column, which should be interpreted as a string when the value is enclosed in single quotes (may be <code>null</code>)*  <LI><B>SQL_DATA_TYPE</B> int {@code =>} unused*  <LI><B>SQL_DATETIME_SUB</B> int {@code =>} unused*  <LI><B>CHAR_OCTET_LENGTH</B> int {@code =>} for char types the*       maximum number of bytes in the column*  <LI><B>ORDINAL_POSITION</B> int {@code =>} index of column in table*      (starting at 1)*  <LI><B>IS_NULLABLE</B> String  {@code =>} ISO rules are used to determine the nullability for a column.*       <UL>*       <LI> YES           --- if the column can include NULLs*       <LI> NO            --- if the column cannot include NULLs*       <LI> empty string  --- if the nullability for the* column is unknown*       </UL>*  <LI><B>SCOPE_CATALOG</B> String {@code =>} catalog of table that is the scope*      of a reference attribute (<code>null</code> if DATA_TYPE isn't REF)*  <LI><B>SCOPE_SCHEMA</B> String {@code =>} schema of table that is the scope*      of a reference attribute (<code>null</code> if the DATA_TYPE isn't REF)*  <LI><B>SCOPE_TABLE</B> String {@code =>} table name that this the scope*      of a reference attribute (<code>null</code> if the DATA_TYPE isn't REF)*  <LI><B>SOURCE_DATA_TYPE</B> short {@code =>} source type of a distinct type or user-generated*      Ref type, SQL type from java.sql.Types (<code>null</code> if DATA_TYPE*      isn't DISTINCT or user-generated REF)*   <LI><B>IS_AUTOINCREMENT</B> String  {@code =>} Indicates whether this column is auto incremented*       <UL>*       <LI> YES           --- if the column is auto incremented*       <LI> NO            --- if the column is not auto incremented*       <LI> empty string  --- if it cannot be determined whether the column is auto incremented*       </UL>*   <LI><B>IS_GENERATEDCOLUMN</B> String  {@code =>} Indicates whether this is a generated column*       <UL>*       <LI> YES           --- if this a generated column*       <LI> NO            --- if this not a generated column*       <LI> empty string  --- if it cannot be determined whether this is a generated column*       </UL>*  </OL>** <p>The COLUMN_SIZE column specifies the column size for the given column.* For numeric data, this is the maximum precision.  For character data, this is the length in characters.* For datetime datatypes, this is the length in characters of the String representation (assuming the* maximum allowed precision of the fractional seconds component). For binary data, this is the length in bytes.  For the ROWID datatype,* this is the length in bytes. Null is returned for data types where the* column size is not applicable.** @param catalog a catalog name; must match the catalog name as it*        is stored in the database; "" retrieves those without a catalog;*        <code>null</code> means that the catalog name should not be used to narrow*        the search* @param schemaPattern a schema name pattern; must match the schema name*        as it is stored in the database; "" retrieves those without a schema;*        <code>null</code> means that the schema name should not be used to narrow*        the search* @param tableNamePattern a table name pattern; must match the*        table name as it is stored in the database* @param columnNamePattern a column name pattern; must match the column*        name as it is stored in the database* @return <code>ResultSet</code> - each row is a column description* @exception SQLException if a database access error occurs* @see #getSearchStringEscape*/ResultSet getColumns(String catalog, String schemaPattern,String tableNamePattern, String columnNamePattern)throws SQLException;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/221.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

聊聊binlog是什么

1. 上一讲思考題解答:redo日志刷盘策略的选择建议 先给大家解释一下上一讲的思考題&#xff0c;我给大家的一个建议&#xff0c;其实对于redo日志的三种刷盘策略&#xff0c;我们通常建议是设置为1 也就是说&#xff0c;提交事务的时候&#xff0c;redo日志必须是刷入磁盘文件…

AWB学习记录

主要参考食鱼者博客&#xff1a;https://blog.csdn.net/wtzhu_13/article/details/119301096&#xff0c;以及相关的论文&#xff0c;感谢食鱼者老师整理分享。 灰度世界和完全反射 灰度世界法和完全反射法分别是基于(Rmean, Gmean, Bmean)和(Rmax, Gmax, Bmax)来进行白平衡校…

Leetcode 76. 最小覆盖子串和Leetcode 34. 在排序数组中查找元素的第一个和最后一个位置

文章目录 Leetcode 76. 最小覆盖子串题目描述C语言题解和思路解题思路 Leetcode 34. 在排序数组中查找元素的第一个和最后一个位置题目描述C语言题解和思路解题思路 Leetcode 76. 最小覆盖子串 题目描述 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子…

多任务学习的显著优势!

多任务学习是一种机器学习技术&#xff0c;它允许模型同时学习多个相关任务。与不进行多任务的整体学习&#xff08;单任务学习&#xff09;相比&#xff0c;多任务学习具有多个显著优势。 首先&#xff0c;多任务学习可以提高学习效率和速度。在并行学习中&#xff0c;多个任…

数组和指针的联系(C语言)

数组和指针是两种不同的数据类型&#xff0c;数组是一种构造类型&#xff0c;用于存储一组相同类型的变量&#xff1b;而指针是一种特殊类型&#xff0c;专门用来存放数据的地址。数组名除了sizeof(数组名)和&数组名表示整个数组外&#xff0c;其他情况下都表示的是首元素的…

profinet协议基础

文章目录 工业以太网自动化通讯金字塔工业以太网技术比较 profinet概述profinet特性 EtherNet通信EtherCAT通信EtherCat特性EtherCat过程同步 工业以太网 工业以太网是基于IEEE 802.3 (Ethernet)的强大的区域和单元网络。 自动化通讯金字塔 各个组织与工业以太网 工业以太网…

水气表CJ/T188协议学习及实例

水气表CJ/T188协议学习及实例 1 CT/J 188协议简介 CJ/T188协议规定了户用计量仪表(以下简称仪表)&#xff0c;包括水表、燃气表、热量表等仪表数据传输的基本原则&#xff0c;接口形式及物理性能、数据链路、数据标识及数据安全性和数据表达格式的要求。 CJ/T188协议为主-从…

DP10RF001一款工作于200MHz~960MHz低功耗、高性能、单片集成的(G)FSK/OOK无线收发芯片

产品概述. DP10RF001是一款工作于200MHz~960MHz范围内的低功耗、高性能、单片集成的(G)FSK/OOK无线收发机芯片。内部集成完整的射频接收机、射频发射机、频率综合器、调制解调器&#xff0c;只需配备简单、低成本的外围器件就可以获得良好的收发性能。芯片支持灵活可设的数据包…

Node.js安装与配置

Node.js是一个基于Chrome V8引擎的JavaScript运行环境&#xff0c;可以让你用JavaScript编写服务器端代码。在本文中&#xff0c;我将向大家介绍如何安装和配置Node.js。 首先&#xff0c;你需要前往Node.js官方网站&#xff08;https://nodejs.org/&#xff09;下载最新版本的…

DDD 领域驱动设计 - Domain Primitive(Kotlin 落地实现)

本文部分借鉴了阿里技术专家的 DDD 系列文章&#xff0c;笔者只是在学习 DDD 的过程中记录自己想法、思考. Ps&#xff1a;为了便于理解&#xff0c;笔者改动了部分案例&#xff0c;语言也换成 Kotlin 文章目录 为什么出现 DDD&#xff1f;Domain Primitive&#xff08;DP&…

C#中检查一个矩阵是否可逆

在C#中&#xff0c;要检查一个矩阵是否可逆&#xff08;即是否是满秩的&#xff0c;或者说是否有逆矩阵&#xff09;&#xff0c;以及计算它的逆矩阵&#xff0c;你可以使用数学库&#xff0c;比如Math.NET Numerics。这个库提供了强大的数学和统计功能&#xff0c;包括线性代数…

JavaEE初阶Day 10:多线程(8)

目录 Day 10&#xff1a;多线程&#xff08;8&#xff09;单例模式阻塞队列1. 生产者消费者模型1.1 生产者消费者模型解耦合1.2 生产者消费者模型削峰填谷 2. 生产者消费者代码3. 阻塞队列实现 Day 10&#xff1a;多线程&#xff08;8&#xff09; 单例模式 单例模式&#xf…

SQL Server Management Studio 显示行号

前言 在使用 SQL Server Management Studio (SSMS) 进行数据库管理和查询时&#xff0c;能够看到代码的行号是非常有用的。这可以帮助您更容易地定位代码错误、讨论特定的代码行&#xff0c;或者在执行长查询时快速找到特定行。在本文中&#xff0c;我将向您展示如何在 SSMS 中…

2024年华中杯数学建模竞赛ABC题思路分析

简单分析一下各个题目可能需要用到的方法和模型&#xff0c;完整代码和成品论文见文末 A题 太阳能路灯光伏板的朝向设计问题: 1. 球面几何、天文学相关知识,如赤纬角、太阳高度角、时角等概念和公式 2. 太阳辐射模型,根据太阳能辐射强度、大气衰减系数等计算地表太阳辐射强度…

蓝桥杯第十五届javab组个人总结

javab组 额今天早上打完了得对自己此次比赛做总结&#xff0c;无论是明年还参赛还是研究生蓝桥杯&#xff0c;体验感有点差&#xff0c;第一题其实一开始想手算但怕进位导致不准确还是让代码跑了&#xff0c;但跑第202420242024个数&#xff08;被20和24整除&#xff09;一直把…

【网络编程】Web服务器shttpd源码剖析——线程池调度

hello &#xff01;大家好呀&#xff01; 欢迎大家来到我的网络编程系列之web服务器shttpd源码剖析——线程池调度&#xff0c;在这篇文章中&#xff0c;你将会学习到在Linux内核中如何创建一个自己的并发服务器shttpd&#xff0c;并且我会给出源码进行剖析&#xff0c;以及手绘…

FebHost:注册.CA域名的企业有什么限制?

在加拿大&#xff0c;只要满足加拿大互联网注册管理局的“加拿大注册要求”&#xff0c;任何类型的企业都可以注册.CA域名。这些要求的目的是为了确保.CA域名空间作为一个重要的公共资源得到合理的使用和开发&#xff0c;以促进所有加拿大人的社会和经济发展。 以下是一些主要…

双向带头循环链表的接口函数实现

学习内容&#xff1a; 1.双向链表的节点形式以及函数声明 #include<stdio.h> #include<stdlib.h> #include<assert.h> typedef int DataType; typedef struct ListNode { int val; struct ListNode* next; struct ListNode* prev; }LTNode; voi…

docker安装EelasticSearch、目录权限修改、并安装IK 中文分词器

文章目录 docker安装EelasticSearch、目录权限修改、并安装IK 中文分词器1、docker安装ES2、docker ps发现容器没有正常启动&#xff0c;docker logs 容器id 查看日志发现是挂载目录的权限不足3、修改目录的权限4、使用docker restart 容器id重新启动刚才没有启动成功的容器5、…

Leetcode 4.18

Leetcode 1.无重复字符的最长子串2.最长回文子串3.整数反转4.字符串转换整数 (atoi)5.正则表达式匹配 1.无重复字符的最长子串 无重复字符的最长子串 滑动窗口&#xff0c;先让右指针右移&#xff0c;如果发现这个子串有元素和右指针当前元素重复。 则&#xff1a; 左指针右移…