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

📄 buyquery.java

📁 jsp超市管理系统系统,是用经典的MVC设计模式开发的非常适合初学者学习。
💻 JAVA
字号:
package com.myservlet.buy;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;	
import javax.servlet.http.*;
import com.dbconn.ConnManager;
import com.mybean.BuyBean;
import com.mybean.Pages;
import com.mybean.Goods;

public class BuyQuery extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2898760444201742699L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		performTask(req, resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		performTask(req, resp);
	}

	private void performTask(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		
		String pageNo = req.getParameter("page");
		if (pageNo == null){
			pageNo = "1";
		}
		int	intPageNo = Integer.parseInt(pageNo);
		
		String content = req.getParameter("content");
		String condition = req.getParameter("condition");
		
		if(null != req.getParameter("content")){
			content = req.getParameter("content");
			req.getSession(true).setAttribute("content", content);
		}
		else{				
			if(null != req.getSession(true).getAttribute("content")){
				content = (String)req.getSession(true).getAttribute("content");
			}
		}
		
		if(null != req.getParameter("condition")){
			condition = req.getParameter("condition");
			req.getSession(true).setAttribute("condition", condition);
		}
		else{				
			if(null != req.getSession(true).getAttribute("condition")){
				condition = (String)req.getSession(true).getAttribute("condition");
			}
		}

		ConnManager connManager = new ConnManager();
		Connection conn = connManager.getConnection("market");  		
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql="";
		String strSql="";
			   		 
		try{			
			if(condition.equals("tname")){
				sql = Pages.query1(((intPageNo - 1) * Pages.PAGE_SIZE + 1), (intPageNo * Pages.PAGE_SIZE), content);
				sql = new String(sql.getBytes("ISO8859-1"), "gbk");
				
				strSql = "SELECT count(*) FROM goods WHERE tname='"+content+"'";
				strSql = new String(strSql.getBytes("ISO8859-1"), "gbk");
				
				System.out.println(sql);
				System.out.println(strSql);
			}
			else if(condition.equals("type")){
				sql = Pages.query2(((intPageNo - 1) * Pages.PAGE_SIZE + 1), (intPageNo * Pages.PAGE_SIZE), content);
				sql = new String(sql.getBytes("ISO8859-1"), "gbk");
				
				strSql = "SELECT count(*) FROM goods WHERE type='"+content+"'";
				strSql = new String(strSql.getBytes("ISO8859-1"), "gbk");
				
				System.out.println(sql);
				System.out.println(strSql);
			}	
			else if(condition.equals("code")){
				sql = Pages.query3(((intPageNo - 1) * Pages.PAGE_SIZE + 1), (intPageNo * Pages.PAGE_SIZE), content);
				sql = new String(sql.getBytes("ISO8859-1"), "gbk");
				
				strSql = "SELECT count(*) FROM goods WHERE code='"+content+"'";
				strSql = new String(strSql.getBytes("ISO8859-1"), "gbk");
				
				System.out.println(sql);
				System.out.println(strSql);
			}	
		
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			Collection col = new ArrayList();
				
			while(rs.next()){					
				Goods goods= new Goods();	
				
				goods.setId(rs.getInt("id"));
				goods.setCode(rs.getString("code"));
				goods.setTname(rs.getString("tname"));
				goods.setAmount(rs.getInt("amount"));
				goods.setBprice(rs.getFloat("bprice"));
				goods.setSprice(rs.getFloat("sprice"));
				goods.setType(rs.getString("type"));
				//goods.setMaker(rs.getString("maker"));
				//goods.setArea(rs.getString("area"));
				//goods.setRemark(rs.getString("remark"));
				
				col.add(goods);
			}				
			req.setAttribute("col", col);				
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		finally{
			try{
				rs.close();
				ps.close();
				conn.close();
			}
			catch(SQLException e){
				System.out.println(e);
			}
			connManager.releaseConnection("oracle", conn);
		}

		int allRecords = Pages.allRecords(strSql);	//所有记录数
		
		Pages pages = new Pages(intPageNo, allRecords, "BuyQuery?id=4", req);	
		String page = pages.allString();	//获取返回分页操作字符串
		req.setAttribute("page", page);
		String totals=String.valueOf(pages.getTotals());
		req.setAttribute("totals", totals);

		RequestDispatcher reqDispatcher = req.getRequestDispatcher("buy/buy.jsp");	//将结果返回到指定页面
		reqDispatcher.forward(req, resp);	

	}
}

⌨️ 快捷键说明

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