在web頁面上實現樹狀結構
在最近的一個MIS系統的開發中
這個MIS系統所用的數據庫是oracle
也參照了網絡上比較著名的xtree(可以到此下載
很慢
他用到的關鍵技術就是這句話:
String sql = "select dwxh
可是許多數據庫不支持迭代查詢
一:需求的提出
二:建表:
用戶信息表:
各字段為:用戶序號
create table XT_YH
(
YHXH INT(
YHBH VARCHAR(
YHMC VARCHAR(
DWXH INT(
PWD VARCHAR(
YHDLH VARCHAR(
)
單位部門表
各字段為:單位序號
create table XT_DW
(
DWXH int(
DWBH VARCHAR(
DWMC VARCHAR(
DWFXH int(
)
注意:
為了實現快速的樹結構實現
第五六位表示三級單位
比如:測試數據的樹結構如下:
其實XT_DW表中的父序號是多余的
才有
String sql = "select dwxh
這句sql在幾乎所有的數據庫平台都能執行
下面貼出采用xtree
/*******Constants
package com
public class Constants {
public static final String DBDRIVER = "com
public static final String DBUrl="jdbc:mysql://localhost/beauoa"; //數據庫url
public static final String USERNAME="root"; //數據庫用戶名
public static final String PASSWORD="root"; //數據庫密碼
/**********DbAccess
package com
import java
import java
/**
* @author 李春雷
*
* TODO 要更改此生成的類型注釋的模板
* 數據庫訪問類
*/
public class DbAccess
{
String strDBDriver = Constants
String strDBUrl = Constants
String username = Constants
String password = Constants
private Connection conn = null;
private Statement stmt = null;
ResultSet rs=null;
//注冊數據庫驅動程序
public DbAccess()
{
try
{
Class
}
//異常處理
catch( java
{
System
}
}
//建立數據庫連接及定義數據查詢
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
conn=DriverManager
stmt=conn
rs=stmt
}
catch(SQLException ex)
{
System
}
return rs;
}
//定義數據操庫作
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
conn=DriverManager
stmt=conn
stmt
stmt
conn
}
catch(SQLException ex)
{
System
}
}
//關閉數據庫
public void closeStmt()
{
try
{
stmt
}
catch(SQLException e)
{
e
}
}
public void closeConn()
{
try
{
conn
}
catch(SQLException e)
{
e
}
}
public static void main(String[] args){
System
DbAccess dbaccess = new DbAccess();
String sql = "select * from xt_yh";
ResultSet rs = dbaccess
try
{
while(rs
System
System
}
dbaccess
dbaccess
}
catch (SQLException e)
{
// TODO 自動生成 catch 塊
e
}
}
}
/*********DepEmplConfig
<%@ page contentType="text/html; charset=gb
<!DOCTYPE HTML PUBLIC "
<html>
<head>
<meta http
<title>無標題文檔</title>
<HEAD>
<script type="text/javascript" src="
<link type="text/css" rel="stylesheet" href="
<style type="text/css">
body {
background: white;
color: black;
}
</style>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script type="text/javascript">
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
webFXTreeConfig
var tree = new WebFXTree("單位人員基本情況"
var child;
var nodeToAddPerson;
function addDeptTreeNode(preNodeLevel
if(curNodeLevel==
child = tree
}
else {
if(curNodeLevel==preNodeLevel) {
if(child
child = child
}
if(curNodeLevel>preNodeLevel) {
child = child
}
if(curNodeLevel<preNodeLevel) {
for(i=
child = child
child = child
}
}
return child;
}
function treeClick() {
if(tree
if(tree
cmdDelete
else
cmdDelete
if(tree
cmdAddDept
cmdAddPeople
var strYhxh;
strYhxh = tree
//window
}
else if(tree
cmdAddDept
cmdAddPeople
var strDwxh;
strDwxh = tree
//window
}
else {
cmdAddDept
cmdAddPeople
//window
}
}
}
function addPeople() {
var strDwxh;
if(tree
if (tree
strDwxh = tree
//window
alert("addPeople");
}
}
}
function addDept() {
var strDwxh;
if(tree
if (tree
strDwfxh = tree
//window
alert("addDept");
}
else if(tree
//window
alert("addDept");
}
}
}
function deleSelected() {
if(!confirm("確認刪除該節點嗎?"))
return;
if(tree
if(tree
var strDwxh;
strDwxh = tree
//window
alert("deleSelected");
}
else if(tree
var strYhxh
strYhxh = tree
strYhbh = tree
//window
alert("deleSelected");
}
}
}
function removeNode() {
if(tree
var node = tree
node
}
}
function addPeopleNode(strParentKey
if(tree
var node = tree
var childNode;
//node
childNode = node
node
childNode
treeClick();
}
}
function addDeptNode(strParentKey
if(tree
var node = tree
var childNode;
childNode = node
node
childNode
treeClick();
}
}
function updateDeptNode(strTag
if(tree
var node = tree
node
node
node
}
}
function updatePeopleNode(strTag
if(tree
var node = tree
node
node
node
}
}
</script>
<%
int dwxh;
int dwfxh;
int yhxh;
String dwbh = null;
String dwmc = null;
String yhmc = null;
String yhbh = null;
int preLevel =
int level =
DbAccess dbaccess = new DbAccess();
String sql = "select dwxh
ResultSet rs = dbaccess
try
{
while(rs
{
dwxh = rs
dwbh = rs
dwmc = rs
dwfxh = rs
//通過單位編號計算level
String last = dwbh
int i =
while(last
i
last = dwbh
}
if(i==
if(i==
if(i==
if(i==
if(i==
//
%>
<script type="text/javascript">
nodeToAddPerson = addDeptTreeNode(<%=preLevel%>
</script>
<%
preLevel = level;
String subsql = "select yhxh
ResultSet subRs = dbaccess
while(subRs
yhxh = subRs
yhmc = subRs
yhbh = subRs
%>
<script type="text/javascript">
nodeToAddPerson
</script>
<%
}
}
dbaccess
dbaccess
}
catch(Exception e)
{
}
%>
<base target="_self">
<META HTTP
</head>
<body>
<table border="
<tr>
<td width="
<font face="宋體" size="
</font>
</td>
</tr>
<tr>
<th width="
<p align="center">
<INPUT id=cmdAddDept name="AddDept" type=button value="增加部門" onclick="addDept()" style="FONT
</p>
</th>
<th width="
<p align="center">
<INPUT id=cmdAddPeople name="AddPeople" type=button value="增加用戶" onclick="addPeople()" style="FONT
</p>
</th>
<th width="
<p align="center">
<INPUT id=cmdDelete name="Delete" type=button value=" 刪除 " onclick="deleSelected()" style="FONT
</p>
</th>
</tr>
<tr>
<td width="
</td>
</tr>
</table>
</body>
<div onclick="treeClick()">
<script type="text/javascript">
document
</script>
</div>
</HTML>
//其中jsp頁面上的幾個javascript函數為同事牛人彭越所寫
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20035.html