javaWeb——日记本系统
日記本系統(tǒng)(更新中)
寫(xiě)在前面
系統(tǒng)采用傳統(tǒng)的Jsp+Servlet+Model的MVC開(kāi)發(fā)模式,前端采用主流的BootStrap技術(shù),數(shù)據(jù)庫(kù)采用Mysql,實(shí)現(xiàn)了個(gè)人日記本系統(tǒng),亮點(diǎn)是Bootstrap的綜合運(yùn)用,ckeditor的運(yùn)用以及FileUpload組件上傳圖片,以及MD5加密的應(yīng)用(該項(xiàng)目是非本人開(kāi)發(fā),只是學(xué)習(xí)其中的知識(shí));
一、需求:
二、數(shù)據(jù)庫(kù)設(shè)計(jì):
三張表:
t_user(userId,userName,password,nickName,imageName,mood)//password因?yàn)椴捎肕D5加密,所以設(shè)置長(zhǎng)一點(diǎn)(50)
t_diary (diaryId,title,content,typeId,releaseDate )//content 內(nèi)容長(zhǎng),datatype用text,releaseDate的格式用datetime
t_diaryType(diaryTypeId,typeName)
主外鍵設(shè)置(外鍵:其他表中的主鍵稱(chēng)為外鍵):
t_diary 中的 typeId外鍵 關(guān)聯(lián) t_diaryTaye中的 diaryTypeId主鍵 (對(duì)應(yīng)關(guān)系為多對(duì)1)
navicat設(shè)置外鍵:
外鍵名(name):可以不填, 系統(tǒng)自動(dòng)生成;
欄位名(Field Name): 就是設(shè)置‘typeId’為外鍵;
參考數(shù)據(jù)庫(kù)(Reference DadaBase): 外鍵關(guān)聯(lián)的數(shù)據(jù)庫(kù);
引用表(Reference Table): 關(guān)聯(lián)的表;
外欄位名(Outside field name): 關(guān)聯(lián)的字段(這里是“diaryTypeId”, 可以與欄位名相同);
刪除時(shí):刪除時(shí)候選擇的動(dòng)作
刷新時(shí):更新時(shí)候選擇的動(dòng)作
(在設(shè)置外鍵的時(shí)候,刪除時(shí)和更新時(shí)兩列有四個(gè)值可以選擇:CASCADE、NO ACTION、RESTRICT、SET NULL,自己全親自試了一遍,它們的區(qū)別如下:
CASCADE:父表delete、update的時(shí)候,子表會(huì)delete、update掉關(guān)聯(lián)記錄;
SET NULL:父表delete、update的時(shí)候,子表會(huì)將關(guān)聯(lián)記錄的外鍵字段所在列設(shè)為null,所以注意在設(shè)計(jì)子表時(shí)外鍵不能設(shè)為not null;
RESTRICT:如果想要?jiǎng)h除父表的記錄時(shí),而在子表中有關(guān)聯(lián)該父表的記錄,則不允許刪除父表中的記錄;
NO ACTION:同 RESTRICT,也是首先先檢查外鍵;)
設(shè)計(jì)表注意事項(xiàng):
1.對(duì)于id這種主鍵:int 型,主鍵,不允許為空,自動(dòng)遞增
2.對(duì)于日期,需要特殊格式:如datetime
3.像比較長(zhǎng)的文本如日記內(nèi)容可設(shè)置格式為text
4.對(duì)于其他的屬性,只要是包含漢字型的,如名稱(chēng),密碼都將類(lèi)型設(shè)置為varchar,長(zhǎng)度根據(jù)需求設(shè)置即可。
三.后臺(tái)代碼
四個(gè)包:
Dao:數(shù)據(jù)訪問(wèn)對(duì)象層
Model:模型層
Util:工具層
Web:web訪問(wèn)層
連接數(shù)據(jù)庫(kù):
注意事項(xiàng):添加mysql得jar包:mysql-connector-java
根據(jù)數(shù)據(jù)庫(kù)中的表建相關(guān)的類(lèi);
用于登陸賬號(hào)密碼的驗(yàn)證等
如驗(yàn)證登陸信息:
注意事項(xiàng):
代碼
package web;import java.io.IOException; import java.sql.Connection;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 dao.UserDao; import model.User; import util.DbUtil;public class LoginServlet extends HttpServlet{private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//獲取session對(duì)象,用于保存當(dāng)前登陸對(duì)象HttpSession session=req.getSession();//獲取前臺(tái)用戶(hù)信息,用于驗(yàn)證是否合法String userName=req.getParameter("userName");String password=req.getParameter("password");Connection con= null;DbUtil dbUtil=new DbUtil();UserDao userDao=new UserDao();try {con=dbUtil.getCon();User currentUser=userDao.login(con, new User(userName,password));if(currentUser==null){}else{session.setAttribute("currentUser", currentUser);resp.sendRedirect("main.jsp");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{try {dbUtil.closeCon(con);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}總結(jié)
以上是生活随笔為你收集整理的javaWeb——日记本系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数据库介绍
- 下一篇: win10怎么设置默认输入法_win10