哪位大侠可以帮我写一段java代码!是关于根据数据库里的数据来形成一个动态树形图
3个回答
展开全部
这是代码,你可以自己调试一下。
数据结构如下:
CREATE TABLE dtree (
id int,
pid int,
name varchar(200),
url varchar(200),
title varchar(200),
target varchar(200),
icon varchar(200),
iconopen varchar(200),
opened bit);
为了实现获取数据库变量功能,需要建立一个DTree类,并编译生成CLASS文件,放入\WEB-INF\classes文件夹下。
DTree类代码如下:
package work3;
public class DTree {
private int id;
private int pid;
private String name;
private String url;
private String title;
private String target;
private String icon;
private String iconOpen;
private int opened;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getIconOpen() {
return iconOpen;
}
public void setIconOpen(String iconOpen) {
this.iconOpen = iconOpen;
}
public int getOpened() {
return opened;
}
public void setOpened(int opened) {
this.opened = opened;
}
}
work3.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id='settree' scope="application" class="work3.DTree" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
<title>dTree in MySQL</title>
</head>
<body>
<h2>
Example
</h2>
<div class="dtree">
<p>
<a href="javascript: d.openAll();">open all</a> |
<a href="javascript: d.closeAll();">close all</a>
</p>
<script type="text/javascript">
<!--
d = new dTree('d');
<%
//驱动程序名
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//数据库用户名
String userName = "sa";
//密码
String userPwd = "1";
//数据库名
String dbName = "master";
//表名
String tableName = "dtree";
//连接字符串
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;
//加载驱动
Class.forName(driverName).newInstance();
//连接数据库
java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);
//得到Statement实例
java.sql.Statement statement = conn.createStatement();
//查询数据
String sql = "select * from " + tableName;
//返回结果
java.sql.ResultSet rs = statement.executeQuery(sql);
//获取变量
while (rs.next()) {
settree.setId(rs.getInt(1));
settree.setPid(rs.getInt(2));
settree.setName(rs.getString(3));
settree.setUrl(rs.getString(4));
settree.setTitle(rs.getString(5));
settree.setTarget(rs.getString(6));
settree.setIcon(rs.getString(7));
settree.setIconOpen(rs.getString(8));
settree.setOpened(rs.getInt(9));
if(settree.getPid()==0)
settree.setOpened(1);
%>
d.add(<%=settree.getId()%>,<%=settree.getPid()%>,'<%=settree.getName()%>','<%=settree.getUrl()%>','<%=settree.getTitle()%>','<%=settree.getTarget()%>','','',<%=settree.getOpened()%>);
<%
}
%>
document.write(d);
//-->
</script>
</div>
</body>
</html>
数据结构如下:
CREATE TABLE dtree (
id int,
pid int,
name varchar(200),
url varchar(200),
title varchar(200),
target varchar(200),
icon varchar(200),
iconopen varchar(200),
opened bit);
为了实现获取数据库变量功能,需要建立一个DTree类,并编译生成CLASS文件,放入\WEB-INF\classes文件夹下。
DTree类代码如下:
package work3;
public class DTree {
private int id;
private int pid;
private String name;
private String url;
private String title;
private String target;
private String icon;
private String iconOpen;
private int opened;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getIconOpen() {
return iconOpen;
}
public void setIconOpen(String iconOpen) {
this.iconOpen = iconOpen;
}
public int getOpened() {
return opened;
}
public void setOpened(int opened) {
this.opened = opened;
}
}
work3.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id='settree' scope="application" class="work3.DTree" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
<title>dTree in MySQL</title>
</head>
<body>
<h2>
Example
</h2>
<div class="dtree">
<p>
<a href="javascript: d.openAll();">open all</a> |
<a href="javascript: d.closeAll();">close all</a>
</p>
<script type="text/javascript">
<!--
d = new dTree('d');
<%
//驱动程序名
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//数据库用户名
String userName = "sa";
//密码
String userPwd = "1";
//数据库名
String dbName = "master";
//表名
String tableName = "dtree";
//连接字符串
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;
//加载驱动
Class.forName(driverName).newInstance();
//连接数据库
java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);
//得到Statement实例
java.sql.Statement statement = conn.createStatement();
//查询数据
String sql = "select * from " + tableName;
//返回结果
java.sql.ResultSet rs = statement.executeQuery(sql);
//获取变量
while (rs.next()) {
settree.setId(rs.getInt(1));
settree.setPid(rs.getInt(2));
settree.setName(rs.getString(3));
settree.setUrl(rs.getString(4));
settree.setTitle(rs.getString(5));
settree.setTarget(rs.getString(6));
settree.setIcon(rs.getString(7));
settree.setIconOpen(rs.getString(8));
settree.setOpened(rs.getInt(9));
if(settree.getPid()==0)
settree.setOpened(1);
%>
d.add(<%=settree.getId()%>,<%=settree.getPid()%>,'<%=settree.getName()%>','<%=settree.getUrl()%>','<%=settree.getTitle()%>','<%=settree.getTarget()%>','','',<%=settree.getOpened()%>);
<%
}
%>
document.write(d);
//-->
</script>
</div>
</body>
</html>
2010-10-13
展开全部
树型结构的构造:
从数据库中返回的是有序的先序树,而XML是完整的树型结构文档,所以将树型数据构造成预定义的XML格式,只需从根节点开始,遍历一遍树,即可将树全部生成。相关JavaScript代码如下:
/*动态加载树的构造方法*/
dtmlXMLObject.prototype.constructTree=function(){
//采用动态加载时获取的xml数据,解析树型数据
var node=this.XMLLoader.getXMLTopNode("tree");
var parentId=node.getAttribute("id");
for(var i=0;i<node.childNodes.length;i ) { //逐个解析xml文件的leaf节点
if((node.childNodes[i].nodeType==1)&&(node.childNodes[i].tagName == "leaf")){
var name=node.childNodes[i].getAttribute("text");
…………
var temp=dhtmlObject.a0Find(parentId);//获取父节点对象
temp.XMLload=1;//已加载
//构造html输出节点
dhtmlObject.insertItem(parentId,cId,name,im0,im1,im2,chd);
dhtmlObject.addDragger = this;//设置可拖放的对象
};
从数据库中返回的是有序的先序树,而XML是完整的树型结构文档,所以将树型数据构造成预定义的XML格式,只需从根节点开始,遍历一遍树,即可将树全部生成。相关JavaScript代码如下:
/*动态加载树的构造方法*/
dtmlXMLObject.prototype.constructTree=function(){
//采用动态加载时获取的xml数据,解析树型数据
var node=this.XMLLoader.getXMLTopNode("tree");
var parentId=node.getAttribute("id");
for(var i=0;i<node.childNodes.length;i ) { //逐个解析xml文件的leaf节点
if((node.childNodes[i].nodeType==1)&&(node.childNodes[i].tagName == "leaf")){
var name=node.childNodes[i].getAttribute("text");
…………
var temp=dhtmlObject.a0Find(parentId);//获取父节点对象
temp.XMLload=1;//已加载
//构造html输出节点
dhtmlObject.insertItem(parentId,cId,name,im0,im1,im2,chd);
dhtmlObject.addDragger = this;//设置可拖放的对象
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
邮箱给我 我发给你代码
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询