基于 Java 和 MySQL 的精品課程網站設計與實現
一、 畢業設計(論文)任務書
摘要:近年來,教育信息化發展十分迅猛,人們的教育觀念、教育手段、學習方法、學習渠道等等都發生了重大的變化。知識性人才也已經日益成為了一個國家繁榮昌盛和民族偉大振興的決定性因素和強大資源后盾。
遠程教育這一基于日益發達的計算機技術和早已大規模普及的通訊技術的現代教育方式,正以其自身強大優勢,成為現代教育的一個亮點和重點。在傳統的教育教學模式中,授課老師通過面授,直接進行教學活動,學生只能在課堂上學習知識。然而教學活動不僅受到了時間的限制同時也受到了空間束縛,師生之間不能及時的進行交流、問題得不到及時的解決,甚至還存在教育資源緊缺不能共享等問題。得益于信息技術的支持與發展及問題急于解決的緊迫性,在線學習類系統的開發與實現成為了必要趨勢。
基于與 Java 和 MySQL 的精品課程網站以 JSP、Servlet 和 Java Bean 為基礎,采用 MySQL 數據庫及 Tomcat 服務器技術進行網站設計和開發。首先對相關的 Java Web 成功案例進行學習,再根據師生的使用需求,對精品課程網進行詳細的系統需求分析,然后根據分析結果設計功能、處理邏輯、編寫模塊代碼。
經過多次分析及測試,最終表明基于與 Java 和 MySQL 的精品課程網站可穩定運行,符合師生的使用需求,且用戶體驗良好,具有一定的價值效益。
關鍵詞:Java Web ?課程網 ?在線學習
二、前言
背景
人類自從進入 21 世紀,科學技術飛速發展,知識經濟初見端倪。一個國家綜合國力的競爭更主要地體現在了國民素質和人才資源上,而人才的培養,關鍵在于教育。當下的社會將對勞動者們有著前所未有的高要求,這講使得教育目標發生了本質上的變化。終身學習和繼續教育成為了社會生活中必不可少的需求。在任何時間任何地點都可以接受所需的教育是未來教育的根本目標。
在市場日益繁榮,經濟文化空前發展的今天,現有的教育模式根本無法滿足人們教育學習的需求。因此,培養現代化建設的高素質人才,使有限的教育資源得到充分合理利用的任務刻不容緩。
在這種現狀下,我國迫切需要采取某種技術手段,充分利用各種資源,增強教育普及、縮小城鄉差距,培育現代化創新性人才,從而構建一個終身學習的體系。
國內外研究概況及發展趨勢
在國外,網絡教學的發展較國內更加迅速,除了普及廣泛外,已經向更高層次的應用和標準化方面發展。信息化的網絡教學和學習方式已經成為了一種趨勢,并即將成為教學教育活動的主流。
以美國為例,美國是目前通過網絡進行教育規模最大的國家。近年來,美國高校通過網絡進行遠程教育,課程建設快速發展,達到了十多萬種。其中本科課程 3.5 萬種,研究生課程 1.4 萬種,非學歷學位課程包括各種知識普及課程 6 萬多種。所有的教學活動和教育管理都是通過網絡教學技術實現的,網絡教學技術幾乎成為了廣大學員獲取知識和相關技能的主要途徑。
在國內,關于教學信息化我國已有一些網絡教學平臺,如:軟酷網、爾雅課程網、網易公開課等,它們都為我們提供了良好的學習平臺。如今,網絡已基本普及到全國各大中小城市,而網絡教育資源仍不是很豐富,因此在未來,我國的網上教育平臺的建設將有很大的前景。要提高我國網絡教學水平,必須注重網絡教育的教學設計,提供更多在線教育平臺,加強適應個性化教學,加強資源庫的標準化建設。
研究意義
由于現有教學資源的復雜多樣,各種不同屬性教學資源紛繁復雜,不易于管理與利用。為了更加有效地建設教學資源庫,使得各項教育資源可以得到充分的利用和有效地管理,增強師生之間的交流,提高學生的自我學習能力,在線教育系統的探索與研究十分必要。
基于 Java 和 MySQL 的精品課程網站,其主要目的是為了提供一個全新的學習、教育、交流平臺。它服務于不同層次的需求者和使用者。用戶可反復觀看視頻進行深入學習;學生可以通過登錄網站進行相應課程的學習,可以在線進行測試,且能立即查看分數與錯題,及時復習并提高學習效率,還可以在網上進行留言提問,跟大家一起交流。教師可以讓同學們直接在網上提交作業,還可以通過該平臺直接獲取作業的提交情況。
這樣的網站為教師教學、學生自學及普通瀏覽的游客提供了優質、高效的服務。在提高教育質量、擴大教育規模、緩解教育資源緊張等方面存在重大意義。
可行性分析
基于 Java 和 MySQL 的精品課程網站,使用上滿足了教育資源共享、在線測試、在線提交作業等各項需求,實現了教師和學生之間的信息交流;管理上,管理員可發布公告方便于讓登入網站的用戶及時了解到公布的重要信息,也可以對用戶帳號進行相應管理,操作簡單便于維護;技術上,使用 JSP、servlet、AJAX、jstl、jQuery 等開源技術進行實現,資料豐富,編寫相對簡單。
需求分析
功能分析
在傳統的學習過程中很都學生都反應枯燥無味,課堂上學習的知識很快就會忘記,問題也得不到及時的解決。同學們總是推遲提交作業,學生越多老師越難統計作業提交情況。因此,基于 Java 和 MySQL 的精品課程網站應幫助用戶提高學習興趣,實現高效率的教育教學。
綜上所述,為滿足所需人群的要求,可將角色分為普通游客、學生、教師、管理員四種,不同的角色有不同的使用權限。普通游客只能瀏覽網站中的視頻,學生用戶則還可以在此平臺進行測試、下載資料、留言討論、提交作業等,教師用戶可在線出題、上傳資料等,管理員可以對用戶進行管理,也可以發布公告。
模塊分析
- 用戶注冊模塊:普通游客只能瀏覽首頁或進入課程中心觀看視,但可通過此功能注冊成為本站的學生用戶,獲得更多使用權限。
- 用戶登錄模塊:登錄用戶可進入其他內置頁面,通過判斷角色來顯示相應的功能。管理員登錄與其他用戶登錄頁面分開。
- 在線測試模塊:只有學生用戶可進入,實現試卷查詢(根據試卷關鍵字查詢試卷)、生成試卷(題庫中隨機抽取題目自動生成試卷)、試題批改(提交答案后顯示相應分數)等功能。
- 師生互動模塊:實現主題查詢(根據主題關鍵字查詢留言)、科目查詢(根據科目類型查詢留言)、創建留言、回復留言等功能。
- 今日作業模塊:學生用戶進入模塊,實現作業查詢(根據科目及作業布置日期查詢提交過的作業)、作業下載(下載之前已提交過的作業)、提交作業(選擇完科目、任課教師、布置日期信息后上傳作業文檔)等功能。教師用戶進入模塊,顯示最近一次布置日期的作業提交情況(展示科目、提交人數、未提交人姓名),可根據科目及具體布置日期查詢詳細作業提交情況,可下載相應的作業。
- 資料下載模塊:學生用戶進入模塊,實現資料查詢(根據關鍵詞查詢相應資料)、資料下載等功能。教師用戶進入模塊,實現資料查詢、資料下載、我的上傳(顯示本人已上傳文件列表,可進行刪除或下載操作)、上傳文件等功能。
- 在線出題模塊:只有教師用戶可進入,實現題目查詢(根據關鍵字查詢自己所出的題目)、題目修改、題目刪除、題目添加功能。
- 用戶管理模塊:只有管理員用戶可進入,實現查詢用戶(根據用戶名查詢)、凍結用戶、初始化密碼、添加教師用戶等功能。
- 公告管理模塊:只有管理員用戶可進入,實現發布公告、查詢公告、刪除公告等功能。
概要設計
結構模塊圖描述
數據庫結構設計
- 關系模型
- 用戶表(用戶 id、帳號、密碼、姓名、科目、學校、角色、刪除標識、注冊時間)
- 題目表(題目 id、科目、題目標題、選項 A、選項 B、選項 C、選項 D、正確答案、困難度、更新時間、用戶 id)
- 用戶答案表(答案 id、用戶 id、試卷 id、用戶答案、創建時間)
- 試卷表(試卷 id、試卷題目、題目答案、用戶 id、科目、困難度、創建時間)
- 作業表(作業 id、科目、布置時間、提交時間、用戶 id、接受人姓名、作業文件名字)
- 留言表(留言 id、留言主題、科目、內容、用戶 id、創建時間)
- 回復表(回復 id、留言 id、用戶 id、回復內容、回復時間)
- 上傳表(上傳 id、用戶 id、科目、標題、大小、存儲路徑、上傳時間、文件后綴、刪除標記)
數據字典
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
u_id | int | 11 | N | 用戶 id,主鍵自增 | |
username | varchar | 20 | Y | NULL | 帳號 |
password | varcahr | 20 | Y | NULL | 密碼 |
name | varchar | 20 | Y | NULL | 姓名 |
subject | varchar | 30 | Y | NULL | 科目 |
school | varchar | 30 | Y | NULL | 學校 |
role | int | 11 | Y | NULL | 角色 ? 管理員:1 教師:2;學生:3 |
del | int | 2 | Y | 1 | 刪除標識 凍結:0 正常:1 |
create_time | varchar | 20 | Y | NULL | 注冊時間 |
表 3-2-1 用戶表(user)
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
t_id | int | 11 | N | 題目 id,主鍵自增 | |
subject | varchar | 30 | Y | NULL | 科目 |
title | varcahr | 500 | Y | NULL | 題目標題 |
optiona | varchar | 200 | Y | NULL | 選項 a |
optionb | varchar | 200 | Y | NULL | 選項 b |
optionc | varchar | 200 | Y | NULL | 選項 c |
optiond | varchar | 200 | Y | NULL | 選項 d |
answer | varchar | 10 | Y | NULL | 正確答案 |
difficulty | varchar | 10 | Y | NULL | 困難度 |
update_time | varchar | 20 | Y | NULL | 更新時間 |
u_id | int | 11 | Y | NULL | 用戶 id,對應用戶表(user) |
表 3-2-2 題目表(topic)
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
upload_id | int | 11 | N | 上傳 id,主鍵自增 | |
u_id | int | 11 | N | NULL | 用戶 id,對應用戶表(user) |
subject | varchar | 30 | Y | NULL | 資料相關科目 |
title | varchar | 100 | Y | NULL | 資料標題 |
size | varchar | 10 | Y | NULL | 文件大小 |
path | varchar | 200 | Y | NULL | 文件存儲路徑 |
upload_time | varchar | 20 | Y | NULL | 上傳時間 |
ext | varchar | 5 | Y | NULL | 文件后綴 |
del | int | 2 | Y | 1 | 刪除標記 ?正常:1 刪除:0 |
表 3-2-3 上傳表(upload)
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
test_id | varchar | 100 | N | 試卷 id,主鍵自增 | |
alltopic | varchar | 100 | Y | NULL | 該試卷的全部題目 |
allanswer | varcahr | 100 | Y | NULL | 該試卷全部題目的答案 |
u_id | int | 11 | Y | NULL | 用戶 id,對應用戶表(user) |
subject | varchar | 30 | Y | NULL | 科目 |
difficulty | varchar | 10 | Y | NULL | 困難度 |
create_time | varchar | 20 | Y | NULL | 創建時間 |
表 3-2-4 試卷表(test)
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
h_id | int | 11 | N | 作業 id,主鍵自增 | |
subject | varchar | 30 | Y | NULL | 科目 |
give_time | varcahr | 20 | Y | NULL | 作業布置時間 |
submit_time | varchar | 20 | Y | NULL | 提交時間 |
u_id | int | 11 | Y | NULL | 用戶 id,對應用戶表(user) |
consignee | varchar | 20 | Y | NULL | 接收人姓名 |
filename | varchar | 100 | Y | NULL | 作業存儲后的名字 |
表 3-2-5 作業表(homework)
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
a_id | int | 11 | N | 答案 id,主鍵自增 | |
u_id | int | 11 | Y | NULL | 用戶 id,對應用戶表(user) |
test_id | varcahr | 100 | Y | NULL | 試卷 id,對應試卷表(test) |
myanswer | varchar | 100 | Y | NULL | 用戶的答案 |
create_time | varchar | 200 | Y | NULL | 創建時間 |
表 3-2-6 用戶答案表(answer)
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
bbs_id | int | 11 | N | 留言 id,主鍵自增 | |
title | varchar | 100 | Y | NULL | 留言主題 |
subject | varcahr | 30 | Y | NULL | 科目 |
content | varchar | 2000 | Y | NULL | 內容 |
u_id | int | 11 | Y | NULL | 用戶 id,對應用戶表(user) |
create_time | varchar | 20 | Y | NULL | 創建時間 |
表 3-2-7 留言表(bbs)
字段名稱 | 數據類型 | 長度 | 允許為空 | 默認值 | 描述 |
reply_id | int | 11 | N | 回復 id,主鍵自增 | |
bbs_id | int | 11 | Y | NULL | 留言 id,對應留言表(bbs) |
u_id | int | 11 | Y | NULL | 用戶 id,對應用戶表(user) |
content | varchar | 500 | Y | NULL | 回復內容 |
create_time | varchar | 20 | Y | NULL | 回復時間 |
表 3-2-8 回復表(reply)
模塊流程圖
登錄與注冊
題目管理
師生互動
在線測試
我的上傳
收取作業
詳細設計
任務概述
基于 Java 和 MySQL 的精品課程網站設計與實現由兩位同學共同完成。程序的實現主要可分為數據庫設計、前端頁面設計、后臺模塊數據提取、前后臺數據交互四個部分。其中數據庫設計及前后臺數據交互由兩人共同討論完成,本人則主要負責后臺代碼塊的實現,從數據庫中提取所需數據。
開發環境
- 操作系統:Windows 7
- 開發平臺:MyEclipse10
- 數據庫:MySQL
- Java Web 服務器:apache-tomcat-6.0.29
- Java 語言的軟件開發工具包:jdk1.7.0_45
代碼模式結構
業務邏輯層(biz)
類名 | 描述 |
IBbsBiz | 留言業務邏輯接口類 |
IHomeWorkBiz | 作業業務邏輯接口類 |
IPostBiz | 公告業務邏輯接口類 |
ITestBiz | 試卷業務邏輯接口類 |
ITopicBiz | 試題業務邏輯接口類 |
IUpDownBiz | 上傳下載業務邏輯接口類 |
IUserBiz | 用戶業務邏輯接口類 |
impl | 包(用來存儲對應的業務邏輯實現類) |
- IBbsBiz(留言業務邏輯接口類):
創建新帖:
public boolean createBbs(String title, String subject, String content, int u_id) ;
查詢單條留言內容:
public Bbs selDanBbs(int bbs_id);
添加回復:
public boolean addReply( int u_id,int bbs_id,String content);
查詢多條留言:
public List<Bbs> selBbs(String sql);
查詢留言下所有回復:
public List<Reply> selReply(int bbs_id);
查詢留言個數:
public int countBbs(String sql);
- IHomeWorkBiz(作業業務邏輯接口類):
查詢作業列表:
public List<HomeWork> selHomeWork(String sql);
提交作業:
public boolean addHomeWork(String subject, String give_time, int u_id,String consignee, String filename);
提交作業人數:
public int countCollect(String sql);
未提交人列表:
public List<User> weiUsers(String sql);
- IPostBiz(公告業務邏輯接口類):
發布公告:
public boolean fbPost(String title, String content);
刪除公告:
public boolean delPost(int post_id);
查詢公告列表:
public List<Post> selPost(String sql);
查詢單個公告詳情:
public Post selPost(int post_id);
- ITestBiz(試卷業務邏輯接口類):
查詢試卷科目:
public List<Topic> selTopic(String sql);
生成試卷,存入試卷表:
public boolean createTest( String test_id,int u_id,List<Topic> topicLists);
將我的答案插入答案表:
public boolean addAnswer( String test_id,String myanswer,int u_id);
查找單個試卷對象:
public Test selDanTest(String sql);
- ITopicBiz(公告業務邏輯接口類):
添加題目:
public boolean addTopic(String subject, String title, String optiona,String optionb, String optionc, String optiond, String answer,String difficulty, int u_id);
刪除題目:
public boolean delTopic(int t_id);
修改題目:
public boolean updateTopic(int t_id, String subject, String title,String optiona, String optionb, String optionc, String optiond,String answer, String difficulty) ;
根據題目關鍵字,查詢題目:
public List<Topic> selTopic(String sql,String title) ;
查詢所有題目:
public List<Topic> selTopic(String sql,int u_id,int pageIndex,int pageSize);
查詢題目個數:
public int countTopic(String sql) ;
按查詢單個題目:
public Topic selOneTopic(String sql,int t_id);
- IUpDownBiz(上傳下載業務邏輯接口類):
上傳文件:
public boolean upFile(String subject, String title, String path,String ext, int u_id, String size) ;
查詢所有文件:
public List<Upload> selUpload(String sql) ;
刪除文件:
public boolean delUpload(int upload_id);
查詢文件個數:
public int countUpload(String sql);
- IUserBiz(用戶業務邏輯接口類):
用戶注冊:
public boolean register(String userName, String password, String name,String school);
根據帳號密碼查詢用戶,返回用戶(判斷登錄):
public User login(String sql);
添加教師用戶:
public boolean addTeacher(String username, String password, String name,String school, String subject);
根據查詢單個用戶:
public User selUser(String sql);
凍結用戶:
public boolean delUser(String userName, int d);
初始化密碼:
public boolean cshPassword(String userName);
查詢帳號是否已存在:
public boolean cxzh(String userName);
修改密碼:
public boolean updatepsw(String userName, String yuan,String xinpsw);
數據訪問層(dao)
類名 | 描述 |
IBbsDao | 留言數據訪問接口類 |
IHomeWorkDao | 作業數據訪問接口類 |
IPostDao | 公告數據訪問接口類 |
ITestDao | 試卷數據訪問接口類 |
ITopicDao | 試題數據訪問接口類 |
IUpDownDao | 上傳下載數據訪問接口類 |
IUserDao | 用戶數據訪問接口類 |
impl | 包(用來存儲對應的數據訪問實現類) |
數據實體層(model)
類名 | 描述 |
Answer | 答案實體類 |
Bbs | 留言實體類 |
HomeWork | 作業實體類 |
Post | 公告實體類 |
Reply | 回復實體類 |
Test | 試卷實體類 |
Topic | 試題實體類 |
Upload | 上傳文件實體類 |
User | 用戶實體類 |
控制層(servlet)
類名 | 描述 |
BbsServlet | 師生互動模塊邏輯控制 |
CollectWorkServlet | 收集作業模塊邏輯控制 |
HomeWorkServlet | 今日作業模塊邏輯控制 |
LoginServlet | 登錄注冊模塊邏輯控制 |
PostServlet | 公告管理模塊邏輯控制 |
TestServlet | 在線測試模塊邏輯控制 |
TopicServlet | 在線出題模塊邏輯控制 |
UploadServlet | 上傳下載模塊邏輯控制 |
UserServlet | 用戶管理模塊邏輯控制 |
過濾器(filter)
類名 | 描述 |
BackFilter | 后臺管理頁面過濾 |
LoginFilter | 精品課程網頁面過濾 |
工具包(util)
類名 | 描述 |
DBUtils | 數據庫操作工具類 |
DownFile | 下載文件方法類 |
獲得連接:
public static Connection getConn();
關閉鏈接釋放資源:
private static void close(ResultSet rs, PreparedStatement pst,Connection conn);
增刪改:
public static int update(String sql, Object... objs);
查詢結果為一條:
public static <T> T query(String sql,Class<T> t,Object...objs);
查詢一個字段的數據:
public static Object selOne(String sql,Object...objs);
查詢數量:
public static int count(String sql,Object...objs);
查詢結果為多條:
public static <T> List<T> queryList(String sql,Class<T>t,Object...objs);
resultset 轉換 MAP:
private static Map<String,Object> rsToMap(ResultSet rs);
把 Map 轉化為 bean 對象:
private static <T> T mapToBean(Class<T> t,Map<String,Object> map);
模塊功能的實現
登錄與注冊
- 用戶注冊:
String userName=request.getParameter("userName");
String school=request.getParameter("school");
String name=request.getParameter("name");
String password=request.getParameter("psd");
if(ub.register(userName, password, name, school)) {u.setUserName(userName);setPassword(password);setSchool(school);setName(name);setRole(3);session.setAttribute("user", u);//把名字存入sessionresponse.sendRedirect("/course/index.jsp");
}
- 用戶登錄:
int role=Integer.parseInt(request.getParameter("role"));
String userName=request.getParameter("userName");
String password=request.getParameter("password");
if(role==1) { //管理員登錄String sql="select * from user where userName='"+userName+"' and password='"+password+"' and role=1";=ub.login(sql);//查找用戶,返回用戶信息if(u!=null) { //存在此用戶session.setAttribute("user", u);//用戶信息存于session}response.sendRedirect("/course/backindex.jsp");
} else { //不是管理員String sql="select * from user where userName='"+userName+"' and password='"+password+"' and role!=1 and del=1";=ub.login(sql);//查找用戶,返回用戶信息if(u!=null) { //存在此用戶session.setAttribute("user", u);//用戶信息存于session}response.sendRedirect("/course/index.jsp");
}
- 檢測帳號:
if("zh".equals(p)) { //檢測帳號是否可用String userName=request.getParameter("userName");if(ub.cxzh(userName)) { //帳號可用out.print(1);} else {out.print(0);}out.flush();out.close();
} else if("cslogin".equals(p)) { //超時后登錄String userName=request.getParameter("userName");String password=request.getParameter("password");String sql="select * from user where userName='"+userName+"' and password ='"+password+"'";u=ub.login(sql);if(u!=null) {session.setAttribute("user", u);//用戶信息存于session}out.flush();out.close();
}
在線出題
- 添加與修改題目:
if ("add".equals(type)||"update".equals(type)) {String subject = request.getParameter("subject");String title = request.getParameter("title");String optiona = request.getParameter("optiona");String optionb = request.getParameter("optionb");String optionc = request.getParameter("optionc");String optiond = request.getParameter("optiond");String answer = request.getParameter("answer");String difficulty = request.getParameter("difficulty");if("add".equals(type)) {if (tb.addTopic(subject, title, optiona, optionb, optionc, optiond,answer, difficulty, u_id)) {// 添加成功out.print(1);} else {out.print(0);}} else {int t_id=Integer.parseInt(request.getParameter("id"));System.out.println(t_id);if (tb.updateTopic(t_id, subject, title, optiona, optionb, optionc, optiond, answer, difficulty)) {// 修改成功out.print(1);} else {out.print(0);}}out.flush();out.close();
}
- 按條件搜索與刪除題目:
if("search".equals(type)) {int pageIndex=Integer.parseInt(request.getParameter("index"));int pageSize=10;//一頁5條String searchTitle = request.getParameter("searchTitle");String title = new String(searchTitle.getBytes("iso8859-1"), "utf-8");String sql="select * from topic where title like ?";List<Topic> topicLists=tb.selTopic(sql,"%"+title+"%");sql="select count(1) from topic where title like '%"+title+"%'";int length=tb.countTopic(sql);//總共多少條int count=length%pageSize>0?(length/pageSize+1):length/pageSize;request.setAttribute("topicLists", topicLists);request.setAttribute("count", coun);request.setAttribute("pageIndex", pageIndex);request.getRequestDispatcher("/jsp/topicmanager.jsp?type=search&title ="+searchTitle).forward(request, response);
} else if("clickUpdate".equals(type)) { //點擊修改int t_id=Integer.parseInt(request.getParameter("id"));String sql="select * from topic where t_id=?";Topic topic=tb.selOneTopic(sql,t_id);request.setAttribute("topic",topic);request.getRequestDispatcher("/jsp/addtopic.jsp?type=update").forward (request, response);
} else if("lie".equals(type)) {int pageIndex=Integer.parseInt(request.getParameter("index"));String sql="select * from topic where u_id=? limit ?,?";int pageSize=10;//一頁5條List<Topic> topicLists=tb.selTopic(sql,u_id,(pageIndex-1)*pageSize,pageSize);System.out.println(topicLists);sql="select count(1) from topic where u_id="+u_id;int length=tb.countTopic(sql);//總共多少條int count=length%pageSize>0?(length/pageSize+1):length/pageSize;request.setAttribute("topicLists", topicLists);request.setAttribute("count", count);request.setAttribute("pageIndex", pageIndex);request.getRequestDispatcher("/jsp/topicmanager.jsp?type=lie").forward(request, response);
} else if("del".equals(type)) {int t_id=Integer.parseInt(request.getParameter("id"));if(tb.delTopic(t_id)) {out.print(1);} else {out.print(0);}out.flush();out.close();
}
}
在線測試
- 隨機產生 10 道題目生產試卷:
String subject = new String(request.getParameter("subject").getBytes("iso8859-1"), "utf-8");
int nd=Integer.parseInt(request.getParameter("nd"));
Map<Integer,String> map=new HashMap<Integer, String>();
map.put(1, "簡單");
map.put(2, "一般");
map.put(3, "困難");
Map<Integer,String> map2=new HashMap<Integer, String>();
map2.put(1, "基礎題");
map2.put(2, "練習題");
map2.put(3, "競賽題");
request.setAttribute("testTop", subject+map2.get(nd));// 試卷標題
sql="select * from topic where subject ='"+subject+"' and difficulty='"+map.get(nd)+"'";
List<Topic> allTopics=tb.selTopic(sql);//查詢所有符合的題目
System.out.println(allTopics);
List<Topic> topicLists=new ArrayList<Topic>();
//隨機產生題目
int sj=allTopics.size();
if(sj>10) { //如果題目足夠int index=0;boolean flag=true;do {index=(int)(Math.random()*sj-1);//隨機產生一個數Topic t=allTopics.get(index);//拿到相應的對象if(topicLists.size()<1) { //才產生一個隨機對象topicLists.add(t);//把隨機產生的這個對象放入集合中} else {for(int i=0; i<topicLists.size(); i++) { //循環所有題目int stid=topicLists.get(i).getT_id();if(t.getT_id()==stid) { //已存在此題flag=false;break;} else {flag=true;}}if(flag) {topicLists.add(t);//把隨機產生的這個對象放入集合中}}} while(topicLists.size()!=10);//循環收取10個題目//把該試卷存入試卷表String test_id = UUID.randomUUID().toString();//生成唯一idtb.createTest(test_id, u_id,topicLists);request.setAttribute("test_id", test_id);
}
request.setAttribute("topicLists", topicLists);
request.getRequestDispatcher("/jsp/test.jsp").forward(request, response);
}
- 批改試卷(學生答案與正確答案相比較):
String myanswer=request.getParameter("myanswer");
String test_id=request.getParameter("test_id");
tb.addAnswer(test_id, myanswer, u_id);//將自己的答案存入答案表
sql="select * from test where test_id='"+test_id+"'";
Test test=tb.selDanTest(sql);
String myAnswerList[]=myanswer.split(",");
String rightAnswerList[]=test.getAllanswer().split(",");
//裝錯誤了的題的答案,正確的賦值1
List<String> worryList=new ArrayList<String>();
for(int i=0; i<rightAnswerList.length; i++) {System.out.println(myAnswerList[i]+"----"+rightAnswerList[i]);if(myAnswerList[i].equals(rightAnswerList[i])) { //答案正確worryList.add("1");} else {worryList.add(rightAnswerList[i]);}
}
String json = JSONArray.fromObject(worryList).toString();
System.out.println(json);
out.write(json);
out.flush();
out.close();
}
資料下載
- 文件上傳關鍵代碼:
try {// 創建文件項工廠DiskFileItemFactory factory = new DiskFileItemFactory();// 創建解析請求數據的ServletFileUpload對象ServletFileUpload upload = new ServletFileUpload(factory);// 解析request請求把流裝入到list中List<FileItem> list = upload.parseRequest(request);HashMap<String, String> map = new HashMap<String, String>();for (int i = 0; i < list.size(); i++) {// 把各個流裝入到item中FileItem item = list.get(i);if (item.isFormField()) {// 判斷當前fileitem是否是表單中的字段String key = item.getFieldName();map.put(key,new String(item.getString().getBytes("iso-8859-1"),"utf-8"));} else {// 當fileitem是文件流時// 文件后綴String filename = item.getName();ext =filename.substring(filename.lastIndexOf(".")+1);String newname= map.get("title")+"."+ext;String newpath = request.getRealPath("/upload") + "/" +newname;size=convertFileSize(item.getSize());item.write(new File(newpath));path = request.getContextPath()+"/upload/"+newname;}}//存儲信息if(ub.upFile(map.get("subject"),map.get("title"),path,ext,u_id,size)) {request.getRequestDispatcher("/jsp/success.jsp").forward(request, response);}
} catch (Exception e) {printStackTrace();
}
}
- 文件大小格式換算:
public static String convertFileSize(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {float f = (float) size / mb;return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {float f = (float) size / kb;return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} elsereturn String.format("%d B", size);
}
師生互動
- 按條件查詢討論留言:
if("search".equals(type)) { //查詢String countsql=null;int pageIndex=Integer.parseInt(request.getParameter("index"));//頁面傳來的頁碼int pageSize=5;//一頁5條String p=request.getParameter("p");if("title".equals(p)) { //按主題查詢String title = new String(request.getParameter("title").getBytes("iso8859-1"), "utf-8");sql="select b.*,u.name from bbs b,user u where title like '%"+title+"%' and u.u_id=b.u_id order by b.create_time desc limit "+(pageIndex-1)*pageSize+","+pageSize;countsql="select count(1) from bbs where title like '%"+title+"%' " ;request.setAttribute("title", title);} else if("subject".equals(p)) { //按科目查詢String subject = new String(request.getParameter("subject").getBytes("iso8859-1"), "utf-8");sql="select b.*,u.name from bbs b,user u where b.subject like '%"+subject+"%' and u.u_id=b.u_id order by b.create_time desc limit "+(pageIndex-1)*pageSize+","+pageSize;countsql="select count(1) from bbs where subject like '%"+subject+"%' " ;request.setAttribute("subject", subject);} else if("all".equals(p)) { //查詢所有sql="select b.*,u.name from bbs b,user u where b.u_id=u.u_id order by b.create_time desc limit "+(pageIndex-1)*pageSize+","+pageSize;countsql="select count(1) from bbs " ;}List<Bbs> bbsLists=bbiz.selBbs(sql);int length=bbiz.countBbs(countsql);//總條數request.setAttribute("bbsLists", bbsLists);int count=length%pageSize>0?(length/pageSize+1):length/pageSize;request.setAttribute("count", count);request.setAttribute("pageIndex", pageIndex);request.getRequestDispatcher("/jsp/bbs.jsp?p="+p).forward(request, response);
} else if("addbbs".equals(type)) {String subject =request.getParameter("subject");String title = request.getParameter("title");String content = request.getParameter("content");if(bbiz.createBbs(title, subject, content, u_id)) {out.print(1);}out.flush();out.close();
}
- 查詢單個留言頁面,添加留言:
if("dan".equals(type)) { //點擊進入單個頁面int bbs_id=Integer.parseInt(request.getParameter("id"));sql="select b.*,u.name from bbs b,user u where b.u_id=u.u_id and bbs_id="+bbs_id;Bbs bbs=bbiz.selDanBbs(bbs_id);request.setAttribute("bbs", bbs);List<Reply> replyLists=bbiz.selReply(bbs_id);request.setAttribute("replyLists", replyLists);request.getRequestDispatcher("/jsp/bbsdan.jsp").forward(request, response);
} else if("addreply".equals(type)) {String content = request.getParameter("content");int bbs_id=Integer.parseInt(request.getParameter("id"));if(bbiz.addReply(u_id, bbs_id, content)) {out.print(1);}out.flush();out.close();
}
}
我的作業
- 查詢該用戶所提交的作業:
if("all".equals(type)) {sql="select * from homework where u_id="+u_id+" order by submit_time desc";
} else if("search".equals(type)) {String subject = new String(request.getParameter("subject").getBytes("iso8859-1"), "utf-8");String start_time=request.getParameter("start_time");String end_time=request.getParameter("end_time");sql="select * from homework where u_id="+u_id+" and subject ='"+subject+"' and give_time between '"+start_time+"' and '"+end_time+"' order by give_time desc ";
}
List<HomeWork> homeWorkList=hb.selHomeWork(sql);
request.setAttribute("homeWorkList", homeWorkList);
request.getRequestDispatcher("/jsp/tijiaohomework.jsp").forward(request, response);
}
收取作業
- 批量下載學生作業:
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");String subject=request.getParameter("subject");String give_time=request.getParameter("give_time");byte[] buffer = new byte[1024];String path=this.getServletConfig().getServletContext().getRealPath("/");// 生成的ZIP文件名String strZipName = "【"+subject+"】"+give_time+".zip";System.out.println(strZipName);ZipOutputStream out = new ZipOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\"+strZipName));//解決中文亂碼問題String filenames=request.getParameter("filenames");System.out.println("filenames-------------"+filenames);String filename[]=filenames.split(",");// 需要同時下載的幾個文件File[] file1 = new File[filename.length];for(int j=0; j<filename.length; j++) {file1[j]=new File(path+"//upload//"+filename[j]);}for (int i = 0; i < file1.length; i++){FileInputStream fis = new FileInputStream(file1[i]);out.putNextEntry(new ZipEntry(file1[i].getName()));int len;// 讀入需要下載的文件的內容,打包到zip文件while ((len = fis.read(buffer)) > 0){out.write(buffer, 0, len);}out.closeEntry();fis.close();}out.flush();out.close();System.out.println("生成.zip成功");
}
用戶管理
- 查詢用戶:
if("search".equals(type)) {String userName=request.getParameter("userName");System.out.println(userName);String sql="select * from user where userName='"+userName+"'";User seluser=ub.selUser(sql);request.setAttribute("seluser", seluser);request.getRequestDispatcher("/backstage/usermanager.jsp").forward(r equest, response);
}
}
- 凍結與初始化密碼:
if("del".equals(type)||"csh".equals(type)) {String userName=request.getParameter("userName");if("del".equals(type)) {int d=Integer.parseInt(request.getParameter("d"));//解凍:1 凍結 0if(ub.delUser(userName,d))out.print(1);} else if("csh".equals(type)) {if(ub.cshPassword(userName))out.print(1);}out.flush();out.close();
}
- 增加教師用戶:
if("add".equals(type)) {String userName=request.getParameter("userName");String name=request.getParameter("name");String school=request.getParameter("school");String[] subjects=request.getParameterValues("subject");String subject="";for(int i=0; i<subjects.length; i++) {subject=subject+subjects[i]+",";}ub.addTeacher(userName, "000000", name, school, subject);request.getRequestDispatcher("/backstage/success.jsp").forward(request, response);
} else if("updatepsw".equals(type)) {String yuanpsw=request.getParameter("yuanpsw");String xinpsw=request.getParameter("xinpsw");User user=(User) session.getAttribute("user");String userName=user.getUserName();if(ub.updatepsw(userName, yuanpsw, xinpsw)) {out.print(1);} else {out.print(0);}
}
}
公告管理
- 展示公告列表:
if ("lie".equals(b)) {// 獲取公告列表String sql = "select * from post order by create_time desc";List<Post> plist = postBiz.selPost(sql);request.setAttribute("plist", plist);request.getRequestDispatcher("/backstage/post.jsp").forward(request, response);
} else if("dan".equals(b)) {int post_id=Integer.parseInt(request.getParameter("id"));Post post=postBiz.selPost(post_id);request.setAttribute("post", post);request.getRequestDispatcher("/jsp/postdetail.jsp").forward(request, response);
}
}
- 發布刪除公告:
if ("fb".equals(type)) {// 發布公告String title = request.getParameter("title");String content = request.getParameter("content");System.out.println(title + "-----" + content);System.out.println(postBiz.fbPost(title, content));
} else if ("all".equals(type)) {String sql = "select * from post order by create_time desc limit 0,5";List<Post> plist = postBiz.selPost(sql);String json = null;if (plist != null) {json = JSONArray.fromObject(plist).toString();}out.write(json);
} else if ("del".equals(type)) {// 刪除公告int post_id = Integer.parseInt(request.getParameter("id"));if(postBiz.delPost(post_id)) {out.print(1);} else {out.print(0);}
}
用戶使用說明
本程序 Java Web 程序,在 MySQL 中建立相應表插入適當數據后,更改數據庫配置文件 database.properties,將程序源代碼導入相應的開發工具或將編譯后的程序直接放在 Tomcat 下,運行即可。
- 精品課程網首頁:localhost:8080/course/index.jsp
- 測試登錄學生用戶: ? 帳號:student ? ?密碼:123456
- 測試登錄教師用戶: ? 帳號:teacher ? ?密碼:123456
- 圖 5-1 精品課程網首頁
- 學生用戶權限:首頁、課程中心、資料下載、師生互動、在線測試、提交作業(如圖 5-2)
- 教師用戶權限:首頁、課程中心、資料下載、師生互動、在線出題、收集作業(如圖 5-3)
圖 5-2 精品課程網在線測試模塊
圖 5-3 精品課程網在線出題模塊
精品課程網后臺管理:localhost:8080/course/backindex.jsp
- 測試登錄管理員用戶: ? 帳號:admin ? ?密碼:admin
- 圖 5-4 精品課程網后臺管理系統登陸頁面
- 管理員用戶權限:用戶管理、公告管理(如圖 5-5)
圖 5-5 精品課程網后臺管理系統用戶管理模塊
三、結論
基于 Java 和 MySQL 的精品課程網站設計與實現由我和同組同學合作完成。它是根據當下教育資源緊缺和廣大教育需求而分析設計的,初步確定了網站系統具有的解決當下教務模式與需求之間的矛盾的功能,包括數據庫設計、網頁界面設計、Web 應用層設計等,是一個具有實際應用意義和使用價值的資源共享網站。
在此次設計中,我主要負責的是后臺代碼的編寫。從一起分析討論到各自完成所負責部分的代碼再到前后臺的數據交互,期間反反復復遇到了很多問題,不過通過在網上查閱資料和相互交流,最終都得以解決。
這次的程序編寫使我對網站設計的開發步驟和思路有了一個全新的認識,也更加熟練的掌握了 Java 基礎代碼,我體會到了代碼規范化的重要性:寫代碼之前要設計好文案,操作步驟、流程圖、數據結構等都要想好,命名規范,盡量多的思考需要什么,將整個流程都構思好后,與大家一起討論。編寫時思路要清晰,盡量避免重復代碼,多加注釋,等等。
由于畢業設計的時間有限,該網站在功能需求和用戶體驗方面仍不完善,還有待進一步改進和提高。
四、參考文獻
- 蔡劍,景楠.JavaWeb 應用開發:J2EE 和 Tomcat[M].北京:清華大學出版社,2004
- 孫鑫編著.JavaWeb 開發詳解[M].電子工業出版社,2006
- 張志峰,朱顥東.Java Web 技術整合應用與項目實戰.清華大學出版社,2013
- 王飛飛,崔洋.MySQL 數據庫應用.中國鐵道出版社,2007
- 鮑格斯坦.JSP 設計第一版.中國電力出版社,2008
- 孫更新.Java 畢業設計指南與項目實踐.北京科海電子出版社,2008
五、致 ? 謝
本次畢業設計能夠順利完成離不開我的導師的指點和幫助。從選題、程序分析、程序設計、程序調試到最終論文的撰寫,其中遇到了不少問題,但在老師的悉心教導下都得以解決,在此由衷的感謝導師對我的幫助及支持。
本次設計也是我和同學第一次搭檔的作品,以前在班上交流不算多,這次的合作使我們更加熟悉加深了友誼,這也再次讓我體會到齊心協力的重要性,我們一起相互討論,研究出相對合適的解決方法,雖然設計還有很多不足,但是我們還會慢慢完善做到更好!