| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 241 人关注过本帖
标题:数据库表结构可视化
只看楼主 加入收藏
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:111
帖 子:1788
专家分:7795
注 册: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();
    }
}

附件: 您没有浏览附件的权限,请 登录注册
2019-01-27 17:39
疯狂的小a
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:29
帖 子:404
专家分:1787
注 册:2018-2-6
  得分:7 
可视化完了之后,怎么在可视化窗口内修改表的字段属性呢

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

知命以下无敌
2019-01-27 18:01
wp231957
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:373
帖 子:12992
专家分:50319
注 册:2012-10-18
  得分:7 
回复 3楼 林月儿
这个是CUI 还是GUI

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

知命以下无敌
2019-01-29 14:42







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

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