发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 大数据 扩展性 并发编程 事件驱动 分布式 SOA

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

2003-06-24 13:26
赞助商链接

看看这段代码,我想要实现的是通过连接数据库来动态的生成树,
问题是在“////////”处
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");
}
}
});
}
}

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

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com