熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

不用迭代算法而快速實現的jsp樹結構

2022-06-13   來源: JSP教程 

  在web頁面上實現樹狀結構有點麻煩

  在最近的一個MIS系統的開發中我們項目組大量用到了樹結構:比如人員的選擇單位的選擇等待

  這個MIS系統所用的數據庫是oracle i  oracle i 的sql支持迭代查詢我們的樹是由牛人彭越寫的不過

  也參照了網絡上比較著名的xtree(可以到此下載他的樹算法支持無限級的樹結構不過性能好像

  很慢我持保留態度

  他用到的關鍵技術就是這句話:

  String sql = "select dwxhdwbhdwmcdwfxhlevel cc from xt_dw connect by  prior dwxh = dwfxh start with dwfxh = ";

  可是許多數據庫不支持迭代查詢並且迭代查詢速度真是不能忍受有什麼更好的辦法呢下面說說我的解決方案

  一:需求的提出

  :客戶需要一個關於部門人員的樹結構數據庫為mysql

  :java實現

  二:建表:

  :

  用戶信息表:

  各字段為:用戶序號用戶編號用戶名稱單位序號密碼用戶登陸號

  create table XT_YH

  (

  YHXH  INT() NOT NULL auto_increment PRIMARY KEY

  YHBH  VARCHAR()

  YHMC  VARCHAR()

  DWXH  INT()

  PWD   VARCHAR()

  YHDLH VARCHAR()

  )

  插入三條測試數據:

  insert into xt_yh(yhbhyhmcdwxhpwdyhdlh) values(licl李春雷passwordlicl)

  insert into xt_yh(yhbhyhmcdwxhpwdyhdlh) values(fengx馮欣passwordfengx)

  insert into xt_yh(yhbhyhmcdwxhpwdyhdlh) values(wangqx王慶香passwordwangqx)

  :

  單位部門表

  各字段為:單位序號單位編號單位名稱單位父序號

  create table XT_DW

  (

  DWXH  int() NOT NULL auto_increment PRIMARY KEY

  DWBH  VARCHAR()

  DWMC  VARCHAR()

  DWFXH int()

  )

  插入條測試數據

  insert into xt_dw(dwbhdwmcdwfxh) values(武漢科技局);

  insert into xt_dw(dwbhdwmcdwfxh) values(人事處);

  insert into xt_dw(dwbhdwmcdwfxh) values(後勤處);

  insert into xt_dw(dwbhdwmcdwfxh) values(人事處son);

  insert into xt_dw(dwbhdwmcdwfxh) values(人事處son);

  insert into xt_dw(dwbhdwmcdwfxh) values(後勤處son);

  注意:

  為了實現快速的樹結構實現我需要充分利用單位編號DWBHDWBH才有位編碼其中第一第二位表示一級單位第三第四位表示二級單位

  第五六位表示三級單位那麼位編碼就可以實現五級單位的樹結構

  比如:測試數據的樹結構如下:

    武漢科技局:

    人事處

    人事處son

    人事處son

    後勤處

  後勤處son

  其實XT_DW表中的父序號是多余的不過如果你要用迭代算法來實現就是必須的

  才有位編碼我只需要一句簡單快速的sql語句就可以實現樹結構:

  String sql = "select dwxhdwbhdwmcdwfxh from xt_dw order by dwbh"

  這句sql在幾乎所有的數據庫平台都能執行速度也快

  下面貼出采用xtree位編碼而不是迭代算法實現的樹:

  /*******Constantsjava**********/

  package comlclcommon;

  public class Constants {

  public static final String DBDRIVER = "commysqljdbcDriver";    //MYSQL驅動

  public static final String DBUrl="jdbc:mysql://localhost/beauoa"; //數據庫url

  public static final String USERNAME="root";                       //數據庫用戶名

  public static final String PASSWORD="root";     //數據庫密碼

  /**********DbAccessjava****************/

  package comlclcommon;

  import javasql*;

  import javalang*;

  /**

  * @author 李春雷

  *

  * TODO 要更改此生成的類型注釋的模板請轉至

  * 數據庫訪問類

  */

  public class DbAccess

  { 

  String strDBDriver = ConstantsDBDRIVER;

  String strDBUrl = ConstantsDBUrl;

  String username = ConstantsUSERNAME;

  String password = ConstantsPASSWORD;

  private Connection conn = null;

  private Statement stmt = null;

  ResultSet rs=null;

  //注冊數據庫驅動程序

  public DbAccess()

  { 

  try

  { 

  ClassforName(strDBDriver);

  }

  //異常處理

  catch( javalangClassNotFoundException e)

  {

  Systemerrprintln("DbAccess():"+egetMessage());

  }

  }

  //建立數據庫連接及定義數據查詢

  public ResultSet executeQuery(String sql)

  {

  rs=null;

  try

  {

  conn=DriverManagergetConnection(strDBUrlusernamepassword);

  stmt=conncreateStatement();

  rs=stmtexecuteQuery(sql);

  }

  catch(SQLException ex)

  {

  Systemerrprintln("apexecuteQuery:"+exgetMessage());

  }

  return rs;

  }

  //定義數據操庫作

  public void executeUpdate(String sql)

  {

  stmt=null;

  rs=null;

  try

  {

  conn=DriverManagergetConnection(strDBUrlusernamepassword);

  stmt=conncreateStatement();

  stmtexecuteQuery(sql);

  stmtclose();

  connclose();

  }

  catch(SQLException ex)

  {

  Systemerrprintln("apexecuteQuery:"+exgetMessage());

  }

  }

  //關閉數據庫

  public void closeStmt()

  {

  try

  {

  stmtclose();

  }

  catch(SQLException e)

  {

  eprintStackTrace();

  }

  }

  public void closeConn()

  {

  try

  {

  connclose();

  }

  catch(SQLException e)

  {

  eprintStackTrace();

  }

  }

  public static void main(String[] args){

  Systemoutprintln("helloits test");

  DbAccess dbaccess = new DbAccess();

  String sql = "select * from xt_yh";

  ResultSet rs = dbaccessexecuteQuery(sql);

  try

  {

  while(rsnext()){

  Systemoutprint(rsgetString()+rsgetString()+rsgetString()+rsgetString()+rsgetString()+rsgetString());

  Systemoutprintln();

  }

  dbaccesscloseStmt();

  dbaccesscloseConn();

  }

  catch (SQLException e)

  {

  // TODO 自動生成 catch 塊

  eprintStackTrace();

  }

  }

  }

  /*********DepEmplConfigjsp************/

  <%@ page contentType="text/html; charset=gb" language="java" import="javasql*comlclcommon*" errorPage="" %>

  <!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN" "

  <html>

  <head>

  <meta httpequiv="ContentType" content="text/html; charset=gb">

  <title>無標題文檔</title>

  <HEAD>

  <script type="text/javascript" src="/resources/xDataTreejs"></script>

  <link type="text/css" rel="stylesheet" href="/resources/xtreecss" />

  <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"> 

  webFXTreeConfigrootIcon  = "/resources/images/xp/folderpng";

  webFXTreeConfigopenRootIcon = "/resources/images/xp/openfolderpng";

  webFXTreeConfigfolderIcon  = "/resources/images/xp/folderpng";

  webFXTreeConfigopenFolderIcon = "/resources/images/xp/openfolderpng";

  webFXTreeConfigfileIcon  = "/resources/images/xp/filepng";

  webFXTreeConfiglMinusIcon  = "/resources/images/xp/Lminuspng";

  webFXTreeConfiglPlusIcon  = "/resources/images/xp/Lpluspng";

  webFXTreeConfigtMinusIcon  = "/resources/images/xp/Tminuspng";

  webFXTreeConfigtPlusIcon  = "/resources/images/xp/Tpluspng";

  webFXTreeConfigiIcon   = "/resources/images/xp/Ipng";

  webFXTreeConfiglIcon   = "/resources/images/xp/Lpng";

  webFXTreeConfigtIcon   = "/resources/images/xp/Tpng";

  webFXTreeConfigblankIcon       = "/resources/images/blankpng";

  var tree = new WebFXTree("單位人員基本情況""R");

  var child;

  var nodeToAddPerson;

  function addDeptTreeNode(preNodeLevelcurNodeLeveldispLabelsKeysTag) {

  if(curNodeLevel==) {

  child = treeadd(new WebFXTreeItem(dispLabelsKeysTag));

  }

  else {

  if(curNodeLevel==preNodeLevel) {

  if(childparentNode)

  child = childparentNodeadd(new WebFXTreeItem(dispLabelsKeysTag));

  }

  if(curNodeLevel>preNodeLevel) {

  child = childadd(new WebFXTreeItem(dispLabelsKeysTag));

  }

  if(curNodeLevel<preNodeLevel) {

  for(i=;i<preNodeLevelcurNodeLevel+;i++)

  child = childparentNode;

  child = childadd(new WebFXTreeItem(dispLabelsKeysTag));

  }

  }

  return child;

  }

  function treeClick() {

  if(treegetSelected()) {

  if(treegetSelected()childNodeslength==&&treegetSelected()key!="R")

  cmdDeletedisabled = false;

  else

  cmdDeletedisabled = true;

  if(treegetSelected()keysubstr()=="RZ") {

  cmdAddDeptdisabled = true;

  cmdAddPeopledisabled = true;

  var strYhxh;

  strYhxh = treegetSelected()keysubstr();

  //windowopen("/userAdm/editYhdo?yhxh="+strYhxh"main");

  }

  else if(treegetSelected()keysubstr()=="RB") {

  cmdAddDeptdisabled = false;

  cmdAddPeopledisabled = false;

  var strDwxh;

  strDwxh = treegetSelected()keysubstr();

  //windowopen("/userAdm/editBmdo?dwxh="+strDwxh"main");

  }

  else {

  cmdAddDeptdisabled = false;

  cmdAddPeopledisabled = true;

  //windowopen("yhrootjsp""main");

  }

  }

  }

  function addPeople() {

  var strDwxh;

  if(treegetSelected()) {

  if (treegetSelected()keysubstr()=="RB") {

  strDwxh = treegetSelected()keysubstr();

  //windowopen("/userAdm/addYhdo?dwxh="+strDwxh"main");

  alert("addPeople");

  }

  }

  }

  function addDept() {

  var strDwxh;

  if(treegetSelected()) {

  if (treegetSelected()keysubstr()=="RB") {

  strDwfxh = treegetSelected()keysubstr();

  //windowopen("/userAdm/addBmdo?dwfxh="+strDwfxh"main");

  alert("addDept");

  }

  else if(treegetSelected()key=="R") {

  //windowopen("/userAdm/addBmdo?dwfxh=""main");

  alert("addDept");

  }

  }

  }

  function deleSelected() {

  if(!confirm("確認刪除該節點嗎?"))

  return;

  if(treegetSelected()) {

  if(treegetSelected()keysubstr()=="RB") {

  var strDwxh;

  strDwxh = treegetSelected()keysubstr();

  //windowopen("/userAdm/delBmdo?dwxh="+strDwxh"main");

  alert("deleSelected");

  }

  else if(treegetSelected()keysubstr()==RZ) {

  var strYhxhstrYhbh;

  strYhxh = treegetSelected()keysubstr();

  strYhbh = treegetSelected()tag;

  //windowopen("/userAdm/delYhdo?yhxh="+strYhxh+"&yhbh="+strYhbh"main");

  alert("deleSelected");

  }

  }

  }

  function removeNode() {

  if(treegetSelected()) {

  var node = treegetSelected();

  noderemove();

  }

  }

  function addPeopleNode(strParentKeystrKeystrTextstrTag) {

  if(treegetSelected()) {

  var node = treegetSelected();

  var childNode;

  //nodeexpand();

  childNode = nodeadd(new WebFXTreeItem(strTextstrKeystrTag"""""/resources/images/peoplepng"));

  nodeexpand(); //why I do so? I dont want to tell youhah!

  childNodefocus();

  treeClick();

  }

  }

  function addDeptNode(strParentKeystrKeystrTextstrTag) {

  if(treegetSelected()) {

  var node = treegetSelected();

  var childNode;

  childNode = nodeadd(new WebFXTreeItem(strTextstrKeystrTag));

  nodeexpand();

  childNodefocus();

  treeClick();

  }

  }

  function updateDeptNode(strTagstrText) {

  if(treegetSelected()) {

  var node = treegetSelected();

  nodetext = strText;

  nodetag  = strTag;

  nodefocus();

  }

  }

  function updatePeopleNode(strTagstrText) {

  if(treegetSelected()) {

  var node = treegetSelected();

  nodetext = strText;

  nodetag  = strTag;

  nodefocus();

  }

  }

  </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 dwxhdwbhdwmcdwfxh from xt_dw order by dwbh";

  ResultSet rs = dbaccessexecuteQuery(sql);

  try

  {

  while(rsnext())

  {

  dwxh = rsgetInt();

  dwbh = rsgetString();

  dwmc = rsgetString();

  dwfxh = rsgetInt();

  //通過單位編號計算level

  String last = dwbhsubstring();

  int i = ;

  while(lastequals("") && i>){

  i;

  last = dwbhsubstring(ii+);

  }

  if(i== || i==) level =;

  if(i== || i==) level =;

  if(i== || i==) level =;

  if(i== || i==) level =;

  if(i== || i==) level =;

  //

  %>

  <script type="text/javascript"> 

  nodeToAddPerson = addDeptTreeNode(<%=preLevel%><%=level%>"<%=dwmc%>""RB<%=dwxh%>""<%=dwbh%>");

  </script>  

  <%

  preLevel = level;

  String subsql = "select yhxhyhmcyhbh from xt_yh where dwxh = "+IntegertoString(dwxh);

  ResultSet subRs = dbaccessexecuteQuery(subsql);

  while(subRsnext()) {

  yhxh = subRsgetInt();

  yhmc = subRsgetString();

  yhbh = subRsgetString();

  %>

  <script type="text/javascript"> 

  nodeToAddPersonadd(new WebFXTreeItem("<%=yhmc%>""RZ<%=yhxh%>""<%=yhbh%>""""""/resources/images/peoplepng"));

  </script>

  <%

  }

  }

  dbaccesscloseStmt();

  dbaccesscloseConn();

  }

  catch(Exception e)

  {

  }

  %>

  <base target="_self">

  <META HTTPEQUIV="PRAGMA" CONTENT="NOCACHE">

  </head>

  <body>

  <table border="" width="%" cellspacing="" cellpadding="">

  <tr>

  <td width="" colspan="">

  <font face="宋體" size="">    

  </font>

  </td>

  </tr>

  <tr>

  <th width="%" align="center" nowrap>

  <p align="center">

  <INPUT id=cmdAddDept name="AddDept" type=button value="增加部門" onclick="addDept()" style="FONTFAMILY: 楷體_GB; FONTSIZE: pt; FONTWEIGHT: bold; HEIGHT: px; WIDTH: px" >

  </p>

  </th>

  <th width="%" align="center" nowrap>

  <p align="center">

  <INPUT id=cmdAddPeople name="AddPeople" type=button value="增加用戶" onclick="addPeople()" style="FONTFAMILY: 楷體_GB; FONTSIZE: pt; FONTWEIGHT: bold; HEIGHT: px; WIDTH: px" >

  </p>

  </th>

  <th width="%" align="center" nowrap>

  <p align="center">

  <INPUT id=cmdDelete name="Delete" type=button value=" 刪除 " onclick="deleSelected()" style="FONTFAMILY: 楷體_GB; FONTSIZE: pt; FONTWEIGHT: bold; HEIGHT: px; WIDTH: px" disabled>

  </p>

  </th>

  </tr>

  <tr>

  <td width="" height=""  colspan="">&nbsp;

  </td>

  </tr>

  </table>

  </body>

  <div onclick="treeClick()">

  <script type="text/javascript"> 

  documentwrite(tree);

  </script>

  </div>

  </HTML>

  //其中jsp頁面上的幾個javascript函數為同事牛人彭越所寫我沒改動在此說明


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20035.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.