?? menuchildbean.java
字號:
/**********************************************************
Version 1.0
Date:2003-10-21
Description:菜單維護(hù)表:列表查詢、單條信息查詢、增加記錄、修改記錄、刪除記錄
Other:
Variable List:
1.int strID //菜單唯一編號
2.int strParentID //父菜單編號
Function List:
1.public MenuBean()//構(gòu)造函數(shù)
2.public void setID(String o)//設(shè)置菜單唯一編號
2.public void setParentID(String o)//設(shè)置父菜單編號
4.public Vector getData()//取得菜單維護(hù)表所有記錄
5.public Hashtable getOneData()//取得一條記錄的信息
6.public boolean getOneData(String a,String b,String c)//判斷是否有滿足條件的記錄 true:有,false:無
7.public Vector getData(String a,String b,String c)//取得菜單維護(hù)表中滿足條件的所有記錄
public Vector changeData(String a,String b,String c)//修改菜單中的須修改的記錄
7.public int addMenu(Hashtable hash)//增加菜單維護(hù)表數(shù)據(jù)記錄
8.public int modMenu(Hashtable hash)//修改菜單維護(hù)表數(shù)據(jù)記錄
9.public int delMenu()//刪除菜單維護(hù)表數(shù)據(jù)記錄
public Hashtable getID3() //取得記錄的三級菜單
History:
***********************************************************/
package oa.bean;
import java.text.*;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.sql.*;
import oa.main.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class MenuChildBean extends ParentBean
{
int strID=-1;//菜單唯一編號
int strParentID=-1; //父菜單編號
public void setID(int o)//設(shè)置菜單唯一編號
{
strID = o;
}
public void setParentID(int o)//設(shè)置父菜單編號
{
strParentID = o;
}
public Hashtable getID3() //取得記錄的三級菜單
{
Hashtable ht = new Hashtable();
Hashtable hash = (Hashtable)getOneData();
String strthis = (String)hash.get("ISTHIS");
String strparent = (String)hash.get("IDPARENT");
if(strthis.equals("1"))
{
ht.put("ISID3","no");
return ht;
}
else
{
ht.put("ISID3","yes");
String sql = "select * from news.menu where id="+strparent;
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try{
//取得列數(shù)和列名
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
if(rs.next())
for(int i=1;i<=cols;i++)
{
String field = ds.toString(rsmd.getColumnName(i));
String value = ds.toString(rs.getString(i));
ht.put(field,value);
}
}catch(Exception e){System.out.println("運行時出錯:"+e);}
finally{
if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("關(guān)閉記錄集rs時出錯"+e);}
if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("關(guān)閉聲明時statement出錯"+e);}
}
return ht;
}
}
public boolean getData() //取得一條記錄的信息
{
Hashtable ht = new Hashtable();
String sql = "";
sql = "select * from news.menuchild where isthis=0 and idparent="+strParentID+"";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try{
if(rs.next())
return false;
}catch(Exception e){System.out.println("運行時出錯:"+e);}
finally{
if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("關(guān)閉記錄集rs時出錯"+e);}
if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("關(guān)閉聲明時statement出錯"+e);}
}
return true;
}
public Hashtable getOneData() //取得一條記錄的信息
{
Hashtable ht = new Hashtable();
String sql = "";
sql = "select * from news.menuchild where ID="+strID+"";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try{
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while(rs.next())
{
for(int i=1;i<=cols;i++)
{
String field = ds.toString(rsmd.getColumnName(i));
String value = ds.toString(rs.getString(i));
ht.put(field,value);
}
}
}catch(Exception e){System.out.println("運行時出錯:"+e);}
finally{
if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("關(guān)閉記錄集rs時出錯"+e);}
if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("關(guān)閉聲明時statement出錯"+e);}
}
return ht;
}
//生成樹并返回根節(jié)點
public Vector buildTree() {
Vector vt = new Vector();
String sql =
" Select * from news.MENUCHILD "
+ " Where IDPARENT="+strParentID
+ " and ISTHIS=0 order by ID ";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try{
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
// 結(jié)果集為空時返回
while(rs.next())
{
Hashtable hash = new Hashtable();
hash.clear();
for (int i = 1; i <= cols; i++)
{
String field = ds.toString(rsmd.getColumnName(i));
String value = ds.toString(rs.getString(i));
hash.put(field, value);
}
DefaultMutableTreeNode root = new DefaultMutableTreeNode(hash);
buildSubTree(root);
vt.add(root);
}
}catch(Exception e){System.out.println("運行時出錯:"+e);}
finally{
if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("關(guān)閉記錄集rs時出錯"+e);}
if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("關(guān)閉聲明時statement出錯"+e);}
}
return vt;
}
//迭代生成樹
private void buildSubTree(DefaultMutableTreeNode rootNode)
{
String sql = "";
String ParentId = (String) ((Hashtable) rootNode.getUserObject()).get("ID");
DefaultMutableTreeNode root = rootNode;
DefaultMutableTreeNode treeNode;
//選出rootNode的子節(jié)點
sql = " Select * from news.MENUCHILD "
+ " Where IDPARENT="+ParentId
+ " and ISTHIS=1 order by ID ";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try{
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next())
{
Hashtable hash = new Hashtable();
for (int i = 1; i <= cols; i++)
{
String field = ds.toString(rsmd.getColumnName(i));
String value = ds.toString(rs.getString(i));
hash.put(field, value);
}
treeNode = new DefaultMutableTreeNode(hash);
root.add(treeNode);
buildSubTree(treeNode);
}
}catch(Exception e){System.out.println("運行時出錯:"+e);}
finally{
if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("關(guān)閉記錄集rs時出錯"+e);}
if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("關(guān)閉聲明時statement出錯"+e);}
}
}
public int addMenu(Hashtable hash) //增加菜單維護(hù)表數(shù)據(jù)記錄
{
String sql = "";
int intMax = db.makeID("MENUCHILD","ID","","",true);
String strParent = ds.toString((String)hash.get("IDPARENT"));
String strBmname = ds.toGBK((String)hash.get("MENUCHILD"));
String strIsthis = ds.toString((String)hash.get("ISTHIS"));
Vector vect =new Vector();
vect.add("MENUCHILD");
vect.add(addVector("ID",String.valueOf(intMax),"NUM"));
vect.add(addVector("IDPARENT",strParent,"NUM"));
vect.add(addVector("MENUCHILD",strBmname,"CHAR"));
vect.add(addVector("ISTHIS",strIsthis,"NUM"));
return insertRecord(vect);
}
public int modMenu(Hashtable hash) //修改菜單維護(hù)表數(shù)據(jù)記錄
{
Vector vect = new Vector();
String strParent = ds.toString((String)hash.get("IDPARENT"));
String strBmname = ds.toGBK((String)hash.get("MENUCHILD"));
String strIsthis = ds.toString((String)hash.get("ISTHIS"));
vect.add("MENUCHILD");
vect.add(addVector("IDPARENT",strParent,"NUM"));
vect.add(addVector("MENUCHILD",strBmname,"CHAR"));
vect.add(addVector("ISTHIS",strIsthis,"NUM"));
vect.add("ID="+strID+"");
return updateRecord(vect);
}
public int delMenu() //刪除菜單維護(hù)表數(shù)據(jù)記錄
{
String sql = "";
sql = "select * from news.menuchild where idparent="+strID+" and isthis=1";
ResultSet rs = selectRecord(sql);
Statement stmt = null;
try{
if(rs.next())
{
return 2;
}
else
{
sql = "delete menuchild where id="+strID;
return deleteRecord(sql);
}
}catch(Exception e){System.out.println("運行時出錯:"+e);}
finally{
if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("關(guān)閉記錄集rs時出錯"+e);}
if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("關(guān)閉聲明時statement出錯"+e);}
}
return -1;
}
public static void main(String args[])
{
MenuChildBean mcb = new MenuChildBean();
mcb.setID(1);
System.out.println("begin\r\n\r\n");
System.out.println(mcb.getID3()+"\r\n\r\n");
System.out.println("\r\n\r\nend");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -