java 已经获取某个mysql数据库的所有表名 创建JTree
publicstaticvoidgetTable()throwsSQLException{ResultSetrsgettable=null;st=conn.createS...
public static void getTable() throws SQLException{ ResultSet rsgettable=null; st=conn.createStatement(); rsgettable=st.executeQuery("show tables"); while (rsgettable.next()){ System.out.println(rsgettable.getString(1)); } rsgettable.close(); }
这段代码可以获取某个mysql数据库的所有表名 ,但是如何根据这些表名创建一个JTree呢?求解? 展开
这段代码可以获取某个mysql数据库的所有表名 ,但是如何根据这些表名创建一个JTree呢?求解? 展开
2014-01-12
展开全部
那只能创建一层的JTree ?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class JTreeDemo2 extends JFrame {
private Connection con;
private Statement ste = null;
ResultSet rs;
JTreeDemo2() {
super("JTreeDemo");
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OpenDb();
DefaultMutableTreeNode node = new DefaultMutableTreeNode("所有表");
createNodes(node, "0");
JTree tree = new JTree(node);
this.add(tree);
this.setVisible(true);
}
public void createNodes(DefaultMutableTreeNode parent, String ParentID){
DefaultMutableTreeNode temp = null;
String strsql = "show tables";
// 光标的相对位置
try{
rs = ste.executeQuery(strsql);
while (rs.next()){
temp = new DefaultMutableTreeNode(rs.getString(1));
parent.add(temp);
}
} catch(Exception e){
System.out.println(e);
}
}
// 连接数据库
void OpenDb(){
String driver = "com.mysql.jdbc.Driver";
String connection = "jdbc:mysql://localhost:3306/test";
// 数据库用户名
String user = "root";
// 数据库密码
String password = "root";
try {
Class.forName(driver);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
con = DriverManager.getConnection(connection, user, password);
ste = con.createStatement();
}catch (SQLException e){
e.printStackTrace();
}
}
public static void main(String[] argv) throws Exception {
new JTreeDemo2();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询