orderBean.java
package org.thc;
import java.sql.*;
public class OrderBean {
private static String strDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static String strDBUrl = "jdbc:odbc:bookstore";
private Connection conn =null;
private ResultSet rs = null;
private java.lang.String bookinfo = null;
private java.lang.Float orderPrice = null;
private java.lang.String orderDate = null;
private java.lang.String orderID = null;
private java.lang.String orderRem = null;
private java.lang.String receiverAddress = null;
private java.lang.String receiverName = null;
private java.lang.String receiverZip = null;
private java.lang.String userID = null;
public OrderBean(){
try {
Class.forName(strDBDriver );
}
catch(java.lang.ClassNotFoundException e){
System.err.println("OrderBean ():" + e.getMessage());
}
}
public static void main(String args[]){
}
/**
* 返回定单的总价。
* @return Java.lang.String
*/
public java.lang.Float getOrderPrice() {
return orderPrice;
}
/**
*返回定单的日期 。
* @return Java.lang.String
*/
public java.lang.String getOrderDate() {
orderDate = new java.util.Date().toString();
return orderDate;
}
/**
* 返回定单的ID号。
* @return Java.lang.String
*/
public java.lang.String getOrderID() {
return orderID;
}
/**
* 返回定单的备注信息。
* @return Java.lang.String
*/
public java.lang.String getOrderRem() {
return orderRem;
}
/**
* 返回接收者的地址
* @return Java.lang.String
*/
public java.lang.String getReceiverAddress() {
return receiverAddress;
}
/**
* 返回接收者的姓名。
* @return Java.lang.String
*/
public java.lang.String getReceiverName() {
return receiverName;
}
/**
* 返回接收者的邮政编码。
* @return Java.lang.String
*/
public java.lang.String getReceiverZip() {
return receiverZip;
}
/**
* 获得用户ID。
* @return Java.lang.String
*/
public java.lang.String getUserID() {
return userID;
}
/**
* 给图书信息赋值。
* @param newBooks Java.util.Properties
*/
public void setBookinfo(java.lang.String newBookinfo) {
bookinfo = newBookinfo;
createNewOrder();
int fromIndex = 0;
int tmpIndex = 0;
int tmpEnd = 0;
String strSql = null;
try{
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
while(bookinfo.indexOf(';',fromIndex) != -1 ){
tmpEnd = bookinfo.indexOf(';',fromIndex);
tmpIndex = bookinfo.lastIndexOf('=',tmpEnd);
//// strSql = "insert into orderdetail (orderID ,bookISBN ,bookcount)values( getOrderID() , '" + bookinfo.substring(fromIndex ,tmpIndex) + "','"+ bookinfo.substring(tmpIndex+1 ,tmpEnd) + "' )";
stmt.executeUpdate(strSql);
fromIndex = tmpEnd + 1;
}
stmt.close();
conn.close();
}
catch(SQLException e){
System.err.println("BuyerBean.getLogontimes():" + e.getMessage());
}
}
/**
*给定单的总价赋值。
* @param newOderprice Java.lang.Float
*/
public void setOrderPrice(java.lang.Float newOrderPrice) {
orderPrice = newOrderPrice;
}
/**
* 给定单的备注赋值。
* @param newOrderRem Java.lang.String
*/
public void setOrderRem(java.lang.String newOrderRem) {
orderRem = newOrderRem;
}
/**
* 给接收者的地址赋值。
* @param newReceiverAddress Java.lang.String
*/
public void setReceiverAddress(java.lang.String newReceiverAddress) {
receiverAddress = newReceiverAddress;
}
/**
* 给接收者的姓名赋值。
* @param newReceiverName Java.lang.String
*/
public void setReceiverName(java.lang.String newReceiverName) {
receiverName = newReceiverName;
}
/**
* 给接收者的邮政编码代码赋值。
* @param newReceiverZip Java.lang.String
*/
public void setReceiverZip(java.lang.String newReceiverZip) {
receiverZip = newReceiverZip;
}
/**
* 给用户代码赋值。
* @param newUserID Java.lang.String
*/
public void setUserID(java.lang.String newUserID) {
userID = newUserID;
}
/**
* 创建一个新定单
*/
private void createNewOrder() {
String strSql = null;
try{
conn = DriverManager.getConnection(strDBUrl);
Statement stmt = conn.createStatement();
//// strSql = "insert into orderInfo (userID,receiverName,receiverAddress,receiverZip,orderRem,orderPrice,Orderdate)"
+ values( '" getUserID()+"' , '" + getReceiverName() + "', '"+ getReceiverAddress() + "', '" + getReceiverZip() + "', '" + getOrderRem() + "', '"+ getOrderPrice() + "' ,'" getOrderDate() + "')" ;
stmt.executeUpdate(strSql);
strSql = "select max(OrderID) from orderInfo where userID = '" getUserID()
+ "' and receiverName = '" + getReceiverName()
+ "' and receiverAddress = '" + getReceiverAddress()
+"' and receiverZip = '" + getReceiverZip()
+"' and orderRem = '" + getOrderRem()
+ "' and orderPrice = " + getOrderPrice()
+" and Orderdate = '" getOrderDate() + "'" ;
orderID = null;
rs = stmt.executeQuery(strSql);
while (rs.next()){
orderID = rs.getString("OrderID");
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException e){
System.err.println("BuyerBean.getLogontimes():" + e.getMessage());
}
}
}
order.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page language="java" contentType="text/html;charset=GB2312"%>
<jsp:useBean class="org.thc.BookBean" id="bookinfo" scope="page"></jsp:useBean>
<jsp:useBean class="org.thc.OrderBean" id="orderBean" scope="page"></jsp:useBean>
<%
/*禁止使用浏览器Cache,网页立即失效*/
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires",0);
%>
<HTML>
<HEAD>
<META http-equiv="Content-Style-Type" content="text/css">
<TITLE>
填写订单
</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<%
if ("send".equals(request.getParameter("send")))
{
///orderBean.setUserID(session.getValue("memberID"));
String str=request.getParameter("receivername");
orderBean.setReceiverName(str==null?"":str);
str=request.getParameter("orderprice");
///orderBean.setOrderPrice(java.lang.Float.valueOf(str==null?"0":str).floatValue());
str=request.getParameter("address");
orderBean.setReceiverAddress(str==null?"":str);
str=request.getParameter("postcode");
orderBean.setReceiverZip(str==null?"":str);
str=request.getParameter("bookinfo");
orderBean.setBookinfo(str==null?"":str);
str=request.getParameter("memo");
orderBean.setMemo(str==null?"":str);
int orderID=orderBean.getOrderID();
if (orderID>0)
{ /*清空Cookie(购物车)信息*/
Cookie[] cookies=request.getCookies();
for (int i=0;i<cookies.length;i++)
{ String isbn=cookies[i].getName();
if (isbn.startsWith("ISBN")&&isbn.length()==17)
{ Cookie c=new Cookie(isbn,"0");
c.setMaxAge(0);//设定Cookie立即失效
response.addCookie(c);
}
}
%>
<p align="center">订购成功!</p>
<p align="center"> 订单号:<%=orderID%></p>
<p align="center"><a href="booklist.jsp">返回首页</a></p>
<%
}
else
{
out.print("订购失败\n");
}
}
else
{ float price=0;
String bookInfo="";
%>
<FORM method="post" name="frm">
<TABLE border="1" width="100%" cellspacing="0" bordercolor="#9999FF">
<TR>
<TD width="90">ISBN</TD>
<TD width="269">书名</TD>
<TD width="50">单价</TD>
<TD width="75">数量</TD>
<TD width="48">价格 </TD>
</TR>
<% /*读取购物车信息*/
Cookie[] cookies=request.getCookies();
for (int i=0;i<cookies.length;i++)
{ String isbn=cookies[i].getName();
String num=cookies[i].getValue();
if (isbn.startsWith("ISBN")&&isbn.length()==17)
{
bookinfo.setBookISBN(isbn.substring(4,17));
Float bookPrice = new Float(bookinfo.getPrice());
%>
<TR>
<TD width="90"><%= bookinfo.getBookISBN()%></TD>
<TD width="269"><A href="bookinfo.jsp?isbn=<%= bookinfo.getBookISBN()%>"><%= bookinfo.getBookName()%></A></TD>
<TD width="50"><%= bookPrice%></TD>
<TD width="75">
<INPUT size="5" type="text" maxlength="5" value="<%= num%>" name="num" readonly></TD>
<TD width="48"><%= bookPrice.floatValue() * java.lang.Integer.parseInt(num)%></TD>
</TR>
<%
price += bookPrice.floatValue()*java.lang.Integer.parseInt(num);
bookInfo += bookinfo.getBookISBN()+"="+num+";";
}
}