java读取mysql表的注释及字段注释
/**
* 读取mysql某数据库下表的注释信息
*
* @author xxx
*/
public class MySQLTableComment {
public static Connection getMySQLConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseName", "root", "root");
return conn;
} /**
* 获取当前数据库下的所有表名称
* @return
* @throws Exception
*/
public static List getAllTableName() throws Exception {
List tables = new ArrayList();
Connection conn = getMySQLConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SHOW TABLES ");
while (rs.next()) {
String tableName = rs.getString(1);
tables.add(tableName);
}
rs.close();
stmt.close();
conn.close();
return tables;
} /**
* 获得某表的建表语句
* @param tableName
* @return
* @throws Exception
*/
public static Map getCommentByTableName(List tableName) throws Exception {
Map map = new HashMap();
Connection conn = getMySQLConnection();
Statement stmt = conn.createStatement();
for (int i = 0; i < tableName.size(); i++) {
String table = (String) tableName.get(i);
ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + table);
if (rs != null && rs.next()) {
String createDDL = rs.getString(2);
String comment = parse(createDDL);
map.put(table, comment);
}
rs.close();
}
stmt.close();
conn.close();
return map;
}
/**
* 获得某表中所有字段的注释
* @param tableName
* @return
* @throws Exception
*/
public static void getColumnCommentByTableName(List tableName) throws Exception {
Map map = new HashMap();
Connection conn = getMySQLConnection();
Statement stmt = conn.createStatement();
for (int i = 0; i < tableName.size(); i++) {
String table = (String) tableName.get(i);
ResultSet rs = stmt.executeQuery("show full columns from " + table);
System.out.println("【"+table+"】");
// if (rs != null && rs.next()) {
//map.put(rs.getString("Field"), rs.getString("Comment"));
while (rs.next()) {
// System.out.println("字段名称:" + rs.getString("Field") + "\t"+ "字段注释:" + rs.getString("Comment") );
System.out.println(rs.getString("Field") + "\t:\t"+ rs.getString("Comment") );
}
// }
rs.close();
}
stmt.close();
conn.close();
// return map;
} /**
* 返回注释信息
* @param all
* @return
*/ public static String parse(String all) {
String comment = null;
int index = all.indexOf("COMMENT='");
if (index < 0) {
return "";
}
comment = all.substring(index + 9);
comment = comment.substring(0, comment.length() - 1);
return comment;
} public static void main(String[] args) throws Exception {
List tables = getAllTableName();
Map tablesComment = getCommentByTableName(tables);
Set names = tablesComment.keySet();
Iterator iter = names.iterator();
while (iter.hasNext()) {
String name = (String) iter.next();
System.out.println("Table Name: " + name + ", Comment: " + tablesComment.get(name));
} getColumnCommentByTableName(tables);
}
java读取mysql表的注释及字段注释的更多相关文章
- mysql:表注释和字段注释
mysql:表注释和字段注释 1 创建表的时候写注释 create table test1 ( field_name int comment '字段的注释' )comment='表的注释'; 2 修改 ...
- 查看文章 mysql:表注释和字段注释
查看文章 mysql:表注释和字段注释 学习了:https://blog.csdn.net/chamtianjiao/article/details/6698690 2 修改表的注释 alter ta ...
- 让hive的表注释和字段注释支持中文
此处用的数据库类型为mysql.发现hive在初始化创建这些表的时候,大部分字段的字符集给设置成了latin1,然后collation设成了latin1_bin. 但是我们在hive中创建表时,表注释 ...
- Django之集合函数使用与mysql表的创建特殊字段分析
1. 集合函数的使用场景: -- 单独使用: 不分组, 只查聚合结果 -- 分组使用: 按字段分组, 可查询分组字段与聚合结果 2. 导入聚合函数 from django.db.models impo ...
- Oracle 查询表注释以及字段注释
Oracle 查询表注释以及字段注释 --表字段信息 select * from all_tab_columns a where a.TABLE_NAME='T_X27_USER'; --表注释信息 ...
- mysql查看表注释和字段注释的方法
1.取字段注释 Select COLUMN_NAME 列名, DATA_TYPE 字段类型, COLUMN_COMMENT 字段注释from INFORMATION_SCHEMA.COLUMNSWhe ...
- 查看文章 mysql:表注释和字段注释[转]
1 创建表的时候写注释 create table test1 ( field_name int comment '字段的注释' )comment='表的注释'; 2 修改表的注释 alter tabl ...
- mysql添加表注释、字段注释、查看与修改注释
1 创建表的时候写注释create table test1( field_name int comment '字段的注释')comment='表的注释'; 2 修改表的注释alter table te ...
- mysql中查看所有表、表字段、表注释、字段注释
查看所有表和表注释 select TABLE_NAME, TABLE_COMMENT from INFORMATION_SCHEMA.Tables where table_schema = '某数据库 ...
随机推荐
- CSS基础:块级元素与盒模型
简介 在 HTML4.01 中,元素通常可以分为块级元素( “Block-level element” ) 和内联元素 ( "Inline-level element" ) 两大类 ...
- api-gateway实践(15)3.6JL分支和3.7并行改造需求
一.名称改为"API网关" --哪个地方的名称?二.开发者视图中,API网关显示两个视图. 1. 服务分类视图:支持按照业务分为多个类别,分类方式参照应用服务化的分类:人像比对.自 ...
- 新概念英语(1-117)Tommy's breakfast
Lesson 117 Tommy's breakfast 汤米的早餐 Listen to the tape then answer this question. What does she mean ...
- SpringCloud的Bus(一)消息中间件的概念和用途
一.概念与定义 1.Message Broker Message Broker是一种消息验证.消息转换.消息路由的架构模式,用于如: 消息路由到一个或多个目的地 消息转化为其他的表现方式 执行消息的聚 ...
- 解决Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Student_recruit]]
查看web.xml文件的书写,特别注意路径与命名一致
- javascript 函数的4种调用方式与 this(上下文)的指向
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! javascript中作用域链和this(上下文)的指向是很容易混淆的,简单的说就是: 作用域链取决于函数 ...
- 浮动和BFC的学习整理转述
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 文档流的概念:html中block块元素默认是单独占据一行的,从上到下排列,也就是我们说的文档流; 脱离文 ...
- Web SCADA 电力接线图工控组态编辑器
前言 SVG并非仅仅是一种图像格式, 由于它是一种基于XML的语言,也就意味着它继承了XML的跨平台性和可扩展性,从而在图形可重用性上迈出了一大步.如SVG可以内嵌于其他的XML文档中,而SVG文档中 ...
- WebGL文字渲染的那些问题
THREE.js开发的应用运行在iphone5下发现有些时候会崩溃,跟了几天发现是因为Sprite太多频繁更新纹理占用显存导致的.通常解决纹理频繁更新问题就要用到one draw all方法,放到纹理 ...
- dev gridControl 自定义绘制列头颜色
1.添加事件CustomDrawColumnHeader private void gvw1_CustomDrawColumnHeader(object sender, DevExpress.Xtra ...