contactbean.java

来自「java编写的网络购物简洁版」· Java 代码 · 共 124 行

JAVA
124
字号

package shop;

import java.util.*;
import shop.productBean;
import java.sql.*;

public class ContactBean
{
	
	private Connection con = null;
	
	Vector v =null;

	DBTools db = new DBTools();
	
	/**
	 *  
	 * @throws Exception
	 */
	public ContactBean()
	{
		
		con=db.getConnection();
		v=new Vector();
	}
	
	/**
	 *  功能: 统计表中多少条记录
	 *  
	 * @return int  表中多少条记录
	 * @throws Exception
	 */
	public int getAvailableCount()
	{
		int ret=0;
		Statement stmt = null;
		try {
			stmt = con.createStatement();
			String strSql="select count(*) from product";
			ResultSet rset=stmt.executeQuery(strSql);
			while(rset.next())
			{
				ret=rset.getInt(1);
			}
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}finally{
			db.close(con,stmt);
        }
		
		return ret;	
		
	}
	
	/**
	 * 根据页数将数据放入PageBean
	 * 
	 * @param page 页数
	 * @return PageBean 返回分页Bean
	 * @throws Exception 
	 */
	
	public PageBean listData(String page)throws Exception
	{		Statement stmt = null;					//测试 如果是第2页
		try
		{
			PageBean pageBean=new PageBean(this);
			
			int pageNum=Integer.parseInt(page);
			
			stmt =con.createStatement();
			String strSql="select top "+ pageNum*pageBean.rowsPerPage+" * from product ORDER BY productName";
										// 第2页 * 每页5行
			ResultSet rset=stmt.executeQuery(strSql);
			
			int i=0;
			
			while(rset.next())
			{	
				// i >  (2-1) * 5 - 1  //第2页
				// i >  4
				// i >  (3-1) * 5 - 1  //第3页
				// i >  9
				if(i>(pageNum-1)*pageBean.rowsPerPage-1)
				{
					productBean obj = new productBean();
					obj.setProductID(rset.getInt(1));	
					obj.setBreedID(rset.getInt(2));
					obj.setProductName(rset.getString(3));
					obj.setFactory(rset.getString(4));
					obj.setPrice(rset.getFloat(5));
					obj.setAmout(rset.getInt(6));
					obj.setRemark(rset.getString(7));
				     v.add(obj);
				}
				i++;
			}
			
			rset.close();
			stmt.close();

			pageBean.curPage=pageNum;
			pageBean.data=v;
			return pageBean;
		}
		catch(Exception e)
		{
		   	e.printStackTrace();
			throw e;
			
		}finally{
			
			db.close(con,stmt);
        }
	}
	public Vector getResult()throws Exception
	{
		return v;
	}
}
	
	

⌨️ 快捷键说明

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