| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2843 人关注过本帖
标题:数据库表结构可视化
只看楼主 加入收藏
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
数据库表结构可视化
程序代码:
package com.huawei.test;

import java.awt.BorderLayout;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

import javax.swing.*;

public class DBTest0127 extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8";
    private static final String USER = "root";
    private static final String PASSWORD = "123";
    private static Connection getConn() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    private static void close(Connection conn) {
        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    private static void close(PreparedStatement preparedStatement, ResultSet resultSet) {
        try {
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private static List<String> getTables() {
        Connection conn = getConn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<String> list = new ArrayList<>();
        try {
            preparedStatement = conn.prepareStatement("show tables");
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                list.add(resultSet.getString("Tables_in_test"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close(preparedStatement, resultSet);
        close(conn);
        return list;
    }
    private static List<Map<String, String>> getTableInfo(String tableName) {
        String sql = "SELECT COLUMN_NAME, COLUMN_TYPE,"
                +"IS_NULLABLE,COLUMN_DEFAULT,COLUMN_COMMENT "
                + "FROM INFORMATION_SCHEMA.COLUMNS "
                + "where table_name = '@1'"; 
        List<Map<String, String>> result = new ArrayList<>();
        Connection conn = getConn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<String> names = Arrays.asList("COLUMN_NAME", "COLUMN_TYPE", "IS_NULLABLE",
                "COLUMN_DEFAULT", "COLUMN_COMMENT"); 
        try {
            preparedStatement = conn.prepareStatement(sql.replace("@1", tableName));
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                Map<String, String> map = new HashMap<>();
                for(String name : names) map.put(name, resultSet.getString(name));
                result.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close(preparedStatement, resultSet);
        close(conn);
        return result;
    }
    public DBTest0127() {
        setSize(400, 400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("DBTest");
        List<String> list = getTables();
        JList<?> jls = new JList<>(list.toArray());
        add(new JScrollPane(jls), BorderLayout.WEST);
        validate();
        jls.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String selVal = jls.getSelectedValue().toString();
                List<Map<String, String>> tableData = getTableInfo(selVal);
                JTable table = getJtable(tableData);
                JScrollPane jsp = new JScrollPane(table);
                add(jsp, BorderLayout.CENTER);
                DBTest0127.this.revalidate();
            }
        });
    }
    private static JTable getJtable(List<Map<String, String>> list) {
        String[] columnNames = {"列名", "数据类型", "是否为空", "默认值", "备注"};
        List<String> names = Arrays.asList("COLUMN_NAME", "COLUMN_TYPE",
                "IS_NULLABLE", "COLUMN_DEFAULT", "COLUMN_COMMENT"); 
        String[][] rowData = new String[list.size()][];
        int rowIndex = 0;
        for(Map<String, String> map : list) {
            rowData[rowIndex] = new String[names.size()];
            for(int i = 0; i < names.size(); i++) {
                rowData[rowIndex][i] = map.get(names.get(i)); 
            }
            rowIndex++;
        }
        JTable table = new JTable(rowData, columnNames);
        return table;

    }
    public static void main(String[] args) {
        new DBTest0127();
    }
}

图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: String conn List new Map 
2019-01-27 17:39
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:7 
可视化完了之后,怎么在可视化窗口内修改表的字段属性呢

假如人生没有梦想,和咸鱼有什么区别!
2019-01-27 17:50
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
我比较菜,不会了,要不您给补充一下?

剑栈风樯各苦辛,别时冰雪到时春
2019-01-27 18:01
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:7 
回复 3楼 林月儿
这个是CUI 还是GUI

DO IT YOURSELF !
2019-01-29 14:38
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
回复 4楼 wp231957
gui

剑栈风樯各苦辛,别时冰雪到时春
2019-01-29 14:42
快速回复:数据库表结构可视化
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035011 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved