[求助]数据库与树简单连接的代码 ??

hooligan000112003 2004-08-11 05:39:19
数据库与树简单连接的代码 ??
我的数据库中有“Id”,"treeName","f_fatherId"

谢谢!!
...全文
86 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hooligan000112003 2004-08-11
  • 打赏
  • 举报
回复
问题已解决,但是又碰到问题:单击节点事件是什么??

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.sql.*;


public class tree
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Statement stmt1 = null;
ResultSet rs1 = null;

// int childCount = 0;
String s = null;
String s1 = null;
int count =0;

public tree()
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:tree");
System.out.println("ok");



}catch(Exception e)
{
e.printStackTrace();
}



JFrame f = new JFrame("firstTree");

Container contentPane = f.getContentPane();


try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("select * from TreeExample where f_fatherid='1'");


while(rs.next())
{
count++;
}


DefaultMutableTreeNode root=new DefaultMutableTreeNode("ROOT");
DefaultTreeModel tree1=new DefaultTreeModel(root,true);

rs.first();//将光标移动第一条记录

for(int i=1;i<=count;i++)
{
s=String.valueOf(i);

DefaultMutableTreeNode s =new DefaultMutableTreeNode(rs.getString("f_treename"),true);
tree1.insertNodeInto(s,root,root.getChildCount());



String id = rs.getString("f_id");

stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs1 = stmt1.executeQuery("select * from TreeExample where f_fatherid='"+id+"'");



int childCount = 0;
while(rs1.next())
{
childCount++;
}


rs1.first();
if(childCount !=0 )
{

for(int j=1;j<=childCount;j++)
{
s1 = String.valueOf(j);
DefaultMutableTreeNode s1 =new DefaultMutableTreeNode(rs1.getString("f_treename"));
tree1.insertNodeInto(s1,s,s.getChildCount());
rs1.next();
}


}
rs.next();

}

JTree tree = new JTree(tree1);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);

contentPane.add(scrollPane);
f.pack();
f.setVisible(true);

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});


}
catch(Exception e)
{
e.printStackTrace();
}



}

public static void main(String args[]) {

new tree();
}
}
hooligan000112003 2004-08-11
  • 打赏
  • 举报
回复
ding

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧