通过数据库建立树型菜单-beta1

本文使用已经写好的自制数据库框架,并通过dtree来创建树型菜单,包含添加和删除菜单选项

 

 图1:初始菜单界面:


 

 图2:添加一个菜单“我的菜单3”,父节点为“28”,即父节点为“我的菜单”:


 

 图3:菜单添加完毕的页面


 

 图4:删除“我的菜单2”


 

 图5:删除“我的菜单2”后的界面

 


 //ConnectionFactory.java package cn.edu.ccut.datebase; import java.sql.Connection; import java.sql.DriverManager; import java.util.ResourceBundle; /** * Created by IntelliJ IDEA. * User: MickeyChen * Date: 2010-7-5 * Time: 16:49:51 * To change this template use File | Settings | File Templates. */ /** * 类名:ConnectionFactory * 作用:产生数据库连接对象 * 属性: * 方法:Connection getConnection() * 作用:返回数据库连接对象 * 参数:无 * 返回:数据库连接对象 * Created by IntelliJ IDEA. * User: 挥着翅膀的鳖 * Date: 2010-2-14 * Time: 21:02:26 * * To change this template use File | Settings | File Templates. */ public abstract class ConnectionFactory { /** 数据库系统的属性文件名: * 用户可以自定义,前提是在dbsystem.properties的当前目录下必须存在此文件. * 推荐用数据库系统名称命名,例如: mysql,mssqlserver */ //private static final String propertiesFileName = "using_which_dbms"; /** 获取Connection对象 * * @return Connection对象 * @throws Exception SqlException异常 */ static synchronized public Connection getConnection() throws Exception { String dbSystem=null; Connection aConnection; // ResourceBundle db = ResourceBundle.getBundle("dbsystem"); //读取配置文件 //dbSystem=db.getString(propertiesFileName); dbSystem = "mickeyMySql"; ResourceBundle rb = ResourceBundle.getBundle(dbSystem); //读取配置文件 Class.forName(rb.getString("database.driver")).newInstance(); aConnection = DriverManager.getConnection(rb.getString("database.url"),rb.getString("database.username"),rb.getString("database.password")); //创建aConnection对象 aConnection.setAutoCommit(true); //设置不自动提交事务 return aConnection; //返回aConnection对象 } } 
 //DBOperationAdapter.java package cn.edu.ccut.datebase; import java.sql.ResultSet; import java.sql.SQLException; /** * Created by IntelliJ IDEA. * User: mickey-chen * Date: 2010-2-14 * Time: 21:14:30 * To change this template use File | Settings | File Templates. */ public class DBOperationAdapter extends ConnectionFactory { private static IOperationCore objIOperationCore = null; private static DBOperationAdapter m_instance = null; private DBOperationAdapter() { try { objIOperationCore = OperationCoreImpl.createFactory(); } catch (Exception e) { e.printStackTrace(); } } public static DBOperationAdapter getInstance() { if (m_instance == null) { m_instance = new DBOperationAdapter(); } return m_instance; } public ResultSet executeQuery(String queryString) throws SQLException { return objIOperationCore.executeQuery(queryString); } public int executeUpdate(String updateString) throws SQLException { return objIOperationCore.executeUpdate(updateString); } public int executeDelete(String deleteString) throws SQLException { return objIOperationCore.executeUpdate(deleteString); } public int executeInsert(String insertString) throws SQLException { return objIOperationCore.executeUpdate(insertString); } public int getRowCount(String queryString) throws SQLException { return objIOperationCore.getRowCount(queryString); } public int getColumnCount(String queryString) throws SQLException { return objIOperationCore.getColumnCount(queryString); } public String getColumnName(int columIndex, String queryString) throws SQLException { return objIOperationCore.getColumnName(columIndex, queryString); } public boolean doLogin(String uname, String pwdMd5,String userRole) throws SQLException{ return objIOperationCore.login(uname, pwdMd5,userRole); } public void dispose() throws SQLException { objIOperationCore.dispose(); } public ResultSet getMenu() throws SQLException{ return objIOperationCore.executeQuery("select * from tb_menu "); } public ResultSet getFather() throws SQLException{ return objIOperationCore.executeQuery("select * from tb_menu where menuPrentId=0"); } public void addMenu(String menuPrentId,String menuName,String menuUrl) throws SQLException{ System.out.println("INSERT INTO tb_menu (menuPrentId, menuName,menuUrl)" + "VALUES ('"+menuPrentId+"', '"+menuName+"','"+menuUrl+"')" ); int n=objIOperationCore.executeUpdate("INSERT INTO tb_menu (menuPrentId, menuName,menuUrl)" + "VALUES ('"+menuPrentId+"', '"+menuName+"','"+menuUrl+"')"); System.out.println(n); } public void delMenu(String menuName,String MenuParentId) throws SQLException{ System.out.println("DELETE FROM tb_menu WHERE menuName = '"+menuName+"', menuPrentId='"+MenuParentId+"'"); objIOperationCore.executeUpdate("DELETE FROM tb_menu WHERE menuName = '"+menuName+"'and menuPrentId='"+MenuParentId+"'"); } } 
 //IOperationCore.java package cn.edu.ccut.datebase; import java.sql.ResultSet; import java.sql.SQLException; /** * Created by IntelliJ IDEA. * User: MickeyChen * Date: 2010-7-5 * Time: 16:50:24 * To change this template use File | Settings | File Templates. */ /** * Created by IntelliJ IDEA. * User: 挥着翅膀的鳖 * 数据库常用操作封装 * Date: 2010-2-14 * Time: 21:15:03 * To change this template use File | Settings | File Templates. */ public interface IOperationCore { /** sql更新语句 * * @param queryString 查询语句 * @return 返回一个<code>ResultSet</code>结果集 * * @exception SQLException */ ResultSet executeQuery(String queryString) throws SQLException; /** * sql更新语句 * * @param updateString 数据库更新语句 * @return 更新数据库影响行数 * * @exception SQLException */ int executeUpdate(String updateString) throws SQLException; /** * 读取行个数 * * @param queryString 查询语句 * @return Transact-SQL 查询后的虚拟表的行数 * * @exception SQLException */ int getRowCount(String queryString) throws SQLException; /** * 读取列数个数 * * @param queryString 查询语句 * @return Transact-SQL 查询后的虚拟表的列数 * * @exception SQLException */ int getColumnCount(String queryString) throws SQLException; /** * 读取列名 * * @param columIndex 列索引 * @param queryString 提供ResultSet二维表的查询字符串 * @return ResultSet表中的指定的列名 * * @exception SQLException */ String getColumnName(int columIndex, String queryString) throws SQLException; /** 验证登陆并返回用户角色 * @param uName 用户名 * @param pwdMd5 MD5加密的密码 * @return account 的用户角色 * */ boolean login(String uName,String pwdMd5,String userRole) throws SQLException; /** * 释放系统连接资源 * 一旦关闭,数据库的操作将全部无效 * @exception SQLException 如果关闭失败将抛出<code>SQLException</code>*/ void dispose() throws SQLException; } 
 //OperationCoreImpl.java package cn.edu.ccut.datebase; import java.sql.*; /** * Created by IntelliJ IDEA. * User: 挥着翅膀的鳖 * Date: 2010-2-14 * Time: 21:19:41 * To change this template use File | Settings | File Templates. */ public class OperationCoreImpl implements IOperationCore { protected Connection aConnection = null; protected Statement aStatement = null; protected ResultSet aResultSet = null; protected ResultSetMetaData rsmd = null; protected static OperationCoreImpl m_instance = null; /** Singleton 即单例(态)模式,用来生成对象唯一实例的方法 * * @return OperationCoreImplements的一个实例 * @throws Exception */ public static OperationCoreImpl createFactory() throws Exception { if(m_instance==null) m_instance=new OperationCoreImpl(); return m_instance; } /** @exception Exception */ private OperationCoreImpl() throws Exception { init(); } /** * 负责初始化Connection连接 * @throws Exception */ private void init() throws Exception{ aConnection=ConnectionFactory.getConnection(); } public void dispose() throws SQLException { //To change body of implemented methods use File | Settings | File Templates. try{ if(aResultSet!=null){ aResultSet.close(); aResultSet=null; } if(aStatement!=null){ aStatement.close(); aStatement=null; } if(aConnection!=null) aConnection.close(); aConnection=null; } catch (SQLException e){ e.printStackTrace(); } } /** sql更新语句 * * @param queryString 查询语句 * @return 返回一个<code>ResultSet</code>结果集 * * @exception SQLException */ public ResultSet executeQuery(String queryString) throws SQLException { try { aStatement = aConnection.createStatement(); aResultSet = aStatement.executeQuery(queryString); } catch (SQLException e) { aResultSet = null; e.printStackTrace(); } return aResultSet; } /** * sql更新语句 * * @param updateString 数据库更新语句 * @return 更新数据库影响行数 * * @exception SQLException */ public int executeUpdate(String updateString) throws SQLException { int effectedRows = 0; try { aConnection.setAutoCommit(false); aStatement = aConnection.createStatement(); effectedRows = aStatement.executeUpdate(updateString); aConnection.commit(); } catch (SQLException e) { e.printStackTrace(); System.out.println(&quot;数据库操作失败&quot;); if (aConnection != null) { try { aConnection.rollback(); System.out.println(&quot;数据回滚成功&quot;); } catch (SQLException ex) { System.out.println(&quot;数据回滚失败&quot;); ex.printStackTrace(); } } } aConnection.setAutoCommit(true); return effectedRows; } /** * 读取列数个数 * * @param queryString 查询语句 * @return Transact-SQL 查询后的虚拟表的列数 * * @exception SQLException */ public int getColumnCount(String queryString) throws SQLException { int columnCount=0; try { aResultSet=executeQuery(queryString); ResultSetMetaData rsmd=aResultSet.getMetaData(); columnCount = rsmd.getColumnCount(); } catch (SQLException e) { e.printStackTrace(); } return columnCount; } /** * 读取列名 * * @param columIndex 列索引 * @param queryString 提供ResultSet二维表的查询字符串 * @return ResultSet表中的指定的列名 * * @exception SQLException */ public String getColumnName(int columIndex, String queryString) throws SQLException { String columnName=null; try { aResultSet=executeQuery(queryString); rsmd=aResultSet.getMetaData(); columnName=rsmd.getColumnName(columIndex + 1); } catch (SQLException e) { e.printStackTrace(); } return columnName; } public boolean login(String uName, String pwdMd5,String userRole) throws SQLException { String role = null; try { aResultSet = executeQuery(&quot;select * from tb_&quot;+userRole+&quot; where name=&#39;&quot; + uName + &quot;&#39; and password = &#39;&quot; + pwdMd5+&quot;&#39;&quot;); System.out.println(&quot;select * from &quot;+userRole+&quot; where name=&#39;&quot; + uName + &quot;&#39; and password = &#39;&quot; + pwdMd5+&quot;&#39;&quot;); } catch (SQLException e) { aResultSet = null; e.printStackTrace(); } return aResultSet.next(); } /** * 读取行个数 * * @param queryString 查询语句 * @return Transact-SQL 查询后的虚拟表的行数 * * @exception SQLException */ public int getRowCount(String queryString) throws SQLException { int rowCount=0; try { aResultSet=executeQuery(queryString); while(aResultSet.next()) rowCount=aResultSet.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return rowCount; } } 
 //getmenu.java package cn.edu.ccut.menu; import cn.edu.ccut.datebase.DBOperationAdapter; import java.sql.ResultSet; import java.sql.SQLException; /** * Created by IntelliJ IDEA. * User: 挥着翅膀的鳖 * Date: 2010-2-16 * Time: 2:12:10 * To change this template use File | Settings | File Templates. */ public class getMenu { public static String getUserMenu() throws SQLException { String menu =""; DBOperationAdapter dbtool = DBOperationAdapter.getInstance(); System.out.println("查找菜单"); try { ResultSet rs = dbtool.getMenu(); while (rs.next()) { menu+= "d.add("+rs.getString("menuID")+","+rs.getString("menuPrentId")+",'"+rs.getString("menuName")+"','"+rs.getString("menuUrl")+"',\"\",\"mainFrame\");"+"\n"; } } catch (SQLException e) { menu = null; e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return menu; } public static String getfather() throws SQLException{ String father=""; DBOperationAdapter dbtool=DBOperationAdapter.getInstance(); ResultSet rs= dbtool.getFather(); while(rs.next()){ father+="【菜单名称】:"+rs.getString("menuName")+" 【节点号】:"+rs.getString("menuID")+"||||"; } return father; } } 

剩下的代码就不一一举出了。。。

代码下载:http://www2.wangxinxin.com/down/javamenu.zip

1 Star2 Stars3 Stars4 Stars5 Stars (27 votes, average: 5.00 out of 5)
Loading ... Loading ...

标签:, , , ,

5 Responses


  1. 挥着翅膀的鳖 on 05 七 2010

    压缩包中包含数据库文件,导入就可以使用,menu.sql

  2. 宋 青云 on 05 七 2010

    很好!!!很强大!!!!

  3. 302509793 on 05 七 2010

    哦,帅呆了!!!

  4. hehai on 18 一 2011

    怎么没有办法下载了, 如果可以给我发一份吧,我想看看.邮件:[email protected]


Leave your comment