当前位置: 首页 > news >正文

【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;

相关文章:

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

需求&#xff1a; 把mysql中所有表的字段名、数据类型、长度、注释整理成csv&#xff0c;做成数据字典。 import java.io.IOException; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import ja…...

第07-2章 TCP/IP模型

7.7 TCP/IP模型详解 7.7.1 简介 应用层的PDU>APDU&#xff08;Application PDU&#xff09; 表示层的PDU>PPDU&#xff08;Presentation PDU&#xff09; 会话层的PDU>SPDU&#xff08;Session PDU&#xff09; 7.7.2 TCP/IP协议体系 &#xff08;1&#xff09;TCP…...

【办公类-21-15】 20240410三级育婴师 712道单选题(题目与答案合并word)

作品展示 背景需求&#xff1a; 前文将APP题库里的育婴师题目下载到EXCEL&#xff0c;并进行手动整理 【办公类-21-13】 2024045三级育婴师 721道单选题 UIBOT下载整理-CSDN博客文章浏览阅读451次&#xff0c;点赞10次&#xff0c;收藏3次。【办公类-21-13】 2024045三级育婴…...

Vue3+Vant开发:登录功能

&#x1f648;作者简介&#xff1a;练习时长两年半的Java up主 &#x1f649;个人主页&#xff1a;程序员老茶 &#x1f64a; ps:点赞&#x1f44d;是免费的&#xff0c;却可以让写博客的作者开心好久好久&#x1f60e; &#x1f4da;系列专栏&#xff1a;Java全栈&#xff0c;…...

Linux程序调试优化(1)——内存占用详解及优化思路

文章目录 1.free查看总体的内存占用2./proc/$PID/status 查看某进程状态 linux开发最重要的两个参数&#xff0c;分别是内存以及CPU使用率&#xff0c;若内存出现严重不足&#xff0c;则在需要使用内存时&#xff0c;可能出现申请不到的情况&#xff0c;导致 OOM&#xff0c;L…...

高效解决Visual Studio Code中文乱码问题

文章目录 问题解决步骤 问题 Visual Studio Code新建一个文件编码方式总是默认GBK&#xff0c;如果我不修改成默认UTF-8&#xff0c;那么每次运行&#xff0c;如果有中文需要输出就会乱码&#xff01; 解决步骤 之后我会持续更新&#xff0c;如果喜欢我的文章&#xff0c;请记…...

springboot接口提高查询速度方法

接口想要提高查询速度&#xff0c;需要减少查询数据库的次数&#xff0c;需要把循环里面的查询提出来一次性查询完毕&#xff0c;然后通过java代码来获取响应的值。如下所示&#xff1a; List<OrderInfoHtVO> orderInfoList orderInfoService.getOrderInfoHtlist(query…...

如何在苹果手机上安装iOS应用的.ipa文件?

哈喽&#xff0c;大家好呀&#xff0c;淼淼又来和大家见面啦&#xff0c;如今移动应用市场不断的发展&#xff0c;许多开发者小伙伴们都选择将他们的应用发布到苹果App Store上&#xff0c;但是&#xff0c;有时候他们可能希望通过直接分享IPA文件来分发他们的App&#xff0c;那…...

IDEA pom.xml显示灰色并被划线

在使用 IDEA 进行开发的过程中&#xff0c;有时候会遇到 pom.xml 显示灰色并被划线的情况&#xff0c;如下图&#xff1a; 这一般是因为该文件被 Maven 忽略导致的&#xff0c;可以进行如下操作恢复&#xff1a; 设置保存后&#xff0c;可以看到 pom.xml 恢复了正常&#xff1a…...

玄子Share-使用 Pycharm 执行 Shell 脚本

玄子Share-使用 Pycharm 执行 Shell 脚本 Why&#xff1f; 为什么我要使用 Pycharm 执行 Shell 脚本呢&#xff0c;我直接使用 Linux 不行吗&#xff1f; 使用 Pycharm 执行 Shell 脚本的好处 我们的宿主机都是 WIndows 平台&#xff0c;若想编译 Shell 脚本&#xff0c;我…...

如何让Nrf connect、EFR connect直接显示特征值数据及其单位

效果如图&#xff1a;app直接显示了我的温度&#xff0c;并且有两位小数&#xff0c;还有温度单位。这是怎么做到的呢&#xff1f; 这次我们仍以TLS8258为例&#xff0c;当然如果是其他蓝牙芯片&#xff0c;配置方式也是大差不差&#xff0c;规则一样的。 #define GATT_CHARA…...

python笔记

Vim 修改文件格式 unix|dos vim fileName :set ff //显示出文件格式类型 :set ffunix //设置成unix格式 :set ffdos //windows文件格式python *和**的区别 将可变关键字打包成不可变的元组 def func(*args): print(args) func(1, 2, 3) # 输出&#xff1a;(1, 2, 3)…...

Java编译期注解处理器AbstractProcessor使用

我们接触的注解主要分为以下两类 运行时注解&#xff1a;通过反射在运行时动态处理注解的逻辑编译时注解&#xff1a;通过注解处理器在编译期动态处理相关逻辑 编译期注解我们常用的有Lombok&#xff0c;在class文件中自动生成get和set方法 解编译期处理流程最关键的一个类就…...

JetBrains相关的IDE有哪些?

JetBrains是一家成立于2002年的捷克软件开发公司&#xff0c;总部位于捷克的布拉格&#xff0c;同时在俄罗斯的圣彼得堡及美国麻州波士顿等地设有办公室。该公司以其高质量的集成开发环境&#xff08;IDE&#xff09;产品而闻名&#xff0c;这些产品被广泛应用于各种编程语言和…...

Git-常规用法-含解决分支版本冲突解决方法

目录 前置条件 已经创建了Gitee账号 创建一个远程仓库 Git的优点 版本控制 Git 下载 Git的使用 检查Git的是否安装成功 git的常用命令 常用流程 Git 分支 分支流程 Git 远程仓库 远程仓库流程 特殊 可能遇到的问题 前置条件 已经创建了Gitee账号 创建一个远程仓…...

基于springboot实现大型商场应急预案管理系统项目【项目源码+论文说明】

基于SpringBoot实现大型商场应急预案管理系统演示 摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了大型商场应急预案管理系统的开发全过程。通过分析大型商场应急预案管理系统管理的不足&#xff0c;创建了一个…...

系统学c#:1、基础准备(软件下载与安装)

一、Vs软件下载与安装 访问Visual Studio官方网站&#xff1a; https://visualstudio.microsoft.com/zh-hans/downloads 下载Visual Studio 运行exe文件&#xff0c;点击“继续” 初始文件安装完成后选择我们需要安装的项&#xff0c;并勾选好必要的单个组件&#xff0c;设…...

解决CSS中鼠标移入到某个元素其子元素被遮挡的问题

我们在开发中经常遇到一种场景&#xff0c;就是给元素加提示信息&#xff0c;就是鼠标移入到盒子上面时&#xff0c;会出现提示信息这一功能&#xff0c;如果我们给盒子加了hover&#xff0c;当鼠标移入到盒子上时&#xff0c;让他往上移动5px&#xff0c;即transform: transla…...

【华为OD机试】虚拟理财游戏【C卷|100分】

【华为OD机试】-真题 !!点这里!! 【华为OD机试】真题考点分类 !!点这里 !! 题目描述 在一款虚拟游戏中生活,你必须进行投资以增强在虚拟游戏中的资产以免被淘汰出局。 现有一家Bank,它提供有若干理财产品 m 个,风险及投资回报不同,你有 N(元)进行投资,能接收的总风险…...

ssh 使用

ssh 使用 一、ssh 安装二、ssh 使用1. ssh 登录2. ssh-keygen 免密登录(1) ssh 生成密钥(2) 开启远程主机的密钥登陆(3) ssh 分发公钥 3. ssh-copy-id 复制公钥到远程主机4. scp 复制 系统环境: linux(ubuntu,debian,kali) 一、ssh 安装 sudo apt update sudo apt install op…...

QMC5883L的驱动

简介 本篇文章的代码已经上传到了github上面&#xff0c;开源代码 作为一个电子罗盘模块&#xff0c;我们可以通过I2C从中获取偏航角yaw&#xff0c;相对于六轴陀螺仪的yaw&#xff0c;qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...

BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践

6月5日&#xff0c;2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席&#xff0c;并作《智能体在安全领域的应用实践》主题演讲&#xff0c;分享了在智能体在安全领域的突破性实践。他指出&#xff0c;百度通过将安全能力…...

今日科技热点速览

&#x1f525; 今日科技热点速览 &#x1f3ae; 任天堂Switch 2 正式发售 任天堂新一代游戏主机 Switch 2 今日正式上线发售&#xff0c;主打更强图形性能与沉浸式体验&#xff0c;支持多模态交互&#xff0c;受到全球玩家热捧 。 &#x1f916; 人工智能持续突破 DeepSeek-R1&…...

C++ Visual Studio 2017厂商给的源码没有.sln文件 易兆微芯片下载工具加开机动画下载。

1.先用Visual Studio 2017打开Yichip YC31xx loader.vcxproj&#xff0c;再用Visual Studio 2022打开。再保侟就有.sln文件了。 易兆微芯片下载工具加开机动画下载 ExtraDownloadFile1Info.\logo.bin|0|0|10D2000|0 MFC应用兼容CMD 在BOOL CYichipYC31xxloaderDlg::OnIni…...

python报错No module named ‘tensorflow.keras‘

是由于不同版本的tensorflow下的keras所在的路径不同&#xff0c;结合所安装的tensorflow的目录结构修改from语句即可。 原语句&#xff1a; from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后&#xff1a; from tensorflow.python.keras.lay…...

浪潮交换机配置track检测实现高速公路收费网络主备切换NQA

浪潮交换机track配置 项目背景高速网络拓扑网络情况分析通信线路收费网络路由 收费汇聚交换机相应配置收费汇聚track配置 项目背景 在实施省内一条高速公路时遇到的需求&#xff0c;本次涉及的主要是收费汇聚交换机的配置&#xff0c;浪潮网络设备在高速项目很少&#xff0c;通…...

A2A JS SDK 完整教程:快速入门指南

目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库&#xff…...

C#中的CLR属性、依赖属性与附加属性

CLR属性的主要特征 封装性&#xff1a; 隐藏字段的实现细节 提供对字段的受控访问 访问控制&#xff1a; 可单独设置get/set访问器的可见性 可创建只读或只写属性 计算属性&#xff1a; 可以在getter中执行计算逻辑 不需要直接对应一个字段 验证逻辑&#xff1a; 可以…...

Go 并发编程基础:通道(Channel)的使用

在 Go 中&#xff0c;Channel 是 Goroutine 之间通信的核心机制。它提供了一个线程安全的通信方式&#xff0c;用于在多个 Goroutine 之间传递数据&#xff0c;从而实现高效的并发编程。 本章将介绍 Channel 的基本概念、用法、缓冲、关闭机制以及 select 的使用。 一、Channel…...

Redis:现代应用开发的高效内存数据存储利器

一、Redis的起源与发展 Redis最初由意大利程序员Salvatore Sanfilippo在2009年开发&#xff0c;其初衷是为了满足他自己的一个项目需求&#xff0c;即需要一个高性能的键值存储系统来解决传统数据库在高并发场景下的性能瓶颈。随着项目的开源&#xff0c;Redis凭借其简单易用、…...