www.pudn.com > StoreJava.rar > BagBean.java


package com.bitc.store;  
//定义Bean所属的套件  
  
//指定欲使用的套件  
import java.sql.*;  
import java.util.Date;  
  
public class BagBean  
{  
	private String BookList = "";	  
	//起始设定BookList属性  
  
	public BagBean()  //建构子  
	{ }		  
  
	//以下为设定Bean的方法	  
  
	//判断BookList中是否有资料, 是则代表购物袋中有购买的书籍  
	public boolean HaveBook()   
	{   
		boolean result = false;  
  
		if (!BookList.equals(""))  
			result = true;   
  
		return result;			  
	}  
  
	//清除购物袋  
	public void ClearBag ()  
	{   
		BookList  = "";  
	}  
  
	//判断某书籍是否已置入购物袋  
	public boolean InBag(String BookID)  
	{   
		boolean result = true;  
  
		if(BookList.indexOf(BookID) == -1)  
			result = false;		  
		  
		return result;  
	}  
	  
	//取得某书籍的订购数量  
	public String getQty(String BookID)  
	{   
		String result = null;  
		int index = 0, start = 0, end = 0;  
		  
		index = BookList.indexOf(BookID);  
			  
		if(index != -1)  
		{  
			start = BookList.indexOf(':', index);  
			end = BookList.indexOf('&', index);  
  
			result = BookList.substring(start + 1, end);			  
		}		  
		  
		return result;  
	}  
	  
	//将预定订购的书籍与数量置入购物袋  
	public void AddBook(String BookID, String Qty)  
	{   
		BookList = BookList + BookID + ":" + Qty + "&";  
	}  
	  
	//设定某书籍的购买数量  
	public void setQty(String BookID, String Qty)  
	{   
		int index = 0;  
				  
		index = BookList.indexOf(BookID);  
		//寻找某书籍在BookList字串中的位置  
  
		if(index != -1)  
		{  
			String str1 = "", str2 = "";  
			int start = BookList.indexOf(':', index);  
			int end = BookList.indexOf('&', index);  
			//寻找 : 与 & 符号的位置, 找出原储存价格资料的位置  
  
			str1 = BookList.substring(0, start - 1);  
			str2 = BookList.substring(end);  
			BookList = str1 + Qty + str2;  
			//将新价格置入BookList  
		}		  
	}  
  
	//与资料库连结有关的Bean属性  
	Connection con = null;  
	Statement stmt = null;  
	ResultSet BagRs = null;  
  
	//起始取得购物袋中已选购书籍的查询字串  
	public void InitBagRs(Connection con)  
	{  
		if(this.con != con)  
			this.con = con;  
  
		String BookID = BookID();   
		//取得购物袋中所有订购书籍的编号		  
		  
		try{  
			stmt = con.createStatement(  
					ResultSet.TYPE_SCROLL_INSENSITIVE,  
					ResultSet.CONCUR_READ_ONLY);  
			//建立Statement物件		  
			  
			String strSQL = "SELECT * FROM Book " +   
				"WHERE BookID IN (" +	BookID + ")";  
			//建立SQL字串  
  
			BagRs = stmt.executeQuery(strSQL);  
			//执行SQL叙述  
		}  
		catch(SQLException sex)  
		{  
			System.out.println(sex.toString());  
		}						  
	}  
  
	//关闭ResultSet物件与Statement物件  
	public void CloseBagRs ()  
	{   
		try{  
			if(BagRs != null)  
			{  
				BagRs.close();				  
				BagRs = null;  
			}  
  
			if(stmt != null)  
			{  
				stmt.close();  
				stmt = null;  
			}  
		}  
		catch(SQLException sex)  
		{  
			System.out.println(sex.toString());  
		}  
	}  
  
	//将ResultSet物件的指标向後移动  
	public boolean next()  
	{   
		boolean result = false;  
  
		try{  
			result = BagRs.next();  
		}  
		catch(SQLException sex)  
		{  
			System.out.println(sex.toString());  
		}  
		return result;  
	}  
	  
	//取得记录集中的ID栏位  
	public String getBookID()  
	{   
		String result = null;  
		try{  
			result = BagRs.getString("BookID");  
		}  
		catch(SQLException sex)  
		{  
			System.out.println(sex.toString());  
		}		  
		return result;  
	}  
  
	//取得记录集中的BookNm栏位  
	public String getBookNm()  
	{   
		String result = null;  
		try{  
			result = BagRs.getString("BookNm");  
		}  
		catch(SQLException sex)  
		{  
			System.out.println(sex.toString());  
		}		  
		return result;  
	}  
	  
	//取得记录集中的Price栏位  
	public double getPrice()  
	{   
		double result = 0.0;  
		try{  
			result = BagRs.getDouble("Price");  
		}  
		catch(SQLException sex)  
		{  
			System.out.println(sex.toString());  
		}		  
		return result;  
	}  
  
	//将订单资料新增至资料库中  
	public void InsertOrder(Connection con, String Name,   
				String Address, String Phone, String E_Mail)  
	{  
		if(this.con != con)  
			this.con = con;  
  
		String BookID = BookID();   
		//取得购物袋中所有订购书籍的编号  
		  
		try{  
			String Time = Now();  
  
			stmt = con.createStatement(  
					ResultSet.TYPE_SCROLL_INSENSITIVE,  
					ResultSet.CONCUR_READ_ONLY);  
			//建立Statement物件		  
			  
			String strSQL = "INSERT INTO BookOrder " +   
			       "(OrderTime, Name, Phone, E_Mail, Address)" +   
			       " VALUES ('" + Time + "','" + Name + "','" +   
			       Phone  + "','" + E_Mail + "','" + Address + "')";  
			//建立将资料新增至资料库的SQL叙述  
  
			stmt.executeUpdate(strSQL); //执行SQL叙述  
		  
			strSQL = "SELECT ID FROM BookOrder " +   
				 "WHERE OrderTime = '" + Time + "'";  
			//从BookOrder资料表取得上次新增资料的SQL叙述  
  
			ResultSet IDRs = stmt.executeQuery(strSQL);  
			//执行SQL叙述取得ResultSet物件  
  
			IDRs.next(); //移至第一笔记录  
  
			String OrdID = IDRs.getString("ID");  
			//取得ID栏位  
		  
			IDRs.close(); //关闭ResultSet物件  
	  
			int num = 0, i = 0, end = 0, start = 0;  
			  
			end = BookList.indexOf(':');   
			//在BookList字串中寻找':'字元  
			  
			//利用while回圈将BookList中的书籍编号串接成BookID字串		  
			while(end != -1)  
			{  
				String BID = BookList.substring(start, end);  
  
				strSQL = "INSERT INTO OrderDetail " +  
     				  "(OrderID, BookID, Quity) VALUES " +  
				  "(" + OrdID + ",'" + BID + "'," +   
				  getQty(BID) + ")";  
				//建立将订购明细资料新增至OrderDetail资料表的SQL叙述  
			  
				stmt.executeUpdate(strSQL);  
				//执行新增订购明细资料的SQL叙述			  
				  
				end = BookList.indexOf(':', end + 1);			  
				start = BookList.indexOf('&', start);  
				start = start + 1;			  
			}		  
		}  
		catch(SQLException sex)  
		{  
			System.out.println(sex.toString());  
		}  
		  
	}  
  
	//建立符合资料库格式要求的系统时间  
	public String Now()  
	{  
		Date Now = new Date(); //取得目前的系统时间  
		String year = String.valueOf(Now.getYear() + 1900);  
		String month = String.valueOf(Now.getMonth() + 1);  
		String day = String.valueOf(Now.getDate());  
		String hour = String.valueOf(Now.getHours());  
		String minute = String.valueOf(Now.getMinutes());  
		String second = String.valueOf(Now.getSeconds());  
  
		return year + "/" + month + "/" + day + " "  
			+ hour + ":" + minute + ":" + second;  
		//取得目前的日期与时间  
	}  
  
	//将BookList中的书籍编号, 建立为以 , 以及 ' 串连的字串  
	public String BookID()  
	{  
		String BookID = "";		  
		int start = 0;  
		int end = 0;  
		int num = 0;  
  
		end = BookList.indexOf(':'); //在BookList字串中寻找':'字元  
  
		//利用while回圈将BookList中的书籍编号串接成BookID字串		  
		while(end != -1)  
		{  
			BookID = BookID + "'" +   
				 BookList.substring(start, end) + "', ";  
			//在书籍编号前後加上 ' 并以 , 分隔  
			  
			end = BookList.indexOf(':', end + 1);			  
			start = BookList.indexOf('&', start);  
			start = start + 1;			  
		}  
  
		return BookID;  
	}		  
}