您当前的位置: 首页 >  sql

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SQL 元数据 工具类

梁云亮 发布时间:2022-04-27 17:11:58 ,浏览量:2

SQL语句参考博客:https://hcshow.blog.csdn.net/article/details/104271739

工具类

public class DBUtil{
    /**
     * 获取数据表中所有的列的名字
     * @param database
     * @param table
     * @return
     * @throws SQLException
     */
    public static String[] getTableColumns(String database, String table) throws SQLException {
        String sql = "select group_concat(column_name) from information_schema.COLUMNS   where table_schema= ? and table_name=?";
        QueryRunner queryRunner = new QueryRunner(TransactionManager.getDataSource());
        String columns = queryRunner.query(sql, new ScalarHandler(), database, table);
        String[] res = columns.split(",");
        return res;
    }

    /**
     * 获取数据库中所有的表的名字
     * @param database
     * @return
     * @throws SQLException
     */
    public static String[] getTableNames(String database) throws SQLException {
        String sql = "SELECT GROUP_CONCAT(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? ORDER BY create_time DESC";
        QueryRunner queryRunner = new QueryRunner(TransactionManager.getDataSource());
        String columns = queryRunner.query(sql, new ScalarHandler(), database);
        String[] res = columns.split(",");
        return res;
    }

    public static void main(String[] args) throws SQLException {
        String database = "db_credits";
        String table = "tb_user";
        String[] res = getTableColumns(database, table);
        System.out.println(Arrays.toString(res));

        String[] res2 = getTableNames(database);
        System.out.println(Arrays.toString(res2));
    }
}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.1432s