?? bagbean.java
字號(hào):
package com.bitc.store;
//定義Bean所屬的套件
//指定欲使用的套件
import java.sql.*;
import java.util.Date;
public class BagBean
{
private String BookList = "";
//起始設(shè)定BookList屬性
public BagBean() //建構(gòu)子
{ }
//以下為設(shè)定Bean的方法
//判斷BookList中是否有資料, 是則代表購(gòu)物袋中有購(gòu)買的書籍
public boolean HaveBook()
{
boolean result = false;
if (!BookList.equals(""))
result = true;
return result;
}
//清除購(gòu)物袋
public void ClearBag ()
{
BookList = "";
}
//判斷某書籍是否已置入購(gòu)物袋
public boolean InBag(String BookID)
{
boolean result = true;
if(BookList.indexOf(BookID) == -1)
result = false;
return result;
}
//取得某書籍的訂購(gòu)數(shù)量
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;
}
//將預(yù)定訂購(gòu)的書籍與數(shù)量置入購(gòu)物袋
public void AddBook(String BookID, String Qty)
{
BookList = BookList + BookID + ":" + Qty + "&";
}
//設(shè)定某書籍的購(gòu)買數(shù)量
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);
//尋找 : 與 & 符號(hào)的位置, 找出原儲(chǔ)存價(jià)格資料的位置
str1 = BookList.substring(0, start - 1);
str2 = BookList.substring(end);
BookList = str1 + Qty + str2;
//將新價(jià)格置入BookList
}
}
//與資料庫(kù)連結(jié)有關(guān)的Bean屬性
Connection con = null;
Statement stmt = null;
ResultSet BagRs = null;
//起始取得購(gòu)物袋中已選購(gòu)書籍的查詢字串
public void InitBagRs(Connection con)
{
if(this.con != con)
this.con = con;
String BookID = BookID();
//取得購(gòu)物袋中所有訂購(gòu)書籍的編號(hào)
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);
//執(zhí)行SQL敘述
}
catch(SQLException sex)
{
System.out.println(sex.toString());
}
}
//關(guān)閉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物件的指標(biāo)向後移動(dòng)
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;
}
//將訂單資料新增至資料庫(kù)中
public void InsertOrder(Connection con, String Name,
String Address, String Phone, String E_Mail)
{
if(this.con != con)
this.con = con;
String BookID = BookID();
//取得購(gòu)物袋中所有訂購(gòu)書籍的編號(hào)
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 + "')";
//建立將資料新增至資料庫(kù)的SQL敘述
stmt.executeUpdate(strSQL); //執(zhí)行SQL敘述
strSQL = "SELECT ID FROM BookOrder " +
"WHERE OrderTime = '" + Time + "'";
//從BookOrder資料表取得上次新增資料的SQL敘述
ResultSet IDRs = stmt.executeQuery(strSQL);
//執(zhí)行SQL敘述取得ResultSet物件
IDRs.next(); //移至第一筆記錄
String OrdID = IDRs.getString("ID");
//取得ID欄位
IDRs.close(); //關(guān)閉ResultSet物件
int num = 0, i = 0, end = 0, start = 0;
end = BookList.indexOf(':');
//在BookList字串中尋找':'字元
//利用while回圈將BookList中的書籍編號(hào)串接成BookID字串
while(end != -1)
{
String BID = BookList.substring(start, end);
strSQL = "INSERT INTO OrderDetail " +
"(OrderID, BookID, Quity) VALUES " +
"(" + OrdID + ",'" + BID + "'," +
getQty(BID) + ")";
//建立將訂購(gòu)明細(xì)資料新增至OrderDetail資料表的SQL敘述
stmt.executeUpdate(strSQL);
//執(zhí)行新增訂購(gòu)明細(xì)資料的SQL敘述
end = BookList.indexOf(':', end + 1);
start = BookList.indexOf('&', start);
start = start + 1;
}
}
catch(SQLException sex)
{
System.out.println(sex.toString());
}
}
//建立符合資料庫(kù)格式要求的系統(tǒng)時(shí)間
public String Now()
{
Date Now = new Date(); //取得目前的系統(tǒng)時(shí)間
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;
//取得目前的日期與時(shí)間
}
//將BookList中的書籍編號(hào), 建立為以 , 以及 ' 串連的字串
public String BookID()
{
String BookID = "";
int start = 0;
int end = 0;
int num = 0;
end = BookList.indexOf(':'); //在BookList字串中尋找':'字元
//利用while回圈將BookList中的書籍編號(hào)串接成BookID字串
while(end != -1)
{
BookID = BookID + "'" +
BookList.substring(start, end) + "', ";
//在書籍編號(hào)前後加上 ' 并以 , 分隔
end = BookList.indexOf(':', end + 1);
start = BookList.indexOf('&', start);
start = start + 1;
}
return BookID;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -