Servlet+JDBC+搜索+分頁

代碼倉庫+文檔:https://gitee.com/DerekAndroid/ServletJDBCSearchPage.git

效果:

分析:

批量刪除

搜索

具體思路:

1.批量刪除商品
	需求:根據數據前面的複選框選中情況,批量刪除選中的數據
	步驟分析:
		1.拷貝day17  修改web
		2.爲表頭和列表加上覆選框,加上刪除勾選的按鈕
		3.使用jquery實現全選全不選的功能
			//導入jquery.js文件
			//派發單擊事件
			//獲取表頭複選框的選中狀態
			//獲取列表中所有複選框對象
			//使列表中複選框狀態和表頭複選框狀態保持一致
		4.爲列表複選框添加name屬性和value  外面嵌套一個form表單  在點擊刪除勾選的時候派發一個click事件,讓事件方法擁有提交form表單的功能
		5.servlet的操作
			//獲取前臺傳遞的id數組
			//調用service
		6.service操作
			//開啓事物
			//遍歷數組
			//調用dao刪除
			//提交事物
			//出現異常,回滾事物
		7.dao操作
			//獲取QueryRunner對象
			//編寫sql
			//執行sql(手動獲取連接)
		
2.模糊查詢
	需求:在列表頁面有按照名稱和關鍵字查詢的兩個輸入框,輸入查詢信息後,點擊搜索按鈕,那麼在下面的列表中會顯示出符合條件的數據
	select * from product where pname like ? and pdesc like ?
	select * from product where pname like ?
	select * from product where pdesc like ?
	select * from product
	
	StringBuffer sb = new StringBuffer("select * from where 1=1");
	
	//判斷輸入的搜索項是否爲“”
	if(name!=""){
		sb.append( and pname like ?)
	}
	
	if(kw!=""){
		sb.append( and pdesc like ?)
	}
		
	
	
3.分頁

	需求:針對於從數據庫查詢的多條數據,展示在一個頁面上  分頁數展示
	作用:提高用戶的體驗度
	
	物理分頁:用戶需要看那一頁的數據,那麼就從數據庫中查詢出來當頁的數據
	邏輯分頁:把數據庫中的所有數據查詢出來,保存到內存中,當用戶需要看某一頁的數據的時候,直接從內存中讀取
	
	mysql數據庫分頁:
		limit n,m  (n是查詢的索引,m查詢幾個)
		
	第一頁: 0 3
	第二頁: 3 3
	第三頁: 6 3
	第n頁:  (當前頁-1)*m ,m
	
	
	分頁需要的數據:
		1.頁面展示的內容    select * from product limit (當前頁-1)*m ,m
		2.每頁顯示的條數    自己規定
		3.當前頁            從前臺傳遞過去
		4.總條數            select count(*) from product  
		5.總頁數            總條數/每頁顯示的條數  向上取整
		
	後臺實現:
	封裝爲PageBean
		1.List list;   //頁面展示的內容 select * from product limit (pageNumber-1)*pageSize ,pageSize
		2.int pageSize;  //每頁顯示的條數    自己規定
		3.int pageNumber  //當前頁            從前臺傳遞過去
		4.int pageCount   //總條數 select count(*) from product 
		5.int pageTotle   //總頁數     (int)Math.ceil(pageCount*1.0/pageSize)
		
	前臺實現:
	[上一頁]1 2 3 4 5 6 7[下一頁]  第幾頁/共幾頁

sql表

CREATE DATABASE day17;
USE day17;
CREATE TABLE `product` (
	`pid` VARCHAR (96),
	`pname` VARCHAR (150),
	`market_price` DOUBLE ,
	`shop_price` DOUBLE ,
	`pimage` VARCHAR (600),
	`pdate` DATE ,
	`pdesc` VARCHAR (765)
); 
INSERT INTO `product` VALUES('1','小米 4c 標準版','1399','1299','products/1/c_0001.jpg','2015-11-02','小米 4c 標準版 全網通 白色 移動聯通電信4G手機 雙卡雙待');
INSERT INTO `product` VALUES('10','華爲 Ascend Mate7','2699','2599','products/1/c_0010.jpg','2015-11-02','華爲 Ascend Mate7 月光銀 移動4G手機 雙卡雙待雙通6英寸高清大屏,纖薄機身,智能超八核,按壓式指紋識別!!選擇下方“移動老用戶4G飛享合約”,無需換號,還有話費每月返還!');
INSERT INTO `product`  VALUES('11','vivo X5Pro','2399','2298','products/1/c_0014.jpg','2015-11-02','移動聯通雙4G手機 3G運存版 極光白【購機送藍牙耳機+藍牙自拍杆】新升級3G運行內存·雙2.5D弧面玻璃·眼球識別技術');
INSERT INTO `product`  VALUES('12','努比亞(nubia)My 布拉格','1899','1799','products/1/c_0013.jpg','2015-11-02','努比亞(nubia)My 布拉格 銀白 移動聯通4G手機 雙卡雙待【嗨11,下單立減100】金屬機身,快速充電!布拉格相機全新體驗!');
INSERT INTO `product`  VALUES('13','華爲 麥芒4','2599','2499','products/1/c_0012.jpg','2015-11-02','華爲 麥芒4 晨曦金 全網通版4G手機 雙卡雙待金屬機身 2.5D弧面屏 指紋解鎖 光學防抖');
INSERT INTO `product`  VALUES('14','vivo X5M','1899','1799','products/1/c_0011.jpg','2015-11-02','vivo X5M 移動4G手機 雙卡雙待 香檳金【購機送藍牙耳機+藍牙自拍杆】5.0英寸大屏顯示·八核雙卡雙待·Hi-Fi移動KTV');
INSERT INTO `product`  VALUES('15','Apple iPhone 6 (A1586)','4399','4288','products/1/c_0015.jpg','2015-11-02','Apple iPhone 6 (A1586) 16GB 金色 移動聯通電信4G手機長期省纔是真的省!點擊購機送費版,月月送話費,月月享優惠,暢享4G網絡,就在聯通4G!');

數據庫操作Dao-ProductDao

package com.itheima.dao;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.itheima.bean.PageBean;
import com.itheima.bean.Product;
import com.itheima.utils.DataSourceUtils;

public class ProductDao {

	/**
	 * 查詢所有商品
	 * @return
	 * @throws SQLException 
	 */
	public List<Product> findAll() throws SQLException {
		//創建QueryRunner
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		//編寫sql
		String sql = "select * from product";
		//執行sql
		List<Product> query = qr.query(sql, new BeanListHandler<Product>(Product.class));
		return query;
	}

	/*
	 *添加商品 
	 */
	public void saveProduct(Product pro) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql = "insert into product values(?,?,?,?,?,?,?)";
		
		qr.update(sql, pro.getPid(),pro.getPname(),pro.getMarket_price(),pro.getShop_price(),pro.getPimage(),pro.getPdate(),pro.getPdesc());
	}
	/**
	 * 根據id查詢數據
	 * @param id
	 * @return
	 * @throws SQLException 
	 */
	public Product getProByPid(String id) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql = "select * from product where pid=?";
		
		Product query = qr.query(sql, new BeanHandler<Product>(Product.class), id);
		return query;
	}
	/**
	 * 根據id修改商品
	 * @param pro
	 * @throws SQLException 
	 */
	public void updatePro(Product pro) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql = "update product set pname=?,market_price=?,shop_price=?,pdesc=? where pid=?";
		
		qr.update(sql, pro.getPname(),pro.getMarket_price(),pro.getShop_price(),pro.getPdesc(),pro.getPid());
	}

	/**
	 * 根據id刪除商品信息
	 * @param pid
	 * @throws SQLException 
	 */
	public void deletePro(String pid) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql = "delete from product where pid = ?";
		
		qr.update(sql, pid);
	}
	/**
	 * 批量刪除
	 * @param id
	 * @throws SQLException 
	 */
	public void delCheck(String id) throws SQLException {
		//創建QueryRunner對象(不帶參數)
		QueryRunner qr = new QueryRunner();
		//編寫sql
		String sql = "delete from product where pid = ?";
		//執行sql(手動獲取連接)
		qr.update(DataSourceUtils.getConnection(), sql, id);
		
		
		
	}
	/**
	 * 模糊查詢
	 * @param name
	 * @param kw
	 * @return
	 * @throws SQLException 
	 */
	public List<Product> search(String name, String kw) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		
		StringBuilder sb = new StringBuilder("select * from product where 1=1");
		List<String> list  = new ArrayList<>();
		System.out.println(name+kw);
		if(name!=""){
			sb.append(" and pname like ?");
			list.add("%"+name+"%");
		}
		
		if(kw!=""){
			sb.append(" and pdesc like ?");
			list.add("%"+kw+"%");
		}
		
		List<Product> query = qr.query(sb.toString(), new BeanListHandler<Product>(Product.class), list.toArray());
		return query;
	}

	/**
	 * 獲取需要顯示的數據
	 * @param pb
	 * @return
	 * @throws SQLException
	 */
	public List<Product> getList(PageBean<Product> pb) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql = "select * from product limit ?,?";
		
		List<Product> query = qr.query(sql, new BeanListHandler<Product>(Product.class), pb.getIndex(),pb.getPageSize());
		return query;
	}
	/**
	 * 獲取總條數
	 * @return
	 * @throws SQLException 
	 */
	public int getPageCount() throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql = "select count(*) from product";
		
		int intValue = ((Long)qr.query(sql,new ScalarHandler())).intValue();
		return intValue;
	}

}

分頁業務bean

package com.itheima.bean;

import java.util.List;

public class PageBean<T> {

	List<T> list;   //頁面展示的內容 select * from product limit (pageNumber-1)*pageSize ,pageSize
	private int pageSize;  //每頁顯示的條數    自己規定
	private int pageNumber;  //當前頁            從前臺傳遞過去
	private int pageCount;   //總條數 select count(*) from product 
	private int pageTotle;   //總頁數     (int)Math.ceil(pageCount*1.0/pageSize)
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	
	public void setPageTotle(int pageTotle) {
		this.pageTotle = pageTotle;
	}
	
	//獲取總頁數
	public int getPageTotle() {
		return (int)Math.ceil(pageCount*1.0/pageSize);
	}
	
	//獲取數據索引
	public int getIndex(){
		return (pageNumber-1)*pageSize;
	}
	
	//定義構造
	public PageBean(int pageSize, int pageNumber) {
		super();
		this.pageSize = pageSize;
		this.pageNumber = pageNumber;
	}
	
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章