?? buy.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; //標記對接收到的數(shù)據(jù)是否正確
private Vector purchaselist; //記錄購物車中已購圖書向量數(shù)組
private Vector allorder; //訂單向量組,記錄訂單對象
private Vector order_list; //訂單列表向量組,記錄訂單列表對象
private int booknumber = 0; //購書總數(shù)量
private float all_price = 0; //購書總價錢
private boolean isEmpty = false; //庫中剩余書的數(shù)量是否夠購買的數(shù)
private int leaveBook = 0; //庫中書的剩余數(shù)量
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循環(huán)結束
if (!match)
purchaselist.addElement(iList);
}
session.setAttribute("shopcar", purchaselist);
return true;
}
/**
* 修改購物車中的購買數(shù)量
*/
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)//判斷購買數(shù)量書否合法
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)) {//判斷購買數(shù)量是否大于庫存
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循環(huán)結束
return true;
}
/**
*刪除購物車中數(shù)據(jù)
*/
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循環(huán)結束
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;//以系統(tǒng)時間產生位移的訂單編號
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 + -