基於JAVA與JSP下的網上商城設計


基於JAVA的網上商城系統的開發與實現

 

 

摘要網上商城系統是基於JAVA的一個電子商務系統,其主要作用就是實現商城的一些功能,如:買賣商品,計算積分,查看產品的具體信息,與賣家聯系,交友等功能!

最后對整個網上商城系統作了一個簡要的總結並附錄上了一些功能模塊的主要源代碼。

 

關鍵詞:網上商城系統;B/S模式;JSP技術;mysql數據庫。

 

 

 

 

 

 

 

 

 

       Based on JAVA online mall system development and implementation

 

Abstract:Online mall system is based on JAVA a e-commerce system, the main function is to realize mall of some functions, such as: buying and selling goods, calculated integral, check product specific information, contact the seller, the function such as friends!

The last of the whole online mall system gives a brief summary and appendix on some function module's main source code.

 

Keywords:online mall system; The B/S mode; JSP technology; Mysql database.

 

 

 

 

 

摘要... I

目錄:... 1

1 引言... 2

2 高校教材管理系統的需求分析... 2

2.1 用戶需求分析... 2

2.1.1 普通用戶主要需要:... 2

2.1.2 系級管理員主要需要:... 2

2.2 數據字典的描述... 2

2.3 教材管理系統的數據流圖... 2

3網上商城系統分析與設計... 3

3.1 系統設計總體思想、目標... 3

3.2 網上商城系統的ER模型... 3

3.3 網上商城系統結構、功能設計... 3

3.3.1 系統結構設計... 3

3.3.2 系統功能模塊設計... 3

3.4 網上商城系統的數據庫設計... 3

4 系統平台、計算模式及技術... 3

4.1 計算模式的進化及本系統選用的模式優越性... 3

4.1.1 集中式管理——主機/終端模式... 4

4.1.2 Client/Server模式... 4

4.1.3 Brower/Server模式... 4

4.1.4 系統采用B/S三層體系結構模式的優越性... 4

4.2 JSP 技術... 4

4.2.2  JSP內建對象... 6

4.3 mysql數據庫... 9

4.3.1 mysql概述... 9

4.3.2 mysql常用命令... 9

5 系統實現... 11

5.1 用戶登陸流程... 11

5.2  買家登陸流程... 11

5.3  賣家登陸流程... 13

5.4 用戶注冊流程... 14

5.4.1 用戶注冊流程圖... 14

5.5 數據庫表單創建語句... 15

6 網上商城系統設計的技術關鍵... 15

6.1 JSP主要網頁代碼... 15

6.2 mysql數據庫表單截圖... 22

6.3 Java語言控制主要類代碼... 23

7 系統試運行結果與評價... 26

 

 

 

1 引言    

隨着計算機的普及以及網絡技術的應用,網上商城已經成為一個新的工具和一個新興的市場,網上商城類似於現實世界當中的商店,差別是利用電子商務的各種手段,達成從買到賣的過程的虛擬商店,從而減少中間環節,消除運輸成本和代理中間的差價,造就對普通消費,和加大市場流通帶來巨大的發展空間。盡能的還消費者以利益,帶動公司發展和企業騰飛,引導國民經濟穩定快速發展,推動國內生產總值。

2 高校教材管理系統的需求分析  

本網上商城構建初始目標主要是面向廣大消費者,由於不同人群對商品的需求不同,因而購物網站應在具有自己特色的同時應適合不同人士的需要。

2.1 用戶需求分析          

用戶主要是買家和賣家以及網站的管理人員:

2.1.1 普通用戶主要需要: 

賣家:上傳商品、注冊用戶、更改商品、刪除商品、增加商品、管理已有商品等   

買家:查看所有商品、搜索某一商品、查看具體的產品以及廠家信息等

2.1.2 系級管理員主要需要:

主要是維護網站秩序,比如說終止某一買家或者賣家的使用權限等

2.2 數據字典的描述

數據字典是關於數據的信息的集合,也就是對數據流圖包含的所有元素的定義的集合。任何字典最重要的用途都是供人查閱對不了解的條目的解釋,數據字典的作用也正是在軟件分析和設計的過程中給人提供關於數據的描述信息。

限於篇幅,下面給出本系統的部分數據字典描述:

⑴ 買家信息的數據字典的描述:

  表名:mall_Buyers

  表內元組:

  BuyId  BuyName  BuySex  BuyPass  BuyLevel  BuyAddress  BuyAge      

⑵ 賣家信息的數據字典的描述:

表名:mall_Sellers

  表內元組:

SeId  SeName  SeSex  SePass  SeLevel  SeAddress  SeAge

⑶ 商品信息的數據字典的描述:

表名:mall_Goods

  表內元組:

  GoodId  GoodName  GoodPrice  Goodintroduction  SeId   Goodkinds   Goodinventory

 

 

 

 

 

2.3 教材管理系統的數據流圖

下面簡要介紹部分需求的數據流圖:

 
   

 

 

 

 

 

 

 

 

 

 

 

 

 

2.3.1 教材庫存查詢的數據流圖(DFD)

用戶操作數據流程圖:

 
   

 

 

 

 

 

 

 

 

 

圖2-1商品庫查詢的數據流圖

2.3.5 用戶信息管理的數據流圖(DFD)

⑴增加、修改和刪除用戶資料,具體的過程的數據流圖(DFD)表示如下::

3網上商城系統分析與設計

3.1 系統設計總體思想、目標

 

 

3.2 網上商城系統的ER模型

經過對網上商城系統的分析,我們將系統划分出了買家用戶、賣家用戶、商品信息三個實體,其各個實體的屬性如下:

本系統的具體ER模型如下:

 

 
   

 

 

 

 

 

 

 

 

 

3.3 網上商城系統結構、功能設計

3.3.1 系統結構設計

      網上商城系統結構主要是JAVA代碼用作控制語句、JSP用於數據接收及顯示、mysql用於術語的存儲,采用常用的Brower/Server模式。

3.3.2 系統功能模塊設計

       功能模塊主要有:登錄驗證模塊、登陸顯示模塊、數據庫操作模塊、信息顯示模塊等

3.4 網上商城系統的數據庫設計

       由於此網上商城屬於網上商城的簡易模式,所以基本數據庫表的建立有mall_Goods、mall_Buyers、mall_Sellers。

4 系統平台、計算模式及技術

⑴  系統的硬件平台:普通PC機

⑵  系統的軟件平台:MyEclipse、mysql、Navicat for MySQL、tomcat

4.1 計算模式的進化及本系統選用的模式優越性

回顧計算機的發展史,計算模式有過三次質的飛躍:

 

4.1.1 集中式管理——主機/終端模式

以主機為中心的計算環境 ,數據管理 ,事務處理高度集中 ,起始成本高 ,系統維護升級只涉及主機,管理成本低 ,但用戶端缺乏個人定置 ,無任何處理功能。適用於大規模集中式應用 ,具有較高的效率和安全性 ,但存在執行效率及容量不足問題。

4.1.2 Client/Server模式

    它把集中管理模式轉化為一種服務器與客戶機負荷均衡的分布式計算模式 ,解決了執行效率及容量不足的問題。該結構以 PC為主 ,適合部門級應用。初級成本低 ,但隨着應用規模擴展 ,網絡上異種資源類型的增多,開發、管理、維護的復雜程度加大,頻繁的軟硬件升級 ,后期成本驟升,缺乏關鍵事物處理的安全性與並發處理能力。

4.1.3 Brower/Server模式

    

以網絡中心計算為特征的 BPS結構恰到好處地取長補短 ,兼容並蓄 ,使計算體系真正成為企業生存發展的生命力。 BPS結構使數據及應用可通過不同平台、不同網絡存取 ,與平台無關 ,伸縮性大 ,為企業、行業提供了開放的基於標准的綜合性服務計算環境 ,它將處理、儲存、通訊能力移入網絡 ,集中主機 P終端和 CPS的優點 ,管理集中 ,只涉及網絡服務器 ,NC通過下載獲得升級功能 ;同時信息高度分散 ,通過 HTTP、JAVA可訪問聯接任何 URL資源和應用 ,共享程度高 ,可伸縮擴展性強 ,具有高度開放性和靈活性 ,同時具備高速率和安全性 ,使用戶真正投資於應用而不是計算機本身。

4.1.4 系統采用B/S三層體系結構模式的優越性

1、開放的標准2、分布計算的基礎結構3、較低的開發和維護成本4、使用簡單 ,界面友好5、系統靈活6、保障系統的安全性7、信息共享度高

4.2 JSP 技術

4.2.1 JSP概述

JSP(JavaServer Pages)是一種基於Java的腳本技術。在JSP 的眾多優點之中,其中之一是它能將 HTML 編碼從 Web 頁面的業務邏輯中有效地分離出來。用 JSP 訪問可重用的組件,如 Servlet、JavaBean 和基於 Java 的 Web 應用程序。JSP 還支持在 Web 頁面中直接嵌入 Java 代碼。可用兩種方法訪問 JSP 文件:瀏覽器發送 JSP 文件請求、發送至 Servlet 的請求。
  1. JSP 文件訪問 Bean 或其它能將生成的動態內容發送到瀏覽器的組件。圖5-1說明了該 JSP 訪問模型。當 Web 服務器接收到一個 JSP 文件請求時,服務器將請求發送至 WebSphere應用服務器。WebSphere應用服務器 對 JSP 文件進行語法分析並生成 Java 源文件(被編譯和執行為 Servlet)。Java 源文件的生成和編譯僅在初次調用 Servlet 時發生,除非已經更新了原始的 JSP 文件。在這種情況下,WebSphere應用服務器 將檢測所做的更新,並在執行它之前重新生成和編譯 Servlet。



圖5-1:瀏覽器發送 JSP 文件請求

  2. 發送至 Servlet 的請求生成動態內容,並調用 JSP 文件將內容發送到瀏覽器。圖5-2說明了該訪問模型。該訪問模型使得將內容生成從內容顯示中分離出來更為方便。WebSphere應用服務器 支持 HttpServiceRequest 對象和 HttpServiceResponse 對象的一套新方法。這些方法允許調用的 Servlet 將一個對象放入(通常是一個 Bean)請求對象中,並將該請求傳遞到另一個頁面(通常是一個 JSP 文件)以供顯示。調用的頁面從請求對象中檢索 Bean, 並用 JSP 來生成客戶機端的 HTML。



圖5-2:發送至 Servlet 的請求

  5.2 JSP示例
  瀏覽器通過一個Web 頁面中的HTML表單請求一個servlet(PopulateBeanServlet),該servlet創建一個名為 dataBean 的DataBean 實例,並調用 JSP 文件將內容發送到瀏覽器。Servlet 示例和 JSP 文件示例說明了啟用內容分離的 JSP 訪問模型。
  A. Servlet 是由下列Web 頁面中的 HTML 表單來調用的。
  <HTML>
  <BODY>
  <H1>運行 PopulateBeanServlet</H1>
  <P>您是否希望運行 PopulateBeanServlet?
  <FORM action="/servlet/PopulateBeanServlet" method="GET">
  <INPUT type="SUBMIT" value="Yes">
  <INPUT type="SUBMIT" value="No">
  </FORM>
  </BODY>
  </HTML>
  B. 被請求的servlet為PopulateBeanServlet, 其源代碼如下:
  /******************************************************************
  *Servlet 示例:PopulateBeanServlet.java
  *這個servlet創建一個名為 dataBean 的DataBean 實例,設置dataBean的若干個屬性,
  *將dataBean放置在當前“請求”對象中,
  *調用 JSP 文件(DisplayData.jsp)來格式化並顯示dataBean的數據
  ********************************************************************/
  import java.io.*;
  import java.beans.Beans;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import javax.ejb.CreateException;

  public class PopulateBeanServlet extends HttpServlet {
  public void Service(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException {
  try {
  dataBean = (DataBean) Beans.instantiate(this.getClass().getClassLoader(), "DataBean");
  }
  catch (Exception ex) {
  throw new ServletException("Can't create BEAN of class DataBean: "
  metaData.setSQL(getSQLString());
  }
  // Set some Bean properties (content generation)
  dataBean.setProp1("Value1");
  dataBean.setProp2("Value2");
  dataBean.setProp3("Value3");
  // To send the Bean to a JSP file for content formatting and display
  // 1) 將dataBean放置在當前“請求”對象中,
  ((com.sun.server.http.HttpServiceRequest) req).setAttribute("dataBean", dataBean);
  // 2) 使用callPage 方法調用JSP文件,文件名為DisplayData.jsp,並把請求對象傳遞給JSP。
  ((com.sun.server.http.HttpServiceResponse) res).callPage("/DisplayData.jsp", req);
  } //end of service mehtod
  } /* end of class PopulateBeanServlet */

  C. 被調用的JSP文件為DisplayData.jsp,其內容如下:
  <!-- 該 JSP 文件獲得在請求對象中傳遞的 dataBean,並顯示該 Bean 的屬性。 -->
  <html>
  <head>
  <title>Bean Data Display</title>
  </head>
  
  <!-- Get the Bean using the BEAN tag -->
  <bean name="dataBean" type="DataBean" introspect="no" create="no" scope="request">
  </bean>
  <body>
  <!-- There are three ways to access Bean properties -->
  <!-- Using a JSP scriptlet -->
  <% out.println("The value of Bean property 1 is " + dataBeans.getProp1());
  %>

  <!-- Using a JSP expression -->
  <p>The value of Bean property 2 is
  <%= dataBean.getProp2() %> </p>

  <!--Using the INSERT tag -->
  <p>The value of Bean property 3 is
  <insert bean=dataBean property=prop3 default="No property value" >
  </insert></p>

  </body>
  </html>

 

 

4.2.2  JSP內建對象

1.request對象

  客戶端的請求信息被封裝在request對象中,通過它才能了解到客戶的需求,然后做出響應。它是HttpServletRequest類的實例。

  序號 方 法 說 明

  1 object getAttribute(String name) 返回指定屬性的屬性值

  2 Enumeration getAttributeNames() 返回所有可用屬性名的枚舉

  3 String getCharacterEncoding() 返回字符編碼方式

  4 int getContentLength() 返回請求體的長度(以字節數)

  5 String getContentType() 得到請求體的MIME類型

  6 ServletInputStream getInputStream() 得到請求體中一行的二進制流

  7 String getParameter(String name) 返回name指定參數的參數值

  8 Enumeration getParameterNames() 返回可用參數名的枚舉

  9 String[] getParameterValues(String name) 返回包含參數name的所有值的數組

  10 String getProtocol() 返回請求用的協議類型及版本號

  11 String getScheme() 返回請求用的計划名,如:http.https及ftp等

  12 String getServerName() 返回接受請求的服務器主機名

  13 int getServerPort() 返回服務器接受此請求所用的端口號

  14 BufferedReader getReader() 返回解碼過了的請求體

  15 String getRemoteAddr() 返回發送此請求的客戶端IP地址

  16 String getRemoteHost() 返回發送此請求的客戶端主機名

  17 void setAttribute(String key,Object obj) 設置屬性的屬性值

  18 String getRealPath(String path) 返回一虛擬路徑的真實路徑

  2.response對象

  response對象包含了響應客戶請求的有關信息,但在JSP中很少直接用到它。它是HttpServletResponse類的實例。

  序號 方 法 說 明

  1 String getCharacterEncoding() 返回響應用的是何種字符編碼

  2 ServletOutputStream getOutputStream() 返回響應的一個二進制輸出流

  3 PrintWriter getWriter() 返回可以向客戶端輸出字符的一個對象

  4 void setContentLength(int len) 設置響應頭長度

  5 void setContentType(String type) 設置響應的MIME類型

  6 sendRedirect(java.lang.String location) 重新定向客戶端的請求

  3.session對象

  session對象指的是客戶端與服務器的一次會話,從客戶端連到服務器的一個WebApplication開始,直到客戶端與服務器斷開連接為止。它是HttpSession類的實例.

  序號 方 法 說 明

  1 long getCreationTime() 返回SESSION創建時間

  2 public String getId() 返回SESSION創建時JSP引擎為它設的惟一ID號

  3 long getLastAccessedTime() 返回此SESSION里客戶端最近一次請求時間

  4 int getMaxInactiveInterval() 返回兩次請求間隔多長時間此SESSION被取消(ms)

  5 String[] getValueNames() 返回一個包含此SESSION中所有可用屬性的數組

  6 void invalidate() 取消SESSION,使SESSION不可用

  7 boolean isNew() 返回服務器創建的一個SESSION,客戶端是否已經加入

  8 void removeValue(String name) 刪除SESSION中指定的屬性

  9 void setMaxInactiveInterval() 設置兩次請求間隔多長時間此SESSION被取消(ms)

  4.out對象

  out對象是JspWriter類的實例,是向客戶端輸出內容常用的對象

  序號 方 法 說 明

  1 void clear() 清除緩沖區的內容

  2 void clearBuffer() 清除緩沖區的當前內容

  3 void flush() 清空流

  4 int getBufferSize() 返回緩沖區以字節數的大小,如不設緩沖區則為0

  5 int getRemaining() 返回緩沖區還剩余多少可用

  6 boolean isAutoFlush() 返回緩沖區滿時,是自動清空還是拋出異常

  7 void close() 關閉輸出流

  5.page對象

  page對象就是指向當前JSP頁面本身,有點象類中的this指針,它是java.lang.Object類的實例

  序號 方 法 說 明

  1 class getClass 返回此Object的類

  2 int hashCode() 返回此Object的hash碼

  3 boolean equals(Object obj) 判斷此Object是否與指定的Object對象相等

  4 void copy(Object obj) 把此Object拷貝到指定的Object對象中

  5 Object clone() 克隆此Object對象

  6 String toString() 把此Object對象轉換成String類的對象

  7 void notify() 喚醒一個等待的線程

  8 void notifyAll() 喚醒所有等待的線程

  9 void wait(int timeout) 使一個線程處於等待直到timeout結束或被喚醒

  10 void wait() 使一個線程處於等待直到被喚醒

  11 void enterMonitor() 對Object加鎖

  12 void exitMonitor() 對Object開鎖

  6.application對象

  

  application對象實現了用戶間數據的共享,可存放全局變量。它開始於服務器的啟動,直到服務器的關閉,在此期間,此對象將一直存在;這樣在用戶的前后連接或不同用戶之間的連接中,可以對此對象的同一屬性進行操作;在任何地方對此對象屬性的操作,都將影響到其他用戶對此的訪問。服務器的啟動和關閉決定了application對象的生命。它是ServletContext類的實例。

  序號 方 法 說 明

  1 Object getAttribute(String name) 返回給定名的屬性值

  2 Enumeration getAttributeNames() 返回所有可用屬性名的枚舉

  3 void setAttribute(String name,Object obj) 設定屬性的屬性值

  4 void removeAttribute(String name) 刪除一屬性及其屬性值

  5 String getServerInfo() 返回JSP(SERVLET)引擎名及版本號

  6 String getRealPath(String path) 返回一虛擬路徑的真實路徑

  7 ServletContext getContext(String uripath) 返回指定WebApplication的application對象

  8 int getMajorVersion() 返回服務器支持的Servlet API的最大版本號

  9 int getMinorVersion() 返回服務器支持的Servlet API的最大版本號

  10 String getMimeType(String file) 返回指定文件的MIME類型

  11 URL getResource(String path) 返回指定資源(文件及目錄)的URL路徑

  12 InputStream getResourceAsStream(String path) 返回指定資源的輸入流

  13 RequestDispatcher getRequestDispatcher(String uripath) 返回指定資源的RequestDispatcher對象

  14 Servlet getServlet(String name) 返回指定名的Servlet

  15 Enumeration getServlets() 返回所有Servlet的枚舉

  16 Enumeration getServletNames() 返回所有Servlet名的枚舉

  17 void log(String msg) 把指定消息寫入Servlet的日志文件

  18 void log(Exception exception,String msg) 把指定異常的棧軌跡及錯誤消息寫入Servlet的日志文件

  19 void log(String msg,Throwable throwable) 把棧軌跡及給出的Throwable異常的說明信息 寫入Servlet的日志文件

  7.exception對象

  exception對象是一個例外對象,當一個頁面在運行過程中發生了例外,就產生這個對象。如果一個JSP頁面要應用此對象,就必須把isErrorPage設為true,否則無法編譯。他實際上是java.lang.Throwable的對象

  序號 方 法 說 明

  1 String getMessage() 返回描述異常的消息

  2 String toString() 返回關於異常的簡短描述消息

  3 void printStackTrace() 顯示異常及其棧軌跡

  4 Throwable FillInStackTrace() 重寫異常的執行棧軌跡

  8.pageContext對象

  pageContext對象提供了對JSP頁面內所有的對象及名字空間的訪問,也就是說他可以訪問到本頁所在的SESSION,也可以取本頁面所在的application的某一屬性值,他相當於頁面中所有功能的集大成者,它的本類名也叫pageContext。

  序號 方 法 說 明

  1 JspWriter getOut() 返回當前客戶端響應被使用的JspWriter流(out)

  2 HttpSession getSession() 返回當前頁中的HttpSession對象(session)

  3 Object getPage() 返回當前頁的Object對象(page)

  4 ServletRequest getRequest() 返回當前頁的ServletRequest對象(request)

  5 ServletResponse getResponse() 返回當前頁的ServletResponse對象(response)

  6 Exception getException() 返回當前頁的Exception對象(exception)

  7 ServletConfig getServletConfig() 返回當前頁的ServletConfig對象(config)

  8 ServletContext getServletContext() 返回當前頁的ServletContext對象(application)

  9 void setAttribute(String name,Object attribute) 設置屬性及屬性值

  10 void setAttribute(String name,Object obj,int scope) 在指定范圍內設置屬性及屬性值

  11 public Object getAttribute(String name) 取屬性的值

  12 Object getAttribute(String name,int scope) 在指定范圍內取屬性的值

  13 public Object findAttribute(String name) 尋找一屬性,返回起屬性值或NULL

  14 void removeAttribute(String name) 刪除某屬性

  15 void removeAttribute(String name,int scope) 在指定范圍刪除某屬性

  16 int getAttributeScope(String name) 返回某屬性的作用范圍

  17 Enumeration getAttributeNamesInScope(int scope) 返回指定范圍內可用的屬性名枚舉

  18 void release() 釋放pageContext所占用的資源

  19 void forward(String relativeUrlPath) 使當前頁面重導到另一頁面

  20 void include(String relativeUrlPath) 在當前位置包含另一文件

  9.config對象

  config對象是在一個Servlet初始化時,JSP引擎向它傳遞信息用的,此信息包括Servlet初始化時所要用到的參數(通過屬性名和屬性值構成)以及服務器的有關信息(通過傳遞一個ServletContext對象)

  序號 方 法 說 明

  1 ServletContext getServletContext() 返回含有服務器相關信息的ServletContext對象

  2 String getInitParameter(String name) 返回初始化參數的值

  3 Enumeration getInitParameterNames() 返回Servlet初始化所需所有參數的枚舉     

4.3 mysql數據庫

4.3.1 mysql概述

MySQL是一個小型關系型數據庫管理系統,開發者為瑞典MySQL AB公司。在2008年1月16號被Sun公司收購。而2009年,SUN又被Oracle收購。MySQL是一種關聯數據庫管理系統,關聯數據庫將數據保存在不同的表中,而不是將所有數據放在一個大倉庫內。這樣就增加了速度並提高了靈活性。MySQL的SQL“結構化查詢語言”。SQL是用於訪問數據庫的最常用標准化語言。MySQL軟件采用了GPL(GNU通用公共許可證)。由於其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網站為了降低網站總體擁有成本而選擇了MySQL作為網站數據庫。

 

4.3.2 mysql常用命令

1:使用SHOW語句找出在服務器上當前存在什么數據庫:
mysql> SHOW DATABASES;
2:2、創建一個數據庫MYSQLDATA
mysql> CREATE DATABASE MYSQLDATA;
3:選擇你所創建的數據庫
mysql> USE MYSQLDATA; (按回車鍵出現Database changed 時說明操作成功!)
4:查看現在的數據庫中存在什么表
mysql> SHOW TABLES;
5:創建一個數據庫表
mysql> CREATE TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));
6:顯示表的結構:
mysql> DESCRIBE MYTABLE;
7:往表中加入記錄
mysql> insert into MYTABLE values (”hyq”,”M”);
8:用文本方式將數據裝入數據庫表中(例如D:/mysql.txt)
mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt” INTO TABLE MYTABLE;
9:導入.sql文件命令(例如D:/mysql.sql)
mysql>use database;
mysql>source d:/mysql.sql;
10:刪除表
mysql>drop TABLE MYTABLE;
11:清空表
mysql>delete from MYTABLE;
12:更新表中數據
mysql>update MYTABLE set sex=”f” where name=’hyq’;

以下是無意中在網絡看到的使用MySql的管理心得,
在windows中MySql以服務形式存在,在使用前應確保此服務已經啟動,未啟動可用net start mysql命令啟動。而Linux中啟動時可用“/etc/rc.d/init.d/mysqld start”命令,注意啟動者應具有管理員權限。
剛安裝好的MySql包含一個含空密碼的root帳戶和一個匿名帳戶,這是很大的安全隱患,對於一些重要的應用我們應將安全性盡可能提高,在這里應把匿名帳戶刪除、 root帳戶設置密碼,可用如下命令進行:
use mysql;
delete from User where User=”";
update User set Password=PASSWORD(’newpassword’) where User=’root’;
如果要對用戶所用的登錄終端進行限制,可以更新User表中相應用戶的Host字段,在進行了以上更改后應重新啟動數據庫服務,此時登錄時可用如下類似命令:
mysql -uroot -p;
mysql -uroot -pnewpassword;
mysql mydb -uroot -p;
mysql mydb -uroot -pnewpassword;
上面命令參數是常用參數的一部分,詳細情況可參考文檔。此處的mydb是要登錄的數據庫的名稱。
在 進行開發和實際應用中,用戶不應該只用root用戶進行連接數據庫,雖然使用root用戶進行測試時很方便,但會給系統帶來重大安全隱患,也不利於管理技 術的提高。我們給一個應用中使用的用戶賦予最恰當的數據庫權限。如一個只進行數據插入的用戶不應賦予其刪除數據的權限。MySql的用戶管理是通過 User表來實現的,添加新用戶常用的方法有兩個,一是在User表插入相應的數據行,同時設置相應的權限;二是通過GRANT命令創建具有某種權限的用 戶。其中GRANT的常用用法如下:
grant all on mydb.* to NewUserName@HostName identified by “password” ;
grant usage on *.* to NewUserName@HostName identified by “password”;
grant select,insert,update on mydb.* to NewUserName@HostName identified by “password”;
grant update,delete on mydb.TestTable to NewUserName@HostName identified by “password”;
若 要給此用戶賦予他在相應對象上的權限的管理能力,可在GRANT后面添加WITH GRANT OPTION選項。而對於用插入User表添加的用戶,Password字段應用PASSWORD 函數進行更新加密,以防不軌之人竊看密碼。對於那些已經不用的用戶應給予清除,權限過界的用戶應及時回收權限,回收權限可以通過更新User表相應字段, 也可以使用REVOKE操作。
下面給出本人從其它資料(www.cn-java.com)獲得的對常用權限的解釋:
全局管理權限:
FILE: 在MySQL服務器上讀寫文件。
PROCESS: 顯示或殺死屬於其它用戶的服務線程。
RELOAD: 重載訪問控制表,刷新日志等。
SHUTDOWN: 關閉MySQL服務。
數據庫/數據表/數據列權限:
ALTER: 修改已存在的數據表(例如增加/刪除列)和索引。
CREATE: 建立新的數據庫或數據表。
DELETE: 刪除表的記錄。
DROP: 刪除數據表或數據庫。
INDEX: 建立或刪除索引。
INSERT: 增加表的記錄。
SELECT: 顯示/搜索表的記錄。
UPDATE: 修改表中已存在的記錄。
特別的權限:
ALL: 允許做任何事(和root一樣)。
USAGE: 只允許登錄–其它什么也不允許做。

5 系統實現

5.1 用戶登陸流程

在瀏覽器上輸入網址http://localhost:8080/online_mall/Login.jsp后會進入如下流程。

 
   

 

 

 

 

 

 

 

 

 

 

 

 

 

5.2  買家登陸流程

 

 圖5-2買家登陸界面

查詢結果:

 

 

 

圖5-3買家商品查看界面

 

 

 

 

圖5-4買家商品具體信息界面

 

圖5-5買家商品賣家具體信息界面

 

 

5.3  賣家登陸流程

 

 

 圖5-6賣家登陸界面

查詢結果:

 

圖5-7賣家商品管理界面

 

 

 

圖5-8賣家新增商品信息界面

 

 

圖5-9賣家商品信息修改界面

 

5.4 用戶注冊流程

5.4.1 用戶注冊流程圖

   

其具體的流程如圖5-10,界面如圖:

 

 

圖5-10用戶注冊界面

 

 

 

 

5.5 數據庫表單創建語句

5.5.1一個賣家表  存儲賣家信息

create table mall_Sellers(

   SeId int primary key auto_increment not null,

   SeName varchar(30),

   SeSex  varchar(10),

   SePass varchar(20),

   SeLevel  int,

   SeAddress

 varchar(50),

   SeAge int

);

 

 

5.5.2一個買家表  存儲買家信息

create table mall_Buyers(

   BuyId int primary key auto_increment not null,

   BuyName varchar(30),

   BuySex  varchar(10),

   BuyPass varchar(20),

   BuyLevel  int,

   BuyAddress

 varchar(50),

   BuyAge int

);

5.5.3一個商品表   存儲所有 賣家的商品信息

create table mall_Goods(

   GoodId int primary key auto_increment not null,

   GoodName varchar(30),

   GoodPrice  float,

   Goodintroduction

 varchar(100),

   SeId  int,

   Goodkinds

 varchar(20),

   Goodinventory

 int,

   foreign key(SeId) references mall_sellers(SeId)

);

6 網上商城系統設計的技術關鍵

6.1 JSP主要網頁代碼

 

6.1.1買家登陸主頁BuyerMallList.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="WEB-INF/c.tld" prefix="c" %>

<%@ page import="online.mall.DTO.*" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'BuyerMallList.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

 

  </head>

 

  <body>

  <form name="form1" action="" method="post">

       <table align="center" border="1" bordercolor="red">

           <tr>

              <td colspan="6" align="center" >

           </tr>

           <tr>

              <td colspan="10" align="center" >

                  <font size="9" color="red" >產品列表 </font>                     

              </td>

           </tr>

           <tr>

              <td class="td" align="center">

                  <font  color="red" >產品ID   </font>                        

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品名稱  </font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品價格  </font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品種類</font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品庫存</font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品介紹</font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >賣家信息</font>

              </td>

              <td class="td" colspan="2" align="center">

                  <font  color="red" >查看信息  </font>

              </td>

           </tr>

           <c:forEach var="good" items="${bGoodThisPageList}" >

           <tr>

              <td align="center" >

                  ${good.goodId }

              </td>

              <td align="center" >

                  ${good.goodName }                  

              </td>

              <td align="center" >

                  ${good.goodPrice }

              </td>

              <td align="center" >

                  ${good.goodkinds }

              </td>

              <td align="center" >

                  ${good.goodinventory }

              </td>

              <td align="center" >

                  ${good.goodintroduction }

              </td>

              <td align="center" >

                  ${good.goodSeId }

              </td>

               <td align="center" >

                  <a href="DoFindBygoodIdSer?id=${good.goodId }" >產品詳情</a>

              </td>

              <td align="center" >

                  <a href="DoFindBySeIdSer?id=${good.goodSeId }" >賣家詳情</a>

              </td>

           </tr>

           </c:forEach>

           <% //}%>

           <tr>

              <td colspan="10" align="center" >

              當前第 <font color="red" >${currentpage }</font>  頁

                  <a href="BuyerListServelet?currentpage=1" >首頁</a>

                  <a href="BuyerListServelet?currentpage=${currentpage-1 }" >上一頁</a>

                  <a href="BuyerListServelet?currentpage=${currentpage+1 }" >下一頁</a>

                  <a href="BuyerListServelet?currentpage=${lastpage }" >尾頁</a>

                  跳轉第<input type="text" id="pageno" name="currentpage" value="${currentpage }" size="1" > 頁

                  <input type="button" id="go" value="GO" onclick="gotoo();" >

              </td>

           </tr>

       </table>

   </form>

  </body>

  <script type="text/javascript">

    function $(id){

       return document.getElementById(id);

    }

    function chck(){

        var form = document.getElementById("form1");

       var ones = document.getElementsByName("one");

       var n = 0;

       for(var i = 0;i<ones.length;i++){

           if(ones[i].checked){

              n++;

           }

       }

       if(n==0){

           alert("請勾選");

       }else{

           form.action = "DoDeleteServlet";

           form.submit();

       }

    }

    function gotoo(){

       var pageno = $("pageno").value;

       var form = $("form1");

       if(pageno.match(/[0-9]*/)==pageno&&pageno!=""){

           form.action = "BuyerListServelet";

           form.submit();

       }else{

           alert("必須為整型數字");

       }

    }

  </script>

</html>

6.1.2賣家登陸主頁SellerMallList.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="WEB-INF/c.tld" prefix="c" %>

<%@ page import="online.mall.DTO.*" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'SellerMallList.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

  </head>

 

 <body>

  <form name="form1" action="" method="post">

       <table align="center" border="1" bordercolor="red">

           <tr>

              <td colspan="9" align="center" >

                  <font size="9" color="red" >產品列表 </font>                     

              </td>

           </tr>

           <tr>

              <td  align="center">

                  <input type="checkbox" id="all" name="all" value="" onclick="getAll();" >                 

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品ID   </font>                        

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品名稱  </font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品價格  </font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品種類</font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品庫存</font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >產品介紹</font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >賣家 編號</font>

              </td>

              <td class="td" align="center">

                  <font  color="red" >操作  </font>

              </td>

           </tr>

           <c:forEach var="good" items="${sGoodThisPageList}" >

           <tr>

               <td  align="center">

                  <input type="checkbox" id="one" name="one" value="${good.goodId }" >                

              </td>

              <td align="center" >

                  ${good.goodId }

              </td>

              <td align="center" >

                  ${good.goodName }                  

              </td>

              <td align="center" >

                  ${good.goodPrice }

              </td>

              <td align="center" >

                  ${good.goodkinds }

              </td>

              <td align="center" >

                  ${good.goodinventory }

              </td>

              <td align="center" >

                  ${good.goodintroduction }

              </td>

              <td align="center" >

                  ${good.goodSeId }

              </td>

              <td align="center" >

                  <a href="DoFindBygoodIdSer?id=${good.goodId }" >修改產品信息</a>

              </td>

           </tr>

           </c:forEach>

           <% //}%>

           <tr>

              <td colspan="9" align="center" >

              當前第 <font color="red" >${currentpage }</font>  頁

                  <a href="SellerListServlet?currentpage=1" >首頁</a>

                  <a href="SellerListServlet?currentpage=${currentpage-1 }" >上一頁</a>

                  <a href="SellerListServlet?currentpage=${currentpage+1 }" >下一頁</a>

                  <a href="SellerListServlet?currentpage=${lastpage }" >尾頁</a>

                  跳轉第<input type="text" id="pageno" name="currentpage" value="${currentpage }" size="1" > 頁

                  <input type="button" id="go" value="GO" onclick="gotoo();" >

              </td>

           </tr>

           <tr>

              <td colspan="9" align="center" >

                  <input type="button" id="sbbut" value="刪除" onclick="chck();" > 

                  <input type="button" id="but" value="新增" onclick="window.location='Goodadd.jsp';" >          

              </td>

           </tr> 

       </table>

   </form>

  </body>

  <script type="text/javascript">

    function $(id){

       return document.getElementById(id);

    }

 

    function getAll(){

       var allObj = document.getElementById("all");

       var ones = document.getElementsByName("one");

       for(var i = 0;i<ones.length;i++){

           if(allObj.checked){

              ones[i].checked = true;

           }else{

              ones[i].checked = false;

           }

       }

    }

 

    function chck(){

        var form = document.getElementById("form1");

       var ones = document.getElementsByName("one");

       var n = 0;

       for(var i = 0;i<ones.length;i++){

           if(ones[i].checked){

              n++;

           }

       }

       if(n==0){

           alert("請勾選");

       }else{

           form.action = "SelrDeleteServlet";

           form.submit();

       }

    }

 

    function gotoo(){

       var pageno = $("pageno").value;

       var form = $("form1");

       if(pageno.match(/[0-9]*/)==pageno&&pageno!=""){

           form.action = "SellerListServlet";

           form.submit();

       }else{

           alert("必須為整型數字");

       }

    }

  </script>

</html>

6.1.3網頁擴展網頁OhterWebSites.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'OhterWebSites.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

 

    </head>

  <body>

    <form name="form1" action="" method="post" >

    <table align="center" border="0" bordercolor="red" >

        <tr>

           <td  align="center" >

               <input type="button" id="rebut" value="網站大全"

           </td>

        </tr>

        <tr>

           <td  align="center" >

               <input type="button" id="rebut" value="新浪" onclick="window.location='http://www.sina.com.cn/'" > 

           </td>

        </tr>

        <tr>

           <td  align="center" >

               <input type="button" id="rebut" value="百度" onclick="window.location='http://www.baidu.com/'" > 

           </td>

        </tr>

        <tr>

           <td  align="center" >

               <input type="button" id="rebut" value="搜狐" onclick="window.location='http://www.sohu.com/'" > 

           </td>

        </tr>

        <tr>

           <td  align="center" >

               <input type="button" id="rebut" value="網易" onclick="window.location='http://www.163.com/'" > 

           </td>

        </tr>

        <tr>

           <td  align="center" >

               <input type="button" id="rebut" value="優酷" onclick="window.location='http://movie.youku.com/'" > 

           </td>

        </tr>

    </table>

    </form>

  </body>

</html>

 

6.2 mysql數據庫表單截圖

6.2.1數據庫截圖

 

6.2.2數據庫截圖mall_buyers表

 

 

6.2.3數據庫截圖mall_sellers表

 

 

6.2.3數據庫截圖mall_goods表

 

6.3 Java語言控制主要類代碼

6.3.1 DoLoginServelet登陸判斷

package online.mall.Servelet;

 

import java.io.IOException;

import java.sql.Connection;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import online.mall.DAO.BuyersDao;

import online.mall.DAO.SellersDao;

import online.mall.DAOFactory.DaoFactory;

import online.mall.DTO.BuyersDto;

import online.mall.DTO.SellersDto;

import online.mall.Datautil.DataSourceFactory;

public class DoLoginServelet extends HttpServlet {

    public void destroy() {

       super.destroy(); // Just puts "destroy" string in log

       // Put your code here

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       this.process(request,response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       this.process(request,response);

    }

    public void process (HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

//     定義一個連接

       Connection conn = null;

//     獲得Login.JSP上的一些屬性值

       String BuyerORSeller =request.getParameter("ra")==null?"":request.getParameter("ra").toString();

       String username = request.getParameter("userName")==null?"":request.getParameter("userName").toString();

       String userpass = request.getParameter("userName")==null?"":request.getParameter("userName").toString();     

       try {

           conn = DataSourceFactory.getDataSource().getConnection();

//            判斷用戶是買家還是賣家並進入相應買家或賣家頁面

           if(BuyerORSeller.equals("A")){

              BuyersDao buyerA = DaoFactory.getBuyer(conn);

              BuyersDto buyerT=buyerA.checkLogin(username, userpass);

              if(buyerT!=null){

                  HttpSession session = request.getSession();

                  request.getSession().setAttribute("buyers",buyerT); 

                  request.getSession().setAttribute("mark",BuyerORSeller)        request.getRequestDispatcher("BuyerListServelet").forward(request, response);

              }else{

                  response.sendRedirect("Login.jsp");

              }

           }else if(BuyerORSeller.equals("B")){

              SellersDao sellera = DaoFactory.getSeller(conn);

              SellersDto sellerT=sellera.checkLogin(username, userpass);

              if(sellerT!=null){

                  HttpSession session = request.getSession();

                  request.getSession().setAttribute("sellers",sellerT);

                  request.getSession().setAttribute("mark",BuyerORSeller);    request.getRequestDispatcher("SellerListServlet").forward(request, response);

              }else{

                  response.sendRedirect("Login.jsp");

              }

           }else{

              response.sendRedirect("Login.jsp");

           }

       } catch (SQLException e) {

           e.printStackTrace();

       }  

    }

    public void init() throws ServletException {

       System.out.println("init被調用");

    }

}

 

 

6.3.2 BuyerListServelet買家登陸主頁

 

package online.mall.Servelet;

 

import java.io.IOException;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import online.mall.DAO.GoodsDao;

import online.mall.DAOFactory.DaoFactory;

import online.mall.DTO.GoodsDto;

import online.mall.Datautil.DataSourceFactory;

public class BuyerListServelet extends HttpServlet {

 

    public void destroy() {

       super.destroy();

    }

 

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       this.process(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       this.process(request, response);

    }  

    public void process(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

       int num = 0;

       int lastpage = 1;

       String cpage = request.getParameter("currentpage")==null?"1":request.getParameter("currentpage");

       int currentpage = Integer.parseInt(cpage);

       List<GoodsDto> bGoodList = new ArrayList<GoodsDto>();

       Connection conn = null;

       try {

           conn = DataSourceFactory.getDataSource().getConnection();

           GoodsDao bDao = DaoFactory.getGood(conn);

           num = bDao.getRecordNum();

           if(num%10==0){

              lastpage = num/10;

           }else{

              lastpage = num/10+1;

           }

           if(currentpage<1){

              currentpage = 1;

           }

           if(currentpage>lastpage){

              currentpage = lastpage;

           }

//         可以購買的物品清單

           bGoodList = bDao.canBuyfindByPage(currentpage);

           request.setAttribute("bGoodThisPageList", bGoodList);

           request.setAttribute("currentpage", currentpage);

           request.setAttribute("lastpage",lastpage);

           request.getRequestDispatcher("BuyerMallList.jsp").forward(request, response);

          

       } catch (SQLException e) {

           e.printStackTrace();

       }

    }

    public void init() throws ServletException {

    }

}

7 系統試運行結果與評價

經過半個月的系統設計和開發,網上商城開發完畢。經測試和試運行,其功能運行良好。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM