productpage.java

来自「一个简易的网上书店(jsp+sqlservler+struts)。」· Java 代码 · 共 110 行

JAVA
110
字号
package com.webshop.page;


import java.sql.*;
import java.util.*;
import com.webshop.domain.*;
import com.webshop.db.DatabaseConnection;

/**
 * @author w
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class ProductPage extends PageBusiness {

	String productTable="product";
	
	
	/**
	 *构造方法
	 */
	public ProductPage()throws Exception
	{
		super();	
		v=new Vector();
	}
	/**
	 *获得所有记录数
	 */
	public int getAvailableCount(String conditions)throws Exception
	{
		Connection conn=null;
		int ret=0;
		try
		{
				 conn=DatabaseConnection.getConnection();
				ret=this.getAvailableCountHelper(conn,productTable,conditions);
				conn.close();
	    }
	    catch(Exception e)
	    {
	    	
	    } 
	    finally
	    {
	    	try{conn.close();}catch(Exception e2){}
	    }
	    return ret;
	}

    /**
     *查询数据库,获得要显示的信息,然后通过pageBean返回
     */
	public PageBean listData(String page,String conditions)throws Exception
	{
		Connection con=null;
		try
		{
			con=DatabaseConnection.getConnection();
			PageBean pageBean=new PageBean(this,conditions);
			int pageNum=Integer.parseInt(page);
			//String searchSql="SELECT TOP "+pageBean.rowsPerPage+" productid,category,name,descn  FROM PRODUCT where "+conditions+" AND productid NOT IN (SELECT TOP "+(pageNum-1)*pageBean.rowsPerPage+" productid FROM PRODUCT where "+conditions+" ORDER BY productid) ORDER BY productid";
			String searchSql="SELECT  productid,category,name,descn  FROM PRODUCT where "+conditions+" ORDER BY productid LIMIT ?,?";
			System.out.println(searchSql);
			PreparedStatement pstmt=con.prepareStatement(searchSql);
			pstmt.setInt(1,((pageNum-1)*pageBean.rowsPerPage));
			pstmt.setInt(2,pageBean.rowsPerPage);
			ResultSet rset=pstmt.executeQuery();
			Product product;
		
			while(rset.next())
			{
								
					product=new Product();
					product.setProductId(rset.getString("productId"));
					product.setCategory(rset.getString("category"));
					product.setName(rset.getString("name"));
					product.setDescription(rset.getString("descn"));		
					
					v.add(product);
							}
			
			rset.close();
			pstmt.close();
            con.close();
			pageBean.curPage=pageNum;
			pageBean.data=v;
			return pageBean;
		}
		catch(Exception e)
		{		   
		   	e.printStackTrace();
			throw e;
			
		}
		finally
		{
				try{con.close();}catch(Exception e2){}
		}
	}
	public Vector getResult()throws Exception
	{
		return v;
	}

}

⌨️ 快捷键说明

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