标题:数据库表结构可视化
取消只看楼主
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
结帖率:100%
已结贴  问题点数:20 回复次数:2 
数据库表结构可视化
程序代码:
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
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:0 
我比较菜,不会了,要不您给补充一下?

剑栈风樯各苦辛,别时冰雪到时春
2019-01-27 18:01
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:0 
回复 4楼 wp231957
gui

剑栈风樯各苦辛,别时冰雪到时春
2019-01-29 14:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-492863-1-1.html




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

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