1 package com.databases.jtree;
2
3 import java.awt.FlowLayout;
4 import java.awt.GridLayout;
5 import java.awt.event.MouseAdapter;
6 import java.awt.event.MouseEvent;
7 import java.awt.event.MouseListener;
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10 import java.sql.ResultSet;
11 import java.sql.ResultSetMetaData;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 import java.util.ArrayList;
15
16 import javax.swing.JFrame;
17 import javax.swing.JOptionPane;
18 import javax.swing.JPanel;
19 import javax.swing.JScrollPane;
20 import javax.swing.JTable;
21 import javax.swing.JTree;
22 import javax.swing.plaf.PanelUI;
23 import javax.swing.table.DefaultTableModel;
24 import javax.swing.tree.DefaultMutableTreeNode;
25 import javax.swing.tree.TreePath;
26
27 public class MainFrame extends JFrame {
28 private Connection conn; // 数据库连接对象
29 ResultSet rs;
30 Statement st;
31 private JTree tree;
32 ArrayList<String> list = new ArrayList<>();
33 private DefaultMutableTreeNode databases;
34 public static JTable jtable;
35 public static JScrollPane jScrollPane;
36
37 public static JPanel p1 = new JPanel();
38 public static JPanel p2 = new JPanel();
39 public static JPanel p3 = new JPanel();
40
41 public static void main(String[] args) throws SQLException {
42 new MainFrame();
43 }
44
45 public MainFrame() throws SQLException {
46 super("数据库管理");
47 this.setBounds(400, 300, 780, 480);
48 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
49 this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
50 databases = new DefaultMutableTreeNode("数据库管理 ");
51 tree = new JTree(databases);
52 tree.setSize(600, 600);
53 tree.setLocation(400, 300);
54
55 p2.add(new JScrollPane(tree));
56 p3.add(jScrollPane = new JScrollPane(jtable));
57 p1.setLayout(new GridLayout(1, 2));
58 p1.add(p2);
59 p1.add(p3);
60 this.add(p1);
61
62 conDB("information_schema");
63 String sql = "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA";
64 rs = st.executeQuery(sql);
65 while (rs.next()) {
66 System.out.println(rs.getString("SCHEMA_NAME"));
67 list.add(rs.getString("SCHEMA_NAME"));
68 }
69 closeDB();
70
71 for (int i = 0; i < list.size(); i++) {
72 DefaultMutableTreeNode roots = new DefaultMutableTreeNode(list.get(i));
73 databases.add(roots);
74 conDB("information_schema");
75 String sql1 = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '" + list.get(i)
76 + "';";
77 rs = st.executeQuery(sql1);
78 while (rs.next()) {
79 System.out.println(rs.getString("TABLE_NAME"));
80 DefaultMutableTreeNode tables = new DefaultMutableTreeNode(rs.getString("TABLE_NAME"));
81 roots.add(tables);
82 }
83 closeDB();
84 }
85 MouseListener ml = new MouseAdapter() {
86 public void mousePressed(MouseEvent e) {
87 int selRow = tree.getRowForLocation(e.getX(), e.getY());
88 TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
89 if (selRow != -1) {
90 if (e.getClickCount() == 1) {
91 try {
92 mySingleClick(selRow, selPath);
93 } catch (SQLException e1) {
94 e1.printStackTrace();
95 }
96 System.out.println("长度" + selPath.getPathCount());
97 }
98 }
99 }
100 };
101
102 tree.addMouseListener(ml);
103 this.setVisible(true);
104 }
105 public void mySingleClick(int selRow, TreePath selPath) throws SQLException {
106 if (selPath.getPathCount() == 3) {
107 String tableName = selPath.getLastPathComponent().toString();
108 String databasesName = selPath.getPathComponent(1).toString();
109 conDB(databasesName);
110 JTable jtable = new JTable();
111 jtable= query(tableName);
112 p3.removeAll();
113 jScrollPane = new JScrollPane(jtable);
114 p3.add(MainFrame.jScrollPane);
115 p3.updateUI();
116 closeDB();
117 }
118 }
119 // 以下是连接数据库
120 public void conDB(String DBName) {
121 try {
122 // 加载数据库驱动
123 Class.forName("com.mysql.jdbc.Driver");
124 } catch (ClassNotFoundException e) {
125 JOptionPane.showMessageDialog(null, "数据库加载失败!");
126 }
127 try {
128 // 连接数据库
129 String url = "jdbc:mysql://localhost:3306/" + DBName + "";
130 System.out.println(url);
131 String user = "root";
132 String passwd = "10521";
133 conn = DriverManager.getConnection(url, user, passwd);
134 st = conn.createStatement();
135 } catch (SQLException e) {
136 JOptionPane.showMessageDialog(null, "数据库连接失败!");
137 }
138 }
139
140 // 以下是关闭数据库
141 public void closeDB() {
142 try {
143 conn.close();
144 } catch (SQLException e) {
145 JOptionPane.showMessageDialog(null, "数据库关闭失败!");
146 }
147 }
148
149 public JTable query(String table) throws SQLException {
150 DefaultTableModel tablemodel = new DefaultTableModel();
151 String sql = "SELECT * FROM " + table + ";";
152 Statement stmt = this.conn.createStatement(); // 创建语句对象
153 ResultSet rest = stmt.executeQuery(sql); // 执行数据查询SELECT语句
154 ResultSetMetaData rsmd = rest.getMetaData(); // 返回表属性对象
155 int count = rsmd.getColumnCount(); // 获得列数
156 for (int j = 1; j <= count; j++) // 将各列名添加到表格模型作为标题,列序号>=1
157 tablemodel.addColumn(rsmd.getColumnLabel(j)); // 将结果集中各行数据添加到表格模型
158 Object[] columns = new Object[count]; // 创建对象数组,数组长度为列数
159 while (rest.next()) // 迭代遍历结果集,从前向后访问每行
160 {
161 for (int j = 1; j <= columns.length; j++) // 获得每行各列值
162 columns[j - 1] = rest.getString(j);
163 tablemodel.addRow(columns); // 表格模型添加一行,参数指明各列值
164 }
165 rest.close();
166 stmt.close();
167 return new JTable(tablemodel);
168 }
169 }