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

📄 buy.java

📁 图书管理系统,用JSP实现,图书的查询,添加等功能
💻 JAVA
字号:
package guanli;

/**
 * 购物,查询订单
*/

import java.sql.*;
import java.util.Vector;
import beans.*;
import javax.servlet.http.*;
import java.util.*;
import java.util.Date;

public class buy extends DataBase {
    private javax.servlet.http.HttpServletRequest request; //建立页面请求
	private HttpSession session; //记录页面的session对象;
	private boolean sqlflag = true; //标记对接收到的数据是否正确
	private Vector purchaselist; //记录购物车中已购图书向量数组
	private Vector allorder; //订单向量组,记录订单对象
	private Vector order_list; //订单列表向量组,记录订单列表对象
	private int booknumber = 0; //购书总数量
	private float all_price = 0; //购书总价钱
	private boolean isEmpty = false; //库中剩余书的数量是否够购买的数
	private int leaveBook = 0; //库中书的剩余数量
	private String orderId = ""; //用户订单号
	private boolean isLogin = true; //用户是否登录!
	static int oid=1;
	static int aid=1;
	Calendar date = Calendar.getInstance();
	long time=date.getTimeInMillis();



	public buy() {
	  super();
  }
	public Vector getPurchaselist() {
	  return purchaselist;
	}
	public Vector getOrder_list() {
	  return order_list;
	}
	public Vector getAllorder() {//info.jsp
	  return allorder;
	}
	public boolean getSqlflag() {
	  return sqlflag;
	}
	public void setIsEmpty(boolean flag) {
	  isEmpty = flag;
	}
	public boolean getIsEmpty() {//buy.jsp
	  return isEmpty;
	}
	public void setLeaveBook(int bknum) {
	  leaveBook = bknum;
	}
	public int getLeaveBook() {//buy.jsp
	  return leaveBook;
	}
	public void setOrderId(String newId) {
	  orderId = newId;
	}
	public String getOrderId() {
	  return orderId;
	}
	public void setIsLogin(boolean flag) {
	  isLogin = flag;
	}
	public boolean getIsLogin() {
	  return isLogin;
	}
	public String getGbk(String str) {
	  try {
		return new String(str.getBytes("ISO8859-1"));
	  }
	  catch (Exception e) {
		return str;
	  }
	}

	public String getSql() {
	  sqlStr = "select Id,ClassName from book order by Id";
	  return sqlStr;
	}
	/**
	 * 向购物车中添加新购买的图书
	 */
	public boolean addnew(HttpServletRequest newrequest) {//buy.jsp
	  request = newrequest;
	  String ID = request.getParameter("bookid");
	  String Amount = request.getParameter("amount");
	  long bookid = 0;
	  int amount = 0;
	  try {
		bookid = Long.parseLong(ID);
		amount = Integer.parseInt(Amount);
	  }
	  catch (Exception e) {
		return false;
	  }
	  if (amount < 1)
		return false;
	  session = request.getSession(false);
	  if (session == null) {
		return false;
	  }
	  purchaselist = (Vector) session.getAttribute("shopcar");
	  sqlStr = "select Leav_number from book where Id=" + bookid;
	  try {
		DataBase db = new DataBase();
				  db.connect();
				  stmt = db.conn.createStatement ();

		rs = stmt.executeQuery(sqlStr);
		if (rs.next()) {
		  if (amount > rs.getInt(1)) {
			leaveBook = rs.getInt(1);
			isEmpty = true;
			return false;
		  }
		}
		rs.close();
	  }
	  catch (SQLException e) {
		return false;
	  }

	  allorder iList = new allorder();
	  iList.setBookNo(bookid);
	  iList.setAmount(amount);
	  boolean match = false; //是否购买过该图书
	  if (purchaselist == null) { //第一次购买
		purchaselist = new Vector();
		purchaselist.addElement(iList);
	  }

	  else { // 不是第一次购买
		for (int i = 0; i < purchaselist.size(); i++) {
		  allorder itList = (allorder) purchaselist.elementAt(i);
		  if (iList.getBookNo() == itList.getBookNo()) {
			itList.setAmount(itList.getAmount() + iList.getAmount());
			purchaselist.setElementAt(itList, i);
			match = true;
			break;
		  } //if name matches结束
		} // for循环结束
		if (!match)
		  purchaselist.addElement(iList);
	  }
	  session.setAttribute("shopcar", purchaselist);
	  return true;
	}
	/**
	 * 修改购物车中的购买数量
	 */
	public boolean modiShoper(HttpServletRequest newrequest) {//shopcar.jsp
	  request = newrequest;
	  String ID = request.getParameter("bookid");
	  String Amount = request.getParameter("amount");
	  long bookid = 0;
	  int amount = 0;
	  try {
		bookid = Long.parseLong(ID);
		amount = Integer.parseInt(Amount);
	  }
	  catch (Exception e) {
		return false;
	  }
	  if (amount < 1)//判断购买数量书否合法
		return false;
	  session = request.getSession(false);
	  if (session == null) {//判断Session对象是否还存在
		return false;
	  }
	  purchaselist = (Vector) session.getAttribute("shopcar");
	  if (purchaselist == null) {
		return false;
	  }
	  sqlStr = "select Leav_number from book where Id=" + bookid;
	  try {
		DataBase db = new DataBase();
				  db.connect();
				  stmt = db.conn.createStatement ();

		rs = stmt.executeQuery(sqlStr);
		if (rs.next()) {
		  if (amount > rs.getInt(1)) {//判断购买数量是否大于库存
			leaveBook = rs.getInt(1);
			isEmpty = true;
			return false;
		  }
		}
		rs.close();
	  }
	  catch (SQLException e) {
		return false;
	  }
	  for (int i = 0; i < purchaselist.size(); i++) {
		allorder itList = (allorder) purchaselist.elementAt(i);
		if (bookid == itList.getBookNo()) {//找到要修改的图书
		
		  itList.setAmount(amount);
		  purchaselist.setElementAt(itList, i);
		  break;
		} //if name matches结束
	  } // for循环结束
	  return true;
	}
	/**
	 *删除购物车中数据
	 */
	public boolean delShoper(HttpServletRequest newrequest) {//shopcar.jsp
	  request = newrequest;
	  String ID = request.getParameter("bookid");
	  long bookid = 0;
	  try {
		bookid = Long.parseLong(ID);
	  }
	  catch (Exception e) {
		return false;
	  }
	  session = request.getSession(false);
	  if (session == null) {
		return false;
	  }
	  purchaselist = (Vector) session.getAttribute("shopcar");
	  if (purchaselist == null) {
		return false;
	  }

	  for (int i = 0; i < purchaselist.size(); i++) {
		allorder itList = (allorder) purchaselist.elementAt(i);
		if (bookid == itList.getBookNo()) {
		  purchaselist.removeElementAt(i);
		  break;
		} //if name matches结束
	  } // for循环结束
	  return true;
	}
	/**
	 * 提交购物车
	 */
	public   boolean payout(HttpServletRequest newrequest) throws Exception 
	{//shopcar.jsp
     
	request = newrequest;
	session = request.getSession(true);
	if (session == null) {
	  return false;
	}
	String Userid = (String) session.getAttribute("userid");
	long userid = 0;
	if (Userid == null || Userid.equals("")) {
	  isLogin = false;
	  return false;
	}
	else {
	  try {
		userid = Long.parseLong(Userid);
	  }
	  catch (NumberFormatException e) {
		return false;
	  }
	}
	purchaselist = (Vector) session.getAttribute("shopcar");
	if (purchaselist == null || purchaselist.size() < 0) {
	  return false;
	}
	String Content = request.getParameter("content");
	if (Content == null) {
	  Content = "";
	}
	Content = getGbk(Content);
	String IP = request.getRemoteAddr();
	String TotalPrice = request.getParameter("totalprice");
	Date aDate=new Date();
	long timeInMillis = System.currentTimeMillis();
	sqlStr = "insert into orders ( Id,orderId,UserId,SubmitTime,ConsignmentTime,TotalPrice,content,IPAddress,IsPayoff,IsSales) values (";
	orderId=""+timeInMillis;//以系统时间产生位移的订单编号
	int n=oid;
	oid++;
	sqlStr=sqlStr+n+",";
	sqlStr = sqlStr  + orderId + ",'";
	sqlStr = sqlStr + userid + "','"+aDate+"','"+aDate+"+7','";
	sqlStr = sqlStr + TotalPrice + "','";
	sqlStr = sqlStr + dataFormat.toSql(Content) + "','";
	sqlStr = sqlStr + IP + "',1,1)";
	try {
	  stmt.execute(sqlStr);
	  for (int i = 0; i < purchaselist.size(); i++) {
		int m=aid;
		aid++;
		allorder iList = (allorder) purchaselist.elementAt(i);
		sqlStr =
			"insert into allorder (Id,orderId,BookNo,Amount) values (";
		sqlStr = sqlStr+m+",";
		sqlStr = sqlStr + orderId + ",'";
		sqlStr = sqlStr + iList.getBookNo() + "','";
		sqlStr = sqlStr + iList.getAmount() + "')";
		stmt.execute(sqlStr);
		sqlStr = "update book set Leav_number=Leav_number - " +
		iList.getAmount() + " where Id = " + iList.getBookNo();
		stmt.execute(sqlStr);
	  }
	  return true;
	}
	catch (SQLException e) {
	  System.out.print(e.getMessage());
	  return false;
	}

  }
    /**
	   * 查询指定用户id的所有订单
	   */
	  public boolean getOrder(long userid) {//info.jsp
		sqlStr = "select * from orders where UserId = '" + userid +
			"' order by Id desc";
		try {
		  DataBase db = new DataBase();
					db.connect();
					stmt = db.conn.createStatement ();

		  rs = stmt.executeQuery(sqlStr);
		  allorder = new Vector();
		  while (rs.next()) {
			order ind = new order();
			ind.setId(rs.getLong("Id"));
			ind.setOrderId(rs.getString("orderId"));
			ind.setUserId(rs.getLong("UserId"));
			ind.setSubmitTime(rs.getString("SubmitTime"));
			ind.setConsignmentTime(rs.getString("ConsignmentTime"));
			ind.setTotalPrice(rs.getFloat("TotalPrice"));
			ind.setContent(rs.getString("content"));
			ind.setIPAddress(rs.getString("IPAddress"));
			if (rs.getInt("IsPayoff") == 1)
			  ind.setIsPayoff(false);
			else
			  ind.setIsPayoff(true);
			if (rs.getInt("IsSales") == 1)
			  ind.setIsSales(false);
			else
			  ind.setIsSales(true);
			allorder.addElement(ind);
		  }
		  rs.close();
		  return true;
		}
		catch (SQLException e) {
		  return false;
		}
	  }

	/**
	 * 获得订单列表
	 */
	public boolean getAllorder(String order_id) {//info.jsp
	  sqlStr = "select * from allorder where orderId = '" + order_id + "'";
	  try {
		DataBase db = new DataBase();
				  db.connect();
				  stmt = db.conn.createStatement ();

		rs = stmt.executeQuery(sqlStr);
		order_list = new Vector();
		while (rs.next()) {
		  allorder identlist = new allorder();
		  identlist.setId(rs.getLong("Id"));
		  identlist.setOrderId(rs.getLong("orderId"));
		  identlist.setBookNo(rs.getLong("BookNo"));
		  identlist.setAmount(rs.getInt("Amount"));
		  order_list.addElement(identlist);
		}
		rs.close();
		return true;
	  }
	  catch (SQLException e) {
		System.out.print(e.getMessage());
		return false;
	  }
	}
}

⌨️ 快捷键说明

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