dtree+JQuery动态生成树.思路其实很简单... 首先把树的节点信息存储到数据库,然后在servlet或jsp中获取数据库表中的数据,把这些信息写成在xml文件中.然后界面jsp页面通过JQuery实现对改servlet的请求.并且回调方法中接受xml数据对象.并且遍历xml文件,取得xml文件中的节点的属性或文本数据.再循环的对dtree添加节点.。
1 . 在 MYSQL 中的test数据库中创建表.tree_table.
SQL语句.
- tree_table CREATE TABLE `tree_table` (
- `id` int(11) NOT NULL auto_increment,
- `nodeId` varchar(12) NOT NULL ,
- `parentId` varchar(12) NOT NULL ,
- `hrefAddress` varchar(85) ,
- `nodeName` varchar(20) ,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=gbk
2.连接数据库的类.主要是读取表中的数据.
- 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&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;
- }
- }
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&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.类.