📄 dbmenu.java
字号:
/*
* DbUnit.java
*
* Created on 2001年7月4日, 上午10:37
*/
package com.gs.db.dbimp;
import com.gs.db.*;
import com.gs.util.Cacheable;
import com.gs.util.CacheSizes;
import java.sql.*;
import java.util.Iterator;
import java.util.ArrayList;
import java.lang.*;
import java.util.*;
/**
*
* @author administrator
* @version
*/
public class DbMenu {
private static final String LOAD_Menu_BY_ID =
"SELECT * FROM gsMenues WHERE IDMenue=? order by IDMenue";
private static final String INSERT_SUB_Menu =
"INSERT INTO gsMenues(IDMenue, IDParent, Name, Type,Description,path) VALUES(?,?,?,?,?,?)";
private static final String UPDATE_Menu =
"UPDATE gsMenues SET IDParent=?,Name=?,Type=?,Description=?,path=? WHERE IDMenue=?";
private static final String SUB_Menus =
"SELECT IDMenue from gsMenues WHERE IDParent=? order by IDMenue";
private static final String SUB_Leaf_Menus =
"SELECT IDMenue from gsMenues WHERE IDParent=? and Type='1' order by IDMenue";
private static final String DELETE_Menu =
"DELETE FROM gsMenues WHERE IDMenue=?";
private static final String DELETE_GroupMenu =
"DELETE FROM gsGroupMenu WHERE IDMenue=?";
private static final String DELETE_UserMenu =
"DELETE FROM gsUserMenu WHERE IDMenue=?";
private int id = -1;
private int parentid = -1;
private String name = null;
private String s_Type = "";
private String s_Description = "";
private String s_path = "";
public DbMenu(int IDMenu) throws UnitNotFoundException {
this.id = IDMenu;
loadFromDb();
}
public DbMenu CreatSubMenu(String type, String s_name, String Description,
String path) throws UnitNotFoundException{
int subid = Integer.parseInt(SequenceAction.getId("menu"));
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_SUB_Menu);
pstmt.setInt(1, subid);
pstmt.setInt(2, id);
pstmt.setString(3, s_name);
pstmt.setString(4, type);
pstmt.setString(5, Description);
pstmt.setString(6, path);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
System.err.println("SQLException in DbMenu.java:" +
"createUnit():cannot insert sub unit data " + sqle);
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
DbMenu subMenu = new DbMenu(subid);
return subMenu;
}
public int getIDParent() {
return parentid;
}
public int getID() {
return id;
}
public String getName() {
return name;
}
public String getType() {
return this.s_Type;
}
public String getDescription() {
return this.s_Description;
}
public String getPath() {
return this.s_path;
}
public boolean isLeaf() {
boolean bLeaf = false;
if (s_Type.equals("1"))
return true;
return bLeaf;
}
public DbMenu getParentMenu() {
if (parentid == -1)return null; // this is a root unit
try {
return new DbMenu(parentid);
}
catch (UnitNotFoundException e) {
return null;
}
}
public ArrayList getSubMenus() {
//We don't know how many results will be returned, so store them
//in an ArrayList.
ArrayList tempMenus = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SUB_Menus);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
try {
tempMenus.add(new DbMenu(rs.getInt("IDMenue")));
}
catch (UnitNotFoundException e) {}
}
}
catch (SQLException sqle) {
System.err.println("Error in DbUnitIterator:constructor()-" + sqle);
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
return tempMenus;
}
public void setName(String s_name) {
name = s_name;
}
public void setType(String s_type) {
s_Type = s_type;
}
public void setDescription(String Description) {
s_Description = Description;
}
public void setPath(String path) {
s_path = path;
}
public void setIDParent(int IDParent) {
parentid = IDParent;
}
/**
* Load the group data from the database.
*/
private void loadFromDb() throws UnitNotFoundException {
String query;
query = LOAD_Menu_BY_ID;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(query);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (!rs.next()) {
throw new UnitNotFoundException();
}
id = rs.getInt("IDMenue");
name = rs.getString("Name");
String s_parentid = rs.getString("IDParent");
s_Type = rs.getString("Type");
s_Description = rs.getString("Description");
s_path = rs.getString("path");
if (s_parentid == null) {
parentid = -1;
}else{
parentid = Integer.parseInt(s_parentid);
}
}
catch (SQLException sqle) {
System.err.println("SQLException in DbMenu.java:" +
"loadFromDb():reading unit data " + sqle);
throw new UnitNotFoundException();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void updateToDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_Menu);
String s_parentid = null;
if (parentid > -1)
s_parentid = parentid + "";
pstmt.setString(1, s_parentid);
pstmt.setString(2, name);
pstmt.setString(3, s_Type);
pstmt.setString(4, s_Description);
pstmt.setString(5, s_path);
pstmt.setInt(6, id);
pstmt.executeUpdate();
}
catch (SQLException sqle) {
System.err.println("SQLException in DbGroup.java:saveToDb(): " + sqle);
sqle.printStackTrace();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public int getLevel() {
if (parentid == -1) {
return 0;
}
return getParentMenu().getLevel() + 1;
}
public static void deleteMenu(DbMenu menu){
ArrayList subMenu = menu.getSubMenus();
for(int i = 0;i < subMenu.size();i++)
DbMenu.deleteMenu((DbMenu)subMenu.get(i));
deleteGroupMenu(menu.getID());
deleteUserMenu(menu.getID());
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_Menu);
pstmt.setInt(1, menu.getID());
pstmt.execute();
pstmt.close();
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void deleteGroupMenu(int menuID){
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_GroupMenu);
pstmt.setInt(1, menuID);
pstmt.execute();
pstmt.close();
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void deleteUserMenu(int menuID){
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_UserMenu);
pstmt.setInt(1, menuID);
pstmt.execute();
pstmt.close();
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -