5

  dtree+JQuery动态生成树.思路其实很简单...  首先把树的节点信息存储到数据库,然后在servlet或jsp中获取数据库表中的数据,把这些信息写成在xml文件中.然后界面jsp页面通过JQuery实现对改servlet的请求.并且回调方法中接受xml数据对象.并且遍历xml文件,取得xml文件中的节点的属性或文本数据.再循环的对dtree添加节点.。

 

1 . 在 MYSQL 中的test数据库中创建表.tree_table. 

     SQL语句.

Sql代码 复制代码
  1. tree_table  CREATE TABLE `tree_table` (                       
  2.               `id` int(11) NOT NULL auto_increment,           
  3.               `nodeId` varchar(12) NOT NULL  ,       
  4.               `parentId` varchar(12) NOT NULL  ,     
  5.               `hrefAddress` varchar(85)  ,           
  6.               `nodeName` varchar(20)  ,              
  7.               PRIMARY KEY  (`id`)                             
  8.             ) ENGINE=InnoDB DEFAULT CHARSET=gbk      

 

2.连接数据库的类.主要是读取表中的数据.

       

Java代码 复制代码
  1. package com.dao;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.DriverManager;   
  5. import java.sql.PreparedStatement;   
  6. import java.sql.ResultSet;   
  7. import java.sql.SQLException;   
  8. import java.util.ArrayList;   
  9.   
  10. public class DaoTest {   
  11.     Connection con = null;   
  12.   
  13.     public Connection getConnection() {   
  14.         Connection conn = null;   
  15.         String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk";   
  16.         String user = "root";   
  17.         String password = "admin";   
  18.         try {   
  19.             if (conn == null) {   
  20.                 Class.forName("com.mysql.jdbc.Driver").newInstance();   
  21.                 conn = DriverManager.getConnection(url, user, password);   
  22.             }   
  23.         } catch (Exception e) {   
  24.             System.out.println("连接失败");   
  25.             return null;   
  26.         } finally {   
  27.             url = null;   
  28.             user = null;   
  29.             password = null;   
  30.         }   
  31.         return conn;   
  32.     }   
  33.   
  34.     public ArrayList<Nodes> getNodeInfo() {   
  35.         String sql = "select nodeId ,parentId ,hrefAddress ,nodeName from tree_table order by id ";   
  36.         PreparedStatement pre = null;   
  37.         Connection conn = null;   
  38.         conn = getConnection();   
  39.         ResultSet rs = null;   
  40.         ArrayList<Nodes> list = new ArrayList<Nodes>();   
  41.         try {   
  42.             pre = conn.prepareStatement(sql);   
  43.             rs =pre.executeQuery();   
  44.             while (rs.next()){   
  45.                 Nodes node = new Nodes();   
  46.                 node.setHrefAddress(rs.getString("hrefAddress"));   
  47.                 node.setNodeId(rs.getString("nodeId"));   
  48.                 node.setParentId(rs.getString("parentId"));   
  49.                 node.setNodeName(rs.getString("nodeName"));   
  50.                 list.add(node);   
  51.             }   
  52.             rs.close();   
  53.             pre.close();   
  54.             conn.close();   
  55.         } catch (SQLException e) {   
  56.             e.printStackTrace();   
  57.         }finally{   
  58.              pre = null;   
  59.              conn = null;   
  60.              rs = null;   
  61.         }   
  62.         return list;   
  63.     }   
  64.   
  65. }  
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class DaoTest {
Connection con = null;
public Connection getConnection() {
Connection conn = null;
String url = "jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=gbk";
String user = "root";
String password = "admin";
try {
if (conn == null) {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, user, password);
}
} catch (Exception e) {
System.out.println("连接失败");
return null;
} finally {
url = null;
user = null;
password = null;
}
return conn;
}
public ArrayList<Nodes> getNodeInfo() {
String sql = "select nodeId ,parentId ,hrefAddress ,nodeName from tree_table order by id ";
PreparedStatement pre = null;
Connection conn = null;
conn = getConnection();
ResultSet rs = null;
ArrayList<Nodes> list = new ArrayList<Nodes>();
try {
pre = conn.prepareStatement(sql);
rs =pre.executeQuery();
while (rs.next()){
Nodes node = new Nodes();
node.setHrefAddress(rs.getString("hrefAddress"));
node.setNodeId(rs.getString("nodeId"));
node.setParentId(rs.getString("parentId"));
node.setNodeName(rs.getString("nodeName"));
list.add(node);
}
rs.close();
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
pre = null;
conn = null;
rs = null;
}
return list;
}
}

 3. 节点。JAVABEAN.类.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值