op_buy.java~28~
上传用户:sxlinghang
上传日期:2022-07-20
资源大小:1405k
文件大小:18k
源码类别:

数据库编程

开发平台:

Java

  1. package bookshop.run;
  2. /**
  3.  * <p>Title: </p>
  4.  * <p>Description: </p>
  5.  * <p>Copyright: Copyright (c) 2004</p>
  6.  * <p>Company: </p>
  7.  * @author not attributable
  8.  * @version 1.0
  9.  */
  10. import java.sql.*;
  11. import java.util.Vector;
  12. import bookshop.util.*;
  13. import bookshop.book.*;
  14. import javax.servlet.http.*;
  15. import java.util.*;
  16. public class op_buy extends DataBase {
  17.     private javax.servlet.http.HttpServletRequest request; //建立页面请求
  18.     private HttpSession session; //页面的session;
  19.     private boolean sqlflag = true; //对接收到的数据是否正确
  20.     private Vector purchaselist; //显示图书列表向量数组
  21.     private Vector allorder; //订购单列表
  22.     private Vector order_list; //订单清单列表
  23.     private int booknumber = 0; //购书总数量
  24.     private float all_price = 0; //购书总价钱
  25.     private boolean isEmpty = false; //库中的书数量是否够购买的数
  26.     private int leaveBook = 0; //库存数量
  27.     private String orderId = ""; //用户订单号
  28.     private boolean isLogin = true; //用户是否登录!
  29.     private int page = 1; //显示的页码
  30.     private int pageSize = 15; //每页显示的订单数
  31.     private int pageCount = 0; //页面总数
  32.     private long recordCount = 0; //查询的记录总数
  33.     Calendar date = Calendar.getInstance();
  34.     long time=date.getTimeInMillis();
  35.     public op_buy() {
  36.       super();
  37.   }
  38.     public Vector getPurchaselist() {
  39.       return purchaselist;
  40.     }
  41.     public Vector getOrder_list() {
  42.       return order_list;
  43.     }
  44.     public Vector getAllorder() {
  45.       return allorder;
  46.     }
  47.     public boolean getSqlflag() {
  48.       return sqlflag;
  49.     }
  50.     public void setIsEmpty(boolean flag) {
  51.       isEmpty = flag;
  52.     }
  53.     public boolean getIsEmpty() {
  54.       return isEmpty;
  55.     }
  56.     public void setLeaveBook(int bknum) {
  57.       leaveBook = bknum;
  58.     }
  59.     public int getLeaveBook() {
  60.       return leaveBook;
  61.     }
  62.     public void setOrderId(String newId) {
  63.       orderId = newId;
  64.     }
  65.     public String getOrderId() {
  66.       return orderId;
  67.     }
  68.     public void setIsLogin(boolean flag) {
  69.       isLogin = flag;
  70.     }
  71.     public boolean getIsLogin() {
  72.       return isLogin;
  73.     }
  74.     public int getPage() { //显示的页码
  75.       return page;
  76.     }
  77.     public void setPage(int newpage) {
  78.       page = newpage;
  79.     }
  80.     public int getPageSize() { //每页显示的图书数
  81.       return pageSize;
  82.     }
  83.     public void setPageSize(int newpsize) {
  84.       pageSize = newpsize;
  85.     }
  86.     public int getPageCount() { //页面总数
  87.       return pageCount;
  88.     }
  89.     public void setPageCount(int newpcount) {
  90.       pageCount = newpcount;
  91.     }
  92.     public long getRecordCount() {
  93.       return recordCount;
  94.     }
  95.     public void setRecordCount(long newrcount) {
  96.       recordCount = newrcount;
  97.     }
  98.     public String getGbk(String str) {
  99.       try {
  100.         return new String(str.getBytes("ISO8859-1"));
  101.       }
  102.       catch (Exception e) {
  103.         return str;
  104.       }
  105.     }
  106.     public String getSql() {
  107.       sqlStr = "select id,classname from book order by id";
  108.       return sqlStr;
  109.     }
  110.     /**
  111.      * 往购物车中添加选购的图书
  112.      * @param newrequest
  113.      * @return
  114.      */
  115.     public boolean addnew(HttpServletRequest newrequest) {
  116.       request = newrequest;
  117.       String ID = request.getParameter("bookid");
  118.       String Amount = request.getParameter("amount");
  119.       long bookid = 0;
  120.       int amount = 0;
  121.       try {
  122.         bookid = Long.parseLong(ID);
  123.         amount = Integer.parseInt(Amount);
  124.       }
  125.       catch (Exception e) {
  126.         return false;
  127.       }
  128.       if (amount < 1)
  129.         return false;
  130.       session = request.getSession(false);
  131.       if (session == null) {
  132.         return false;
  133.       }
  134.       purchaselist = (Vector) session.getAttribute("shopcar");
  135.       sqlStr = "select leav_number from book where id=" + bookid;
  136.       try {
  137.         DataBase db = new DataBase();
  138.                   db.connect();
  139.                   stmt = db.conn.createStatement ();
  140.         rs = stmt.executeQuery(sqlStr);
  141.         if (rs.next()) {
  142.           if (amount > rs.getInt(1)) {
  143.             leaveBook = rs.getInt(1);
  144.             isEmpty = true;
  145.             return false;
  146.           }
  147.         }
  148.         rs.close();
  149.       }
  150.       catch (SQLException e) {
  151.         return false;
  152.       }
  153.       allorder iList = new allorder();
  154.       iList.setBookNo(bookid);
  155.       iList.setAmount(amount);
  156.       boolean match = false; //是否购买过该图书
  157.       if (purchaselist == null) { //第一次购买
  158.         purchaselist = new Vector();
  159.         purchaselist.addElement(iList);
  160.       }
  161.       else { // 不是第一次购买
  162.         for (int i = 0; i < purchaselist.size(); i++) {
  163.           allorder itList = (allorder) purchaselist.elementAt(i);
  164.           if (iList.getBookNo() == itList.getBookNo()) {
  165.             itList.setAmount(itList.getAmount() + iList.getAmount());
  166.             purchaselist.setElementAt(itList, i);
  167.             match = true;
  168.             break;
  169.           } //if name matches结束
  170.         } // for循环结束
  171.         if (!match)
  172.           purchaselist.addElement(iList);
  173.       }
  174.       session.setAttribute("shopcar", purchaselist);
  175.       return true;
  176.     }
  177.     /**
  178.      * 修改已经放进购物车的数据
  179.      * @param newrequest
  180.      * @return
  181.      */
  182.     public boolean modiShoper(HttpServletRequest newrequest) {
  183.       request = newrequest;
  184.       String ID = request.getParameter("bookid");
  185.       String Amount = request.getParameter("amount");
  186.       long bookid = 0;
  187.       int amount = 0;
  188.       try {
  189.         bookid = Long.parseLong(ID);
  190.         amount = Integer.parseInt(Amount);
  191.       }
  192.       catch (Exception e) {
  193.         return false;
  194.       }
  195.       if (amount < 1)
  196.         return false;
  197.       session = request.getSession(false);
  198.       if (session == null) {
  199.         return false;
  200.       }
  201.       purchaselist = (Vector) session.getAttribute("shopcar");
  202.       if (purchaselist == null) {
  203.         return false;
  204.       }
  205.       sqlStr = "select leav_number from book where id=" + bookid;
  206.       try {
  207.         DataBase db = new DataBase();
  208.                   db.connect();
  209.                   stmt = db.conn.createStatement ();
  210.         rs = stmt.executeQuery(sqlStr);
  211.         if (rs.next()) {
  212.           if (amount > rs.getInt(1)) {
  213.             leaveBook = rs.getInt(1);
  214.             isEmpty = true;
  215.             return false;
  216.           }
  217.         }
  218.         rs.close();
  219.       }
  220.       catch (SQLException e) {
  221.         return false;
  222.       }
  223.       for (int i = 0; i < purchaselist.size(); i++) {
  224.         allorder itList = (allorder) purchaselist.elementAt(i);
  225.         if (bookid == itList.getBookNo()) {
  226.           itList.setAmount(amount);
  227.           purchaselist.setElementAt(itList, i);
  228.           break;
  229.         } //if name matches结束
  230.       } // for循环结束
  231.       return true;
  232.     }
  233.     /**
  234.      *删除购物车中数据
  235.      * @param newrequest
  236.      * @return
  237.      */
  238.     public boolean delShoper(HttpServletRequest newrequest) {
  239.       request = newrequest;
  240.       String ID = request.getParameter("bookid");
  241.       long bookid = 0;
  242.       try {
  243.         bookid = Long.parseLong(ID);
  244.       }
  245.       catch (Exception e) {
  246.         return false;
  247.       }
  248.       session = request.getSession(false);
  249.       if (session == null) {
  250.         return false;
  251.       }
  252.       purchaselist = (Vector) session.getAttribute("shopcar");
  253.       if (purchaselist == null) {
  254.         return false;
  255.       }
  256.       for (int i = 0; i < purchaselist.size(); i++) {
  257.         allorder itList = (allorder) purchaselist.elementAt(i);
  258.         if (bookid == itList.getBookNo()) {
  259.           purchaselist.removeElementAt(i);
  260.           break;
  261.         } //if name matches结束
  262.       } // for循环结束
  263.       return true;
  264.     }
  265.     /**
  266.      * 提交购物车
  267.      * @param newrequest
  268.      * @return
  269.      * @throws java.lang.Exception
  270.      */
  271.     public boolean payout(HttpServletRequest newrequest) throws Exception {
  272.       request = newrequest;
  273.       session = request.getSession(false);
  274.     // System.out.print("f1");
  275.       if (session == null) {
  276.         return false;
  277.       }
  278.       String Userid = (String) session.getAttribute("userid"); //取得用户ID号
  279.       long userid = 0;
  280.       if (Userid == null || Userid.equals("")) {
  281.         isLogin = false;
  282.         return false;
  283.       }
  284.       else {
  285.         try {
  286.           userid = Long.parseLong(Userid);
  287.         }
  288.         catch (NumberFormatException e) {
  289.         //  System.out.print(e.getMessage());
  290.           return false;
  291.         }
  292.       }
  293.   System.out.println("f2");
  294.       purchaselist = (Vector) session.getAttribute("shopcar");
  295.       if (purchaselist == null || purchaselist.size() < 0) {
  296.         return false;
  297.       }
  298.       String Content = request.getParameter("content");
  299.       if (Content == null) {
  300.         Content = "";
  301.       }
  302.      //   System.out.print("f3");
  303.       Content = getGbk(Content);
  304.       String IP = request.getRemoteAddr();
  305.       String TotalPrice = request.getParameter("totalprice");
  306.       long timeInMillis = System.currentTimeMillis();
  307.   //System.out.println("f4");
  308.       sqlStr = "insert into orders (orderId,UserId,SubmitTime,ConsignmentTime,TotalPrice,content,IPAddress,IsPayoff,IsSales) values (";
  309.       orderId=""+timeInMillis;//以系统时间产生位移的订单编号
  310.       sqlStr = sqlStr  + orderId + ",'";
  311.       sqlStr = sqlStr + userid + "',now(),now()+7,'";
  312.       sqlStr = sqlStr + TotalPrice + "','";
  313.       sqlStr = sqlStr + dataFormat.toSql(Content) + "','";
  314.       sqlStr = sqlStr + IP + "',1,1)";
  315.     //  System.out.print(sqlStr);
  316.     //String setIdSql="udate orders set orderid=orderid+id where UserId = " + userid;
  317.       try {
  318.         stmt.execute(sqlStr);
  319.         //stmt.execute(setIdSql);
  320.        // sqlStr = "select max(id) from orders where UserId = " + userid;
  321.        // rs = stmt.executeQuery(sqlStr);
  322.         //long indentid = 0;
  323.        // while (rs.next()) {
  324.          // indentid = rs.getLong(1);
  325.         //}
  326.         //rs.close();
  327.         for (int i = 0; i < purchaselist.size(); i++) {
  328.           allorder iList = (allorder) purchaselist.elementAt(i);
  329.           sqlStr =
  330.               "insert into allorder (orderId,BookNo,Amount) values (";
  331.           sqlStr = sqlStr + orderId + ",'";
  332.           sqlStr = sqlStr + iList.getBookNo() + "','";
  333.           sqlStr = sqlStr + iList.getAmount() + "')";
  334.           stmt.execute(sqlStr);
  335.           sqlStr = "update book set leav_number=leav_number - " +
  336.               iList.getAmount() + " where id = " + iList.getBookNo();
  337.           stmt.execute(sqlStr);
  338.         }
  339.         return true;
  340.       }
  341.       catch (SQLException e) {
  342.         System.out.print(e.getMessage());
  343.         return false;
  344.       }
  345.     }
  346.     /**
  347.      * 查询指定用户id的所有订单
  348.      * @param userid
  349.      * @return
  350.      */
  351.     public boolean getOrder(long userid) {
  352.       sqlStr = "select * from orders where userid = '" + userid +
  353.           "' order by id desc";
  354.       try {
  355.         DataBase db = new DataBase();
  356.                   db.connect();
  357.                   stmt = db.conn.createStatement ();
  358.         rs = stmt.executeQuery(sqlStr);
  359.         allorder = new Vector();
  360.         while (rs.next()) {
  361.           order ind = new order();
  362.           ind.setId(rs.getLong("id"));
  363.           ind.setOrderId(rs.getString("orderId"));
  364.           ind.setUserId(rs.getLong("userid"));
  365.           ind.setSubmitTime(rs.getString("submitTime"));
  366.           ind.setConsignmentTime(rs.getString("ConsignmentTime"));
  367.           ind.setTotalPrice(rs.getFloat("TotalPrice"));
  368.           ind.setContent(rs.getString("content"));
  369.           ind.setIPAddress(rs.getString("IpAddress"));
  370.           if (rs.getInt("IsPayoff") == 1)
  371.             ind.setIsPayoff(false);
  372.           else
  373.             ind.setIsPayoff(true);
  374.           if (rs.getInt("IsSales") == 1)
  375.             ind.setIsSales(false);
  376.           else
  377.             ind.setIsSales(true);
  378.           allorder.addElement(ind);
  379.         }
  380.         rs.close();
  381.         return true;
  382.       }
  383.       catch (SQLException e) {
  384.         return false;
  385.       }
  386.     }
  387.     /**
  388.      * 查询指定订单编号的订单
  389.      * @param iid
  390.      * @return
  391.      */
  392.     public boolean getSinggleOrder(String order_id) {
  393.       sqlStr = "select * from orders where orderId = '" + order_id +
  394.           "' ";
  395.       try {
  396.         DataBase db = new DataBase();
  397.                   db.connect();
  398.                   stmt = db.conn.createStatement ();
  399.         rs = stmt.executeQuery(sqlStr);
  400.         allorder = new Vector();
  401.         while (rs.next()) {
  402.           order ind = new order();
  403.           ind.setId(rs.getLong("id"));
  404.           ind.setOrderId(rs.getString("orderId"));
  405.           ind.setUserId(rs.getLong("userid"));
  406.           ind.setSubmitTime(rs.getString("submitTime"));
  407.           ind.setConsignmentTime(rs.getString("ConsignmentTime"));
  408.           ind.setTotalPrice(rs.getFloat("TotalPrice"));
  409.           ind.setContent(rs.getString("content"));
  410.           ind.setIPAddress(rs.getString("IpAddress"));
  411.           if (rs.getInt("IsPayoff") == 1)
  412.             ind.setIsPayoff(false);
  413.           else
  414.             ind.setIsPayoff(true);
  415.           if (rs.getInt("IsSales") == 1)
  416.             ind.setIsSales(false);
  417.           else
  418.             ind.setIsSales(true);
  419.           allorder.addElement(ind);
  420.           System.out.print("allorder:"+allorder.size());
  421.         }
  422.         rs.close();
  423.         return true;
  424.       }
  425.       catch (SQLException e) {
  426.         System.out.print(e.getMessage());
  427.         return false;
  428.       }
  429.     }
  430.     /**
  431.      * 查询书店的所有订单数据
  432.      * @return
  433.      */
  434.     public boolean getOrder() {
  435.       sqlStr = "select count(*) from orders"; //取出记录数
  436.       int rscount = pageSize;
  437.       try {
  438.         DataBase db = new DataBase();
  439.                   db.connect();
  440.                   stmt = db.conn.createStatement ();
  441.         ResultSet rs1 = stmt.executeQuery(sqlStr);
  442.         if (rs1.next())
  443.           recordCount = rs1.getInt(1);
  444.         rs1.close();
  445.       }
  446.       catch (SQLException e) {
  447.         return false;
  448.       }
  449.       //设定有多少pageCount
  450.       if (recordCount < 1)
  451.         pageCount = 0;
  452.       else
  453.         pageCount = (int) (recordCount - 1) / pageSize + 1;
  454.         //检查查看的页面数是否在范围内
  455.       if (page < 1)
  456.         page = 1;
  457.       else if (page > pageCount)
  458.         page = pageCount;
  459.       rscount = (int) recordCount % pageSize; // 最后一页记录数
  460.       //sql为倒序取值
  461.       sqlStr = "select  * from orders ";
  462.       if (page == 1) {
  463.         sqlStr = sqlStr + " order by Id desc";
  464.       }
  465.       else {
  466.         sqlStr = sqlStr + " order by Id desc  limit "+(recordCount - pageSize * page) +","+ (recordCount - pageSize * (page - 1));
  467.       }
  468.       try {
  469.         DataBase db = new DataBase();
  470.                   db.connect();
  471.                   stmt = db.conn.createStatement ();
  472.         rs = stmt.executeQuery(sqlStr);
  473.         allorder = new Vector();
  474.         while (rs.next()) {
  475.           order ind = new order();
  476.           ind.setId(rs.getLong("id"));
  477.           ind.setOrderId(rs.getString("orderid"));
  478.           ind.setUserId(rs.getLong("userid"));
  479.           ind.setSubmitTime(rs.getString("submitTime"));
  480.           ind.setConsignmentTime(rs.getString("ConsignmentTime"));
  481.           ind.setTotalPrice(rs.getFloat("TotalPrice"));
  482.           ind.setContent(rs.getString("content"));
  483.           ind.setIPAddress(rs.getString("IpAddress"));
  484.           if (rs.getInt("IsPayoff") == 1)
  485.             ind.setIsPayoff(false);
  486.           else
  487.             ind.setIsPayoff(true);
  488.           if (rs.getInt("IsSales") == 1)
  489.             ind.setIsSales(false);
  490.           else
  491.             ind.setIsSales(true);
  492.           allorder.addElement(ind);
  493.         }
  494.         rs.close();
  495.         return true;
  496.       }
  497.       catch (SQLException e) {
  498.         System.out.println(e);
  499.         return false;
  500.       }
  501.     }
  502.     /**
  503.      * 获得订单列表
  504.      * @param nid
  505.      * @return
  506.      */
  507.     public boolean getAllorder(String order_id) {
  508.       sqlStr = "select * from allorder where orderId = '" + order_id + "'";
  509.       try {
  510.         DataBase db = new DataBase();
  511.                   db.connect();
  512.                   stmt = db.conn.createStatement ();
  513.         rs = stmt.executeQuery(sqlStr);
  514.         order_list = new Vector();
  515.         while (rs.next()) {
  516.           allorder identlist = new allorder();
  517.           identlist.setId(rs.getLong("id"));
  518.           identlist.setOrderId(rs.getLong("orderId"));
  519.           identlist.setBookNo(rs.getLong("BookNo"));
  520.           identlist.setAmount(rs.getInt("Amount"));
  521.           order_list.addElement(identlist);
  522.         }
  523.         rs.close();
  524.         return true;
  525.       }
  526.       catch (SQLException e) {
  527.         System.out.print(e.getMessage());
  528.         return false;
  529.       }
  530.     }
  531.     /**
  532.      * 修改订单,修改付款标志
  533.      * @param res
  534.      * @return
  535.      */
  536.     public boolean update(HttpServletRequest res) {
  537.       request = res;
  538.       int payoff = 1;
  539.       int sales = 1;
  540.       //long orderId = 0;
  541.       try {
  542.         System.out.println("payoff:"+request.getParameter("payoff"));
  543.         payoff = Integer.parseInt(request.getParameter("payoff"));
  544.         sales = Integer.parseInt(request.getParameter("sales"));
  545.         orderId =request.getParameter("orderId");
  546.         sqlStr = "update orders set IsPayoff = '" + payoff + "',IsSales='" +
  547.             sales + "' where orderId =" + orderId;
  548.         DataBase db = new DataBase();
  549.                   db.connect();
  550.                   stmt = db.conn.createStatement ();
  551.                   System.out.print(sqlStr);
  552.         stmt.execute(sqlStr);
  553.         return true;
  554.       }
  555.       catch (Exception e) {
  556.         System.out.print(e.getMessage());
  557.         return false;
  558.       }
  559.     }
  560.     /**
  561.      * 删除订单
  562.      * @param id
  563.      * @return
  564.      */
  565.     public boolean delete(long id) {
  566.       try {
  567.         DataBase db = new DataBase();
  568.                   db.connect();
  569.                   stmt = db.conn.createStatement ();
  570.         sqlStr = "delete from allorder where id =" + id;
  571.         stmt.execute(sqlStr);
  572.         sqlStr = "delete from orders where id= " + id;
  573.         stmt.execute(sqlStr);
  574.         return true;
  575.       }
  576.       catch (SQLException e) {
  577.         return false;
  578.       }
  579.     }
  580.   }