




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、/需求分析:寫一個較為嚴謹?shù)膶W生選課系統(tǒng),實現(xiàn)學生可以選擇多門選課課程,并將有效選課結(jié)果保存到數(shù)據(jù)庫。學生需要登錄后,才能選課。讓學生可以在選課系統(tǒng)通過多種方式查詢到要選的課程信息。/選課規(guī)則:1、每個學生可以選多門課程,多次選課的總學分不能超過6學分;2、不能重復選擇一門課程;3、每一門課程的選課人數(shù)都有數(shù)量限制,當某門課程的選課名額滿時,則應另選課程。4、憑用戶名和密碼登錄,通過提交某一課程號來選課/總體設(shè)計方案:建立三個類:登錄類register,選課類studentChooseCourse,數(shù)據(jù)庫工具類JDBCUtil;一個SQL腳本文件用于生成數(shù)據(jù)庫表結(jié)構(gòu)和初始記錄,以搭建數(shù)據(jù)庫環(huán)境
2、。登錄類register類,負責對用戶的身份進行驗證;工具類JDBCUtil用于實現(xiàn)連接,你可調(diào)用JDBCUtil的getConnection()方法等到鏈接。選課類studentChooseCourse用于,實現(xiàn)選課功能。其中包括幾個主要方法:1、actionPerformed(ActionEvent) 用于監(jiān)聽用戶“查詢”和“提交”操作,并負責調(diào)用各種方法對其進行處理2、createSearchCourse()用于產(chǎn)生圖形用戶界面3、processBeforeCommit()用于對用戶的“提交”查找進行驗證,剔除無效的用戶操作4、tryCommit()負責對有效的“提交”操作,進一步處理,
3、并將有效的操作結(jié)果時時保存到數(shù)據(jù)庫,并更新數(shù)據(jù)庫原有信息/本程序用到的知識點:數(shù)據(jù)庫連接JDBC;SQL建表、插入輸入、動態(tài)查詢;圖形用戶界面的產(chǎn)生以及處理查詢結(jié)果集并較好顯示;程序設(shè)計基礎(chǔ)知識。/代碼如下:import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.*;import javax.swing.*;import javax.swing.table.DefaultTableModel;import school.sc
4、hoolPicture.JdbcUtil;public class StudentChooseCourse extends JFrame implements ActionListener JTextField jtfSearch = new JTextField(11);String columnNames = new String 課程ID, 課程名, 學時, 學分, 學期, 性質(zhì) ;DefaultTableModel dtmSearch = new DefaultTableModel(columnNames, 27);JTable jtSearch = new JTable(dtmSea
5、rch);JScrollPane jspSearch = new JScrollPane(jtSearch);JComboBox jcbSearch = new JComboBox();JTextField jtfSelectedCourse = new JTextField(10);Connection con = null;PreparedStatement ps = null;ResultSet rs = null;private static String usrName;private static String passwrd;/在構(gòu)造方法中添加兩個參數(shù)。以便在提交時,將學生的身份
6、連同所選的課程,一同記錄在學生選課表中。public StudentChooseCourse(String usrName, String passwrd) createSearchCourse();this.usrName = usrName;this.passwrd = passwrd;public String getUsrName() return usrName;public void setUsrName(String usrName) this.usrName = usrName;public String getPasswrd() return passwrd;public v
7、oid setPasswrd(String passwrd) this.passwrd = passwrd;/根據(jù)用戶的時間,做出相應的反映public void actionPerformed(ActionEvent e) String str = e.getActionCommand();/清空結(jié)果顯示區(qū)中的內(nèi)容,如果有的話。if (查詢.trim().equals(str) int k = 1;while (k 10) for (int i = 1; i 7; i+) jtSearch.setValueAt(null, k - 1, i - 1);k+;/調(diào)用下面的這個方法,在數(shù)據(jù)庫中進
8、行查找,并將結(jié)果顯示在表格中。searchAvailableCourse(); else if (提交.equals(str) /processBeforeCommit()對用戶選課操作進行有效性檢驗;/剔除無效操作:如輸入無效的課程號,或已經(jīng)選擇了某一課程,已經(jīng)選滿的6學分等各種情況boolean effect=processBeforeCommit(); /如果課程存在,且該學生具有選擇該課程的資格,即effect為true,進入正式提交程序(tryCommit()if(effect=true) tryCommit();/對用戶選課操作進行有效性檢驗;public boolean proc
9、essBeforeCommit()/清空原結(jié)果顯示區(qū)中的內(nèi)容,如果有的話。int k = 1;while (k 10) for (int i = 1; i 6) JOptionPane.showMessageDialog(null, 你已經(jīng)選滿6學分,系統(tǒng)將退出);this.setVisible(false);return false;/無效操作3:課程該學生已經(jīng)選擇了某課程,則不能再選該課程了。sql = select * from choicesxx where sname=? and cno=?;boolean flag = false;try ps = con.prepareState
10、ment(sql);ps.setString(1, this.getUsrName();ps.setString(2, userInput);rs = ps.executeQuery();flag = rs.next(); catch (Exception eaa) eaa.printStackTrace();if (flag) JOptionPane.showMessageDialog(null, 你已經(jīng)選擇了該課程。請另選課程);return false;/如果以上無效操作都不存在,則返回true,意為這是一個準有效操作return true;/對有效的提交操作的進行處理public vo
11、id tryCommit() / userInput為用戶輸入的課程ID.String userInput = jtfSelectedCourse.getText().toString().trim().toLowerCase();/ if course still available(count 0) / save studentId and courseId to student-course table./ this.getUsrName();userInputsql = insert into choicesxx values(?,?);ps = con.prepareStatemen
12、t(sql);ps.setString(1, this.getUsrName();ps.setString(2, userInput);ps.executeUpdate();JOptionPane.showMessageDialog(null, 選課成功: + this.getUsrName()+ 選了 + userInput + . + + 還有 + RemainedCount+ 人可以選該課程。);/ 更新課程中已選該課程的人數(shù):即將可選該課程的人數(shù)減去1個人。sql = update CourseXX set selectedCount=selectedCount+1 where cno
13、=?;ps = con.prepareStatement(sql);ps.setString(1, userInput);ps.executeUpdate();mit();/如果該課程已經(jīng)沒有選擇名額,提示重新選課 catch (Exception es) es.printStackTrace();try con.rollback(); catch (Exception ey) ey.printStackTrace();/對用戶查詢課程信息,進行處理,并顯示查詢結(jié)果public void searchAvailableCourse() / 讓程序自動選擇連接的是Oracle或Sq
14、lServer.if (JDBCUtil.getConnection() != null) System.out.println(JDBCUtil.getConnection();con = JDBCUtil.getConnection(); else con = JdbcUtil.getConnection();/userInput取得用戶輸入的信息,selectedItem取得用戶選擇的查詢方式String userInput = jtfSearch.getText().toString().trim().toLowerCase();String selectedItem = jcbSea
15、rch.getSelectedItem().toString().trim();System.out.println(User search: + userInput);System.out.println(selectedItem: + selectedItem);String sql = null;/按用戶查詢方式,如按課程名,課程ID或?qū)W時的查詢進行處理;并在表格中實現(xiàn)結(jié)果try if (課程名.equals(selectedItem) sql = select cno,cname,hour,grade,term,isNeed from CourseXX where cname = ?;
16、ps = con.prepareStatement(sql);ps.setString(1, userInput); else if (課程ID.equals(selectedItem) sql = select cno,cname,hour,grade,term,isNeed from CourseXX where cno = ?;ps = con.prepareStatement(sql);ps.setString(1, userInput); else if (學時.equals(selectedItem) sql = select cno,cname,hour,grade,term,i
17、sNeed from CourseXX where hour = ?;ps = con.prepareStatement(sql);ps.setInt(1, Integer.parseInt(userInput); else if (學分.equals(selectedItem) sql = select cno,cname,hour,grade,term,isNeed from CourseXX where grade = ?;ps = con.prepareStatement(sql);ps.setInt(1, Integer.parseInt(userInput); else if (學
18、期.equals(selectedItem) sql = select cno,cname,hour,grade,term,isNeed from CourseXX where term = ?;ps = con.prepareStatement(sql);ps.setString(1, userInput);System.out.println(sql);rs = ps.executeQuery();mit();ResultSetMetaData meta = rs.getMetaData();int cols = meta.getColumnCount();String re
19、sult = null;int k = 1;boolean flag = false;/將查詢結(jié)果以表格的形式顯示出來while (rs.next() for (int i = 1; i = cols; i+) result = rs.getString(i);System.out.println(result);jtSearch.setValueAt(result, k - 1, i - 1);k+;flag = true;/如果查詢結(jié)果集為空,提示用戶沒有該課程if (flag = false) JOptionPane.showMessageDialog(null, 該課程不存在,請重新輸
20、入);return; catch (Exception ex) ex.printStackTrace();try con.rollback(); catch (Exception er) er.printStackTrace();/產(chǎn)生圖形用戶界面,以便用戶操作public void createSearchCourse() this.setLayout(new GridLayout(3, 1);JPanel jp1 = new JPanel();jp1.setLayout(new GridLayout(4, 1);JPanel jp2 = new JPanel();JPanel jp3 =
21、new JPanel();JPanel jp10 = new JPanel();JPanel jp11 = new JPanel();JPanel jp12 = new JPanel();JPanel jp13 = new JPanel();JLabel jlSearch = new JLabel( 學 生 選 課 系 統(tǒng) );jp11.add(jlSearch);jcbSearch.addItem(new String(課程名);jcbSearch.addItem(new String(課程ID);jcbSearch.addItem(new String(學時);jcbSearch.addI
22、tem(new String(學分);jcbSearch.addItem(new String(學期);jp12.add(jtfSearch);jp12.add(jcbSearch);JButton jbOK = new JButton(查詢);jbOK.addActionListener(this);jbOK.setSize(90, 20);jp13.add(jbOK);jp1.add(jp10);jp1.add(jp11);jp1.add(jp12);jp1.add(jp13);jp2.add(jspSearch);JLabel jlSelectedCourse = new JLabel(
23、請輸入課程ID:);JButton jbSelectedCourse = new JButton(提交);jbSelectedCourse.addActionListener(this);jp3.add(jlSelectedCourse);jp3.add(jtfSelectedCourse);jp3.add(jbSelectedCourse);this.add(jp1);this.add(jp2);this.add(jp3);this.setVisible(true);this.setSize(485, 600);/當某學生有效登錄后,啟動程序(將學生的登錄信息也傳過來,以便保存選課操作時使用
24、)public static void main(String args) /String usrName = xuliang;/String passwrd = 123;new StudentChooseCourse(usrName, passwrd);/-以下為搭建學生選課系統(tǒng)的SQL腳本文件。只需運行一次,就可有完整的數(shù)據(jù)庫表結(jié)構(gòu)和初始記錄。 /-/該腳本針對SQLserver;Oracle的SQL腳本類似,只需將將數(shù)據(jù)類型如varchar改為varchar2,int改為number等-table1: registerXU 登錄表drop table registerXu;create
25、table registerXu (id varchar(20),userName varchar(20),passWord varchar(20),identify varchar(20);insert into registerXu values(s001,xuliang,123,學生);insert into registerXu values(s002,xuliang2,1234,學生);insert into registerXu values(j001,jack,12345,學生);insert into registerXu values(001,user,user,學生);in
26、sert into registerXu values(z001,zlm,corejava,老師);-String sql = select * from registerXu -+ where userName=? and passWord=? and identify=?;-table 2:Coursexx課程表drop table Coursexx;create table Coursexx(cno varchar(20) primary key,cname varchar(20),hour Int,grade Int,term varchar(20),isNeed varchar(20
27、), selectedCount Int ,Max Int);insert into Coursexx values(c001,CoreJava,50,5,NoNecessary,0,50);insert into Coursexx values(c002,XML,20,2,NoNecessary,0,40);insert into Coursexx values(c003,HIBERNATE,20,4,NoNecessary,0,30);insert into Coursexx values(c004,SQL,20,4,NoNecessary,0,5);insert into Coursex
28、x values(c005,JDBC,20,2,NoNecessary,0,3);insert into Coursexx values(c006,AJAX,20,2,NoNecessary,0,1);insert into Coursexx values(c007,JSP,100,8,NoNecessary,0,1-sql = select cno,cname,hour,grade,term,isNeed from CourseXX where cname = ?; -table 3:學生表-drop table studentxx;create table studentxx(sid va
29、rchar(20),sname varchar(20) primary key,sex varchar(20),birthday varchar(20),className varchar(20), image varchar(20);insert into studentxx values(s001,xuliang,male,sd1003,good);insert into studentxx values(s002,xuliang2,male,sd0910,good);insert into studentxx values(j001,jack,male,sd1003,good);inse
30、rt into studentxx values(001,user,male,sd1005,good);insert into studentxx values(s003,sisi,female,sd1007,good);insert into studentxx values(as003,crystal,female,asd1007,good);-table 4:choices學生選課表drop table choicesxx;create table choicesxx(sname varchar(20) references studentxx(sname),cno varchar(20
31、) references coursexx(cno);-判斷某一個學生已經(jīng)選的課程的總學分是否小于6分/-用戶登錄類 -/用途:驗證用戶是否具有登錄系統(tǒng)的資格-package school;import javax.swing.*;import school.schoolPicture.JdbcUtil;import java.sql.*;import java.awt.*;import java.awt.event.*;public class Register implements ActionListener JFrame jf = new JFrame(學生成績管理與選課系統(tǒng));JTe
32、xtField jtfUserName = new JTextField(10);JPasswordField jpfPassWord = new JPasswordField(10);JComboBox identify = new JComboBox();/constructorpublic Register() CreateRegisterGUI();/ deal with user action, when user check:登錄,取消or 注冊public void actionPerformed(ActionEvent e) String str = e.getActionCo
33、mmand();if (str.equalsIgnoreCase(登錄) /當用戶點擊登錄時,調(diào)用以下方法去數(shù)據(jù)庫做匹配processLogin(); else if (str.equalsIgnoreCase(取消) jtfUserName.setText();jpfPassWord.setText(); else if (str.equalsIgnoreCase(注冊) new CreateLogin();/當用戶點擊登錄時,調(diào)用以下方法去數(shù)據(jù)庫做匹配public void processLogin() / create connection to the database.Connect
34、ion con = null;/ Connection con = JDBCUtil.getConnection();/讓程序自動連接相應的數(shù)據(jù)庫,以避免連接數(shù)據(jù)庫時頻繁改動連接程序if (JdbcUtil.class = null) /連接達內(nèi)Oracle數(shù)據(jù)庫con = JdbcUtil.getConnection(); else /連接本地SQLSERVER數(shù)據(jù)庫con = JDBCUtil.getConnection();/ write sql sentenceString usrName = jtfUserName.getText().trim();String passwrd =
35、new String(jpfPassWord.getPassword().trim();String ident = identify.getSelectedItem().toString().trim();String sql = select * from registerXu + where userName=? and passWord=? and identify=?;System.out.println(usrName + : + passwrd + : + ident);/ create object of PreparedStatementtry PreparedStateme
36、nt ps = con.prepareStatement(sql);/ Prepare parameter for the sqlps.setString(1, usrName);ps.setString(2, passwrd);ps.setString(3, ident);/ send parameter to compiler to compile.ResultSet rs = ps.executeQuery();StringBuffer sb = new StringBuffer();ResultSetMetaData meta = rs.getMetaData();int cols =
37、 meta.getColumnCount();/you can use another simple way to check whether the people has records in database:/define a boolean flag=false, if has record change it/to true;otherwise, if flag=flase,showMessage(Input ERROR)while (rs.next() for (int i = 1; i cols; i+) sb.append(meta.getColumnName(i);sb.ap
38、pend(rs.getString(i); if (sb.length() = 1) if (ident.equals(student) /if he or she is a student, and usrName-passwrd alright, then go to 學生選課系統(tǒng)new StudentChooseCourse(usrName, passwrd);jf.setVisible(false);/ new StudentEntered(); else if (ident.equals(teacher) / new TeacherEntered(usrName,passwrd);n
39、ew TeacherEntered();jf.setVisible(false); else if (ident.equals(admin) / go to administrator pages. catch (Exception er) er.printStackTrace();/產(chǎn)生圖形用戶界面public void CreateRegisterGUI() jf.setLayout(new GridLayout(5, 1);JPanel jp1 = new JPanel();JLabel jl1 = new JLabel(學生成績管理系統(tǒng));jp1.add(jl1);jf.add(jp1
40、);JPanel jp2 = new JPanel();JLabel jl2 = new JLabel(用戶名:);jp2.add(jl2);jp2.add(jtfUserName);jf.add(jp2);JPanel jp3 = new JPanel();/ JPasswordField jpfPassWord = new JPasswordField(10);JLabel passWord = new JLabel(密 碼:);jp3.add(passWord);jp3.add(jpfPassWord);jf.add(jp3);JPanel jp4 = new JPanel();JLabel jl4 = new JLabel(身 份:);/ identify.addItem(new String(學生 );identify.addItem(new String(student );/ identify.addItem(new String(老師 );identify.addItem(new String(teacher );/ identify.addItem(new
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 行政效能評估標準的試題及答案
- 醫(yī)學影像診斷技巧試題及答案總結(jié)
- 人員流失風險分析與應對策略
- 深入分析語文試題及答案2025年版
- 經(jīng)濟法與當代時代試題及答案
- 水平提升技巧盤點執(zhí)業(yè)醫(yī)師考試試題及答案
- 2025年自考行政管理個性化學習試題及答案探討
- 細化2025年行政管理??普Z文考試考點試題及答案
- 新疆哈密地區(qū)第二中高二下學期期末考試歷史試題
- 中東呼吸綜合征病例診療方案課件
- 甲狀腺手術(shù)甲狀旁腺保護
- 2023年法律職業(yè)資格《主觀題》真題及答案
- 24節(jié)氣-中國人的時間美學智慧樹知到期末考試答案章節(jié)答案2024年東北農(nóng)業(yè)大學
- 人教部編版七(下)語文《愛蓮說》練習
- 關(guān)于菜鳥驛站轉(zhuǎn)讓合同范本
- DL-T1342-2014電氣接地工程用材料及連接件
- 學校體育工作條例
- 施工現(xiàn)場視頻監(jiān)控系統(tǒng)施工方案
- 2024年貴州省貴陽市南明區(qū)中考一模考試物理試題
- 電梯維護保養(yǎng)規(guī)則(TSG T5002-2017)
- 水準測量記錄表自動計算(轉(zhuǎn)點)
評論
0/150
提交評論