【Mysql】请问怎么在程序中实现show databases\show table功能

java
恩,就是突然想到在java程序里面能不能查询出数据库有哪些表,表里有哪些字段、类型、注释这些

就像实现了一个微型的数据库GUI的样子,就像phpadmin那样,怎么查出来的呢?

提供两个sql可以查询。

 -- 查询表信息
SELECT
    table_name,
    table_comment 
FROM
    information_schema.`TABLES` 
WHERE
    table_schema = ( SELECT DATABASE ( ) );

-- 查询某表的列信息
SELECT
    column_name columnName,
    data_type dataType,
    column_comment columnComment
FROM
    information_schema.COLUMNS 
WHERE
    table_name = #{tableName} and table_schema = (select database()) order by ordinal_position

本质上还是jdbc查询,用jdbc执行mysql的这些sql获取结果集返回到应用

建议注入jdbcTemplate,进行操作,很方便的

提供一个查MySQL查数据库表信息的SQL

 select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns
where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position