通过连接数据库来动态的生成树的问题

03-06-24 alun
看看这段代码,我想要实现的是通过连接数据库来动态的生成树,

问题是在“////////”处

import javax.swing.*;

import javax.swing.tree.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

public class TreeView1 extends JApplet {

JTextArea textArea = new JTextArea();

String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=login";

String user="sa";

String password="";

ResultSet rs;

ResultSet rs2;

public void init() {

Class.forName(sDBDriver); //连接数据库并读出相关字段

Connection conn= DriverManager.getConnection(url,user,password);

Statement

stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

String sql="select * from modules where m_parentId=0";

rs=stmt.executeQuery(sql);

while(rs.next()) {

int id=rs.getInt(1);

String iid=rs.getString(3);

stmt=conn.createStatement();

DefaultMutableTreeNode root = new DefaultMutableTreeNode(iid);

rs2 = stmt.executeQuery("select * from modules where m_parentId='"+id+"'");

while (rs2.next()) {

String iid1=rs2.getString(3);

stmt=conn.createStatement();

DefaultMutableTreeNode ParenId1 = new DefaultMutableTreeNode(iid1);

root.add(ParenId1);

}

JTree tree=new JTree(root); //生成树

JScrollPane scrollpane=new JScrollPane(tree);

Container con=getContentPane();

con.add(scrollpane);}

/*rs.close();

stmt.close();

conn.close();*/

JSplitPane splitPane = new JSplitPane(

JSplitPane.HORIZONTAL_SPLIT,

new JScrollPane(tree), ////////错误是找不到我这个包里面的tree

splitPane.setDividerLocation(150);

textArea.setFont(new Font("Serif", Font.PLAIN, 17));

getContentPane().add(splitPane, BorderLayout.CENTER);

tree.addTreeSelectionListener( ////////这里的提示同上

new TreeSelectionListener() {

public void valueChanged(TreeSelectionEvent e) {

TreePath path = e.getNewLeadSelectionPath();

String s = new String();

if(path != null) {

s += "New lead selection path: " +

path.toString() + "\n";

}

else

s += "selection cleared\n";

path = e.getOldLeadSelectionPath();

if(path != null) {

s += "Old lead selection path: " +

path.toString() + "\n";

}

else

s += "No previous lead selection\n";

textArea.append(s + "\n");

printSelectionInformation(e);

}

void printSelectionInformation(TreeSelectionEvent e) {

showPaths(e);

textArea.append("\n----------------------------");

textArea.append("----------------------------\n");

}

private void showPaths(TreeSelectionEvent e) {

TreePath[] paths = e.getPaths();

textArea.append("Number of Paths: " +

paths.length + "\n");

for(int i=0; i < paths.length; ++i) {

TreePath path = paths;

boolean wasAdded = e.isAddedPath(path);

textArea.append(" path " + i + ": ");

textArea.append(path +

(wasAdded ? " added to selection" :

" removed from selection") + "\n");

}

}

});

}

}

请各位看看,如果有代码帖上来看看也可以,谢谢!

猜你喜欢