Jtree绑定数据库并读取内容生成树状结构

90sdemo 发布于 2015/05/20 15:33
阅读 1K+
收藏 0
已经可以用连接数据库,并能读出数据库中表,但是要想在读出表中的字段,该怎么办啊?求指教,这是我的代码
package com.yang.tree;
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 eee extends JFrame {  
    private Connection con;  
    private Statement ste = null;  
    ResultSet rs;  
   
    eee() {
        super("eee");
        this.setSize(200, 200);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        OpenDb();
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("db_yang");  
        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 = "select name from sysobjects where xtype='U'";  
        
        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.microsoft.sqlserver.jdbc.SQLServerDriver";  
        String connection = "jdbc:sqlserver://localhost:1433; "
				+ "DatabaseName=db_yang";  
        // 数据库用户名  
        String user = "sa";  
        // 数据库密码  
        String password = "19921221";  
        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 eee();  
   
    }  
}


这是我读出来数据库中的表,比如想把表table_1里的字段读出来,并且插入到table_1节点下,该怎么办啊?



加载中
返回顶部
顶部