数据库表结构可视化
程序代码:
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(); } }