登录+分页实现

servlet类

package com.tdb.sevice.controller;


import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tdb.sevice.model.ContactBean;
import com.tdb.sevice.model.PageBean;

public class PageServlet extends javax.servlet.http.HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws javax.servlet.ServletException, java.io.IOException {
		response.setContentType("text/html");
		try {
			PageBean pagebean = new PageBean();
			ContactBean contact = new ContactBean();
			System.out.println("servlet测试点1");
			int pagecount = pagebean.countPage();//获取总共的页数
			System.out.println("servlet测试点2"+pagecount);
			String topage = request.getParameter("topage");//获取当前的页码
			System.out.println("servlet测试点3"+topage);
			
			
			if(topage != null && !topage.trim().equals("")){
				if (Integer.parseInt(topage) > pagecount) {  //判断当前的页码是否越界,如果是的话就进行处理
					topage =String.valueOf(pagecount);
					System.out.println("servlet测试点4"+topage);
				} else if (Integer.parseInt(topage) <= 0) {
					topage = "1";
					System.out.println("servlet测试点5"+topage);
				}
			}else {
				topage = "1";
				System.out.println("servlet测试点6"+topage);
			}
			
			
		
			
			ArrayList devicebase = contact.getData(topage, pagecount);
			request.getSession().setAttribute("pagecount", pagecount);
			request.getSession().setAttribute("topage", topage);//将当前的页码返回给页面,这样进行计算
			request.getSession().setAttribute("devicebase", devicebase);
			response.sendRedirect("main.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)

	throws javax.servlet.ServletException, java.io.IOException {
		doGet(request, response);
	}
}


分页javabean

package com.tdb.sevice.model;


public class PageBean {

	public int maxPage; // 一共有多少页
	public int maxRowCount; // 一共有多少行
	public static final int pagesize = 15; // 每页多少行
	public PageBean() {

	}

	public int countPage() throws Exception {
		
		int maxRowCount=this.MaxCount();
		System.out.println("MaxCount="+maxRowCount);
		if (maxRowCount % this.pagesize == 0) {
			maxPage = maxRowCount / this.pagesize;
		} else {
			maxPage =maxRowCount/ this.pagesize + 1;
		}
		return maxPage;
	}


	public int MaxCount() throws Exception {
		ContactBean contact=new ContactBean();
		this.maxRowCount = contact.getAvailableCount(); // 得到总行数
		return maxRowCount;
	}

}


数据基础javabean

package com.tdb.sevice.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.tdb.util.MyOracle;

public class DeviceBean {
	
	private String deviceNo;
	private String deviceModel;
	private String deviceScope;
	private String deviceAddress;
	private String radminUserName;
	private String radminPass;
	private String radminAddress;
	
	
	public String getDeviceNo() {
		return deviceNo;
	}
	public void setDeviceNo(String deviceNo) {
		this.deviceNo = deviceNo;
	}
	public String getDeviceModel() {
		return deviceModel;
	}
	public void setDeviceModel(String deviceModel) {
		this.deviceModel = deviceModel;
	}
	public String getDeviceScope() {
		return deviceScope;
	}
	public void setDeviceScope(String deviceScope) {
		this.deviceScope = deviceScope;
	}
	public String getDeviceAddress() {
		return deviceAddress;
	}
	public void setDeviceAddress(String deviceAddress) {
		this.deviceAddress = deviceAddress;
	}
	public String getRadminUserName() {
		return radminUserName;
	}
	public void setRadminUserName(String radminUserName) {
		this.radminUserName = radminUserName;
	}
	public String getRadminPass() {
		return radminPass;
	}
	public void setRadminPass(String radminPass) {
		this.radminPass = radminPass;
	}
	public String getRadminAddress() {
		return radminAddress;
	}
	public void setRadminAddress(String radminAddress) {
		this.radminAddress = radminAddress;
	}
	
	
	
	
	private String deviceBase_insert="insert into deviceBase values (?,?,?,?,?,?,?)";
	
	public void insertnavlink() throws Exception
	{
		try{
			ResultSet rs=null;
			MyOracle myoracle = new MyOracle(deviceBase_insert);
			myoracle.setString(1, this.deviceNo);
			myoracle.setString(2, this.deviceModel);
			myoracle.setString(3, this.deviceScope);
			myoracle.setString(4, this.deviceAddress);
			myoracle.setString(5, this.radminUserName);
			myoracle.setString(6, this.radminPass);
			myoracle.setString(7, this.radminAddress);
			myoracle.executeUpdate();
			myoracle.close();

		

		} catch (Exception ex) {
			throw new Exception("insertnavlink()"+ex.getMessage());
		}
	}
	
	/**
	 * 根据编号查询设备基本信息
	 */
	private String deviceBase_select="select * from devicebase where deviceNo = ?";
	public DeviceBean getParmslink() throws Exception
	{
		DeviceBean db = null;
		try{
			ResultSet rs=null;
			MyOracle myoracle = new MyOracle();
			rs = myoracle.executeQuery(deviceBase_select);
			db = new DeviceBean();
			while(rs.next()){
				db.setDeviceNo(rs.getString(1));
				db.setDeviceModel(rs.getString(2));
				db.setDeviceScope(rs.getString(3));
				db.setDeviceAddress(rs.getString(4));
				db.setRadminUserName(rs.getString(5));
				db.setRadminPass(rs.getString(6));
				db.setRadminAddress(rs.getString(7));
			}
			myoracle.close();

		} catch (Exception ex) {
			throw new Exception("getParmslink()"+ex.getMessage());
		}
		return db;
	}
	
	/**
	 * 分页程序
	 * 查询所有的设备基本信息
	 * return ArrayList
	 */
	
	public <DeviceBase>ArrayList getListParmslink(int start, int end) throws Exception
	{
		String getListParms_select ="SELECT * FROM devicebase WHERE ROWNUM<=" + end + " minus SELECT * FROM devicebase WHERE ROWNUM<="+start;
		ArrayList al = null;
		try{
			
			MyOracle myoracle = new MyOracle();
			ResultSet rs = myoracle.executeQuery(getListParms_select);
			//System.out.println("测点一");
			al = new ArrayList();
			//System.out.println("测点二");
			while(rs.next()){
				//System.out.println("测点三");
				DeviceBean db = new DeviceBean();
			//	System.out.println("测点四");
				db.setDeviceNo(rs.getString(1));
				db.setDeviceModel(rs.getString(2));
				db.setDeviceScope(rs.getString(3));
				db.setDeviceAddress(rs.getString(4));
				db.setRadminUserName(rs.getString(5));
				db.setRadminPass(rs.getString(6));
				db.setRadminAddress(rs.getString(7));
				al.add(db);
			}
			myoracle.close();

		} catch (Exception ex) {
			throw new Exception("getListParmslink()"+ex.getMessage());
		}
		return al;
	}
	
	
	String pageSizeSelect = "select count(*) from devicebase";
	public int getPageSize(){
		MyOracle myoracle;
		ResultSet rs ;
		int pageSize = 0;
	
			try {
				myoracle = new MyOracle();
				rs = myoracle.executeQuery(pageSizeSelect);
				rs.next();
				pageSize = rs.getInt(1);
				System.out.println(pageSize);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return pageSize;
			
	}
		
	
	/**
	 * 测试数据
	 * @param args
	 */
	public static void main(String []args){
		DeviceBean db = new DeviceBean();
		try {
			for(int i=2; i<103; i++){
				db.setDeviceNo("Ae-00"+i);
				db.setDeviceModel("TFDS-30"+i);
				db.setDeviceScope("长北上行");
				db.setDeviceAddress("苏家屯");
				db.setRadminUserName("TF");
				db.setRadminPass("tf123456");
				db.setRadminAddress("192.168.255.255");
				db.insertnavlink();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


分页与数据打交道的桥javabean

package com.tdb.sevice.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;

import com.tdb.sevice.model.PageBean;
import com.tdb.util.MyOracle;

public class ContactBean {
/*获取连接数据库工具*/
private MyOracle myOracle ;

/** *返回要查询的记录数 */
public int getAvailableCount() throws Exception {
int ret = 0;
String strSql = "select count(*) from devicebase";
myOracle = new MyOracle();
ResultSet rset = myOracle.executeQuery(strSql);
while (rset.next()) {
ret = rset.getInt(1);
}
myOracle.close();
return ret;
}

/*获取查寻的记录数*/
public ArrayList getData(String topage, int pagecount)
throws Exception {
myOracle = new MyOracle();
int pagesize = PageBean.pagesize;
/*计算查询数据的起始值*/
int pageStart = (Integer.parseInt(topage) - 1) * pagesize;

/*计算查询数据的结束值*/
int pageEnd = pageStart + pagesize;

myOracle = new MyOracle();
String sql = "SELECT * FROM devicebase WHERE ROWNUM<=" + pageEnd + " minus SELECT * FROM devicebase WHERE ROWNUM<="+pageStart;;
ResultSet rs = myOracle.executeQuery(sql);
ArrayList list = new ArrayList();
int j=0;
while(rs.next()){
DeviceBean device = new DeviceBean();
device.setDeviceNo(rs.getString(1));
device.setDeviceModel(rs.getString(2));
device.setDeviceScope(rs.getString(3));
device.setDeviceAddress(rs.getString(4));
device.setRadminUserName(rs.getString(5));
device.setRadminPass(rs.getString(6));
device.setRadminAddress(rs.getString(7));
list.add(j, device);
++j;
}
myOracle.close();
return list;
}
}

你可能感兴趣的:(分页)