⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mbeanpage.java

📁 具备多表组合查询组件功能。通过将表字段与表间关系映射到对象关系及属性
💻 JAVA
字号:
package cn.edu.buaa.ieguam.logmanage;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.edu.buaa.ieguam.logmanage.DBConnection;
import cn.edu.buaa.ieguam.logmanage.ResultFormat;


public class MBeanPage 
{
	private int curPage = 1;//当前是第几页
	private int maxPage = 0;//一共有多少页
	private int maxRowCount = 0;//一共有多少行
	private int rowsPerPage = 15;//每页有多少行
	private List data = null;//本页中要显示的记录
	private List querySqlList = null;//存放SQL查询语句
	private List formatList = null;//记录的查询结果的格式,元素为ResultFormat类型,元素的index值与querySqlList对应Sql语句的index值一致
	//private List viewQueueList = null;//要显示的属性队列,元素的index值与querySqlList对应Sql语句的index值一致;元素为Map类型,key为format中colList元素对应的index值,value为ResultFormat.ColBean 类型
	private List viewQueueList = null;//要显示的属性队列,元素的index值与querySqlList对应Sql语句的index值一致;元素也为List类型,index为用户设置的showQueue属性显示顺序,值为ResultFormat.ColBean 类型
	private DBConnection dbConnection = null;//数据库连接类
	
	public MBeanPage()
	{
		this.data = new ArrayList();
		this.querySqlList = new ArrayList();
		this.formatList = new ArrayList();
		this.viewQueueList = new ArrayList();
	}
	/**
	 * Getting and Setting methods
	 */
	public int getCurPage()
	{
		return this.curPage;
	}
	public void setCurPate(int curPage)
	{
		this.curPage = curPage;
	}
	
	public int getMaxPage()
	{
		return this.maxPage;
	}
	public void setMaxPage(int maxPage)
	{
		this.maxPage = maxPage;
	}
	
	public int getMaxRowCount()
	{
		return this.maxRowCount;
	}
	public void setMaxRowCount(int maxRowCount)
	{
		this.maxRowCount = maxRowCount;
	}

	public int getRowsPerPage()
	{
		return this.rowsPerPage;
	}
	public void setRowsPerPage(int rowsPerPage)
	{
		this.rowsPerPage = rowsPerPage;
	}
	
	public List getData()
	{
		return this.data;
	}
	public void setData(List data)
	{
		this.data = data;
	}
	
	public List getQuerySqlList()
	{
		return this.querySqlList;
	}
	public void setQuerySqlList(List querySqlList)
	{
		this.querySqlList = querySqlList;
	}
	
	public List getFormatList()
	{
		return this.formatList;
	}
	public void setFormat(List formatList)
	{
		this.formatList = formatList;
	}
	
	public List getViewQueueList()
	{
		return this.viewQueueList;
	}
	public void setViewQueueList(List viewQueueList)
	{
		this.viewQueueList = viewQueueList;
	}
	
	public DBConnection getDBConnection()
	{
		return this.dbConnection;
	}
	public void setDBConnection(DBConnection dbCon)
	{
		this.dbConnection = dbCon;
	}
	
	/**
	 * 初始化MBeanPage的相关参数
	 * @throws Exception 
	 *
	 */
	public void initPage() throws Exception
	{
		this.maxRowCount = this.getQueryCount(this.querySqlList);
		this.countMaxPage();
	}
	
	/**
	 * 重置mBeanPage的信息。用于完成一次查询操作后。
	 *
	 */
	public void resetPage()
	{
		this.getData().clear();
		this.getFormatList().clear();
		this.getQuerySqlList().clear();
		this.getViewQueueList().clear();
	}
	
	/**
	 * 根据总行数计算总页数
	 *
	 */
	public void countMaxPage()
	{
		if(this.maxRowCount%this.rowsPerPage == 0)
		{
			this.maxPage = this.maxRowCount/this.rowsPerPage;
		}
		else
		{
			this.maxPage = this.maxRowCount/this.rowsPerPage + 1;
		}
	}
	
	/**
	 * 返回要查询的记录数
	 * @param queryList
	 * @param dbConnec
	 * @return 若查询成功返回结果数
	 * @throws Exception
	 */
	public int getQueryCount(List queryList) throws Exception
	{
		if (this.dbConnection == null)
		{
			return 0;
		}
		
		int count = 0;
		StringBuffer querySb = null;
		String query = null;
		ResultSet resultSet = null;
		
		this.dbConnection.connect();
		Iterator iter = queryList.iterator();
		while(iter.hasNext())
		{
			querySb = (StringBuffer) iter.next();
			
			//拼接求得查询结果数量的语句
			query = "Select count(*)  " + querySb.substring(querySb.lastIndexOf("From"));
			//query = "Select count(*)  " + querySb.substring(querySb.lastIndexOf("From"),querySb.indexOf(";"));
			this.dbConnection.executeQuery(query);		
			resultSet = this.dbConnection.getResultSet();
			
			while(resultSet.next())
			{
				count += resultSet.getInt(1);
			}
		
		}
		/******************************/
		System.out.println("count is : "+count);
		/*******************************/
		
		this.dbConnection.closeConnec();
		return count;
	}
	
	
	/**
	 * 根据查询语句集合,生成当前页的查询结果
	 * @throws Exception 
	 */
	public MBeanPage queryPageData() throws Exception
	{
		this.data = new ArrayList();
		StringBuffer querySb = null;
		String query = null;
		ResultSet rs = null;
		int count = 0;//设置记录索引号
		
		ResultFormat format = null;
		ResultFormat.ColBean colBean = null;
		int dataNum = 0;
		int upperLimit = this.curPage*this.rowsPerPage;//应读取记录数上限
		int lowerLimit = (this.curPage-1)*this.rowsPerPage;//应读取记录数下限
		
       
	    this.dbConnection.connect();	
		
		Iterator iterSql = this.querySqlList.iterator();
		Iterator iterFormat = this.formatList.iterator();
		/**
		 * 取满一页数据后,就不要再继续执行查询!!!
		 */
		while(iterSql.hasNext())
		{
			format = (ResultFormat) iterFormat.next();
			querySb = (StringBuffer) iterSql.next();
			//query = querySb.substring(0, querySb.indexOf(";"));
			query = querySb.toString();
			/***************************************/
			System.out.println("Sql exceute :"+query);
			/****************************************/
			this.dbConnection.executeQuery(query);	
			
			rs = this.dbConnection.getResultSet();
			
			if(rs != null)
			{		      				
				while(rs.next())
				{			
					
					count++;
					if(count > upperLimit)
					{
						this.dbConnection.closeConnec();
						return this;
					}
								
					if(count > lowerLimit)
					{
						Object[] obj = new Object[format.getSize()+1];
					
						for(dataNum=0;dataNum < format.getSize();dataNum++)
						{
							//colBean = format.getCol(dataNum);	  
							//obj[dataNum] = rs.getObject(colBean.getKey());
							obj[dataNum] = rs.getObject(dataNum+1);
							
						/**
							if(obj[dataNum] != null)
							{
								System.out.println(obj[dataNum].toString());
							}
							else
							{
								System.out.println("");
							}
						*/
							
						}
						obj[dataNum] = new Integer(this.formatList.indexOf(format));//将该纪录对应的formatList项序号,存入数组最后一相										
						this.data.add(obj);//将obj加入到查询结果集
					}
								
				}
			}
				
		}
		
		this.dbConnection.closeConnec();
		return this;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -