版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、/需求分析:寫一個(gè)較為嚴(yán)謹(jǐn)?shù)膶W(xué)生選課系統(tǒng),實(shí)現(xiàn)學(xué)生可以選擇多門選課課程,并將有效選課結(jié)果保存到數(shù)據(jù)庫。學(xué)生需要登錄后,才能選課。讓學(xué)生可以在選課系統(tǒng)通過多種方式查詢到要選的課程信息。/選課規(guī)則:1、每個(gè)學(xué)生可以選多門課程,多次選課的總學(xué)分不能超過6學(xué)分;2、不能重復(fù)選擇一門課程;3、每一門課程的選課人數(shù)都有數(shù)量限制,當(dāng)某門課程的選課名額滿時(shí),則應(yīng)另選課程。4、憑用戶名和密碼登錄,通過提交某一課程號(hào)來選課/總體設(shè)計(jì)方案:建立三個(gè)類:登錄類register,選課類studentChooseCourse,數(shù)據(jù)庫工具類JDBCUtil;一個(gè)SQL腳本文件用于生成數(shù)據(jù)庫表結(jié)構(gòu)和初始記錄,以搭建數(shù)據(jù)庫環(huán)境
2、。登錄類register類,負(fù)責(zé)對(duì)用戶的身份進(jìn)行驗(yàn)證;工具類JDBCUtil用于實(shí)現(xiàn)連接,你可調(diào)用JDBCUtil的getConnection()方法等到鏈接。選課類studentChooseCourse用于,實(shí)現(xiàn)選課功能。其中包括幾個(gè)主要方法:1、actionPerformed(ActionEvent) 用于監(jiān)聽用戶“查詢”和“提交”操作,并負(fù)責(zé)調(diào)用各種方法對(duì)其進(jìn)行處理2、createSearchCourse()用于產(chǎn)生圖形用戶界面3、processBeforeCommit()用于對(duì)用戶的“提交”查找進(jìn)行驗(yàn)證,剔除無效的用戶操作4、tryCommit()負(fù)責(zé)對(duì)有效的“提交”操作,進(jìn)一步處理,
3、并將有效的操作結(jié)果時(shí)時(shí)保存到數(shù)據(jù)庫,并更新數(shù)據(jù)庫原有信息/本程序用到的知識(shí)點(diǎn):數(shù)據(jù)庫連接JDBC;SQL建表、插入輸入、動(dòng)態(tài)查詢;圖形用戶界面的產(chǎn)生以及處理查詢結(jié)果集并較好顯示;程序設(shè)計(jì)基礎(chǔ)知識(shí)。/代碼如下: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, 課程名, 學(xué)時(shí), 學(xué)分, 學(xué)期, 性質(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)造方法中添加兩個(gè)參數(shù)。以便在提交時(shí),將學(xué)生的身份
6、連同所選的課程,一同記錄在學(xué)生選課表中。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ù)用戶的時(shí)間,做出相應(yīng)的反映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)用下面的這個(gè)方法,在數(shù)據(jù)庫中進(jìn)
8、行查找,并將結(jié)果顯示在表格中。searchAvailableCourse(); else if (提交.equals(str) /processBeforeCommit()對(duì)用戶選課操作進(jìn)行有效性檢驗(yàn);/剔除無效操作:如輸入無效的課程號(hào),或已經(jīng)選擇了某一課程,已經(jīng)選滿的6學(xué)分等各種情況boolean effect=processBeforeCommit(); /如果課程存在,且該學(xué)生具有選擇該課程的資格,即effect為true,進(jìn)入正式提交程序(tryCommit()if(effect=true) tryCommit();/對(duì)用戶選課操作進(jìn)行有效性檢驗(yàn);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學(xué)分,系統(tǒng)將退出);this.setVisible(false);return false;/無效操作3:課程該學(xué)生已經(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)選擇了該課程。請(qǐng)另選課程);return false;/如果以上無效操作都不存在,則返回true,意為這是一個(gè)準(zhǔn)有效操作return true;/對(duì)有效的提交操作的進(jìn)行處理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個(gè)人。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();/對(duì)用戶查詢課程信息,進(jìn)行處理,并顯示查詢結(jié)果public void searchAvailableCourse() / 讓程序自動(dòng)選擇連接的是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時(shí)的查詢進(jìn)行處理;并在表格中實(shí)現(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 (學(xué)時(shí).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 (學(xué)分.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 (學(xué)
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, 該課程不存在,請(qǐng)重新輸
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( 學(xué) 生 選 課 系 統(tǒng) );jp11.add(jlSearch);jcbSearch.addItem(new String(課程名);jcbSearch.addItem(new String(課程ID);jcbSearch.addItem(new String(學(xué)時(shí));jcbSearch.addI
22、tem(new String(學(xué)分);jcbSearch.addItem(new String(學(xué)期);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、請(qǐng)輸入課程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);/當(dāng)某學(xué)生有效登錄后,啟動(dòng)程序(將學(xué)生的登錄信息也傳過來,以便保存選課操作時(shí)使用
24、)public static void main(String args) /String usrName = xuliang;/String passwrd = 123;new StudentChooseCourse(usrName, passwrd);/-以下為搭建學(xué)生選課系統(tǒng)的SQL腳本文件。只需運(yùn)行一次,就可有完整的數(shù)據(jù)庫表結(jié)構(gòu)和初始記錄。 /-/該腳本針對(duì)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,學(xué)生);insert into registerXu values(s002,xuliang2,1234,學(xué)生);insert into registerXu values(j001,jack,12345,學(xué)生);insert into registerXu values(001,user,user,學(xué)生);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:學(xué)生表-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學(xué)生選課表drop table choicesxx;create table choicesxx(sname varchar(20) references studentxx(sname),cno varchar(20
31、) references coursexx(cno);-判斷某一個(gè)學(xué)生已經(jīng)選的課程的總學(xué)分是否小于6分/-用戶登錄類 -/用途:驗(yàn)證用戶是否具有登錄系統(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(學(xué)生成績管理與選課系統(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 注冊(cè)public void actionPerformed(ActionEvent e) String str = e.getActionCo
33、mmand();if (str.equalsIgnoreCase(登錄) /當(dāng)用戶點(diǎn)擊登錄時(shí),調(diào)用以下方法去數(shù)據(jù)庫做匹配processLogin(); else if (str.equalsIgnoreCase(取消) jtfUserName.setText();jpfPassWord.setText(); else if (str.equalsIgnoreCase(注冊(cè)) new CreateLogin();/當(dāng)用戶點(diǎn)擊登錄時(shí),調(diào)用以下方法去數(shù)據(jù)庫做匹配public void processLogin() / create connection to the database.Connect
34、ion con = null;/ Connection con = JDBCUtil.getConnection();/讓程序自動(dòng)連接相應(yīng)的數(shù)據(jù)庫,以避免連接數(shù)據(jù)庫時(shí)頻繁改動(dòng)連接程序if (JdbcUtil.class = null) /連接達(dá)內(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 學(xué)生選課系統(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(學(xué)生成績管理系統(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(學(xué)生 );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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年04月中國農(nóng)業(yè)發(fā)展銀行廣東省分行紀(jì)委辦調(diào)查專業(yè)人才社會(huì)招考筆試歷年參考題庫附帶答案詳解
- 2025年度常州消防設(shè)施檢測與鑒定合同4篇
- 2024版水泥混凝土運(yùn)輸合同書
- 2025年度城市基礎(chǔ)設(shè)施配套拆遷施工合同4篇
- 專業(yè)菊花供應(yīng)商2024年銷售協(xié)議版B版
- 《流行病癥:新型冠狀病毒肺炎》課件
- 二零二五年度玻璃原材料期貨交易合同6篇
- 2024年03月廣東中信銀行深圳分行社會(huì)招考筆試歷年參考題庫附帶答案詳解
- 二零二五版存量房市場政策研究合同3篇
- 2024簡易散伙協(xié)議規(guī)范格式
- 蘇北四市(徐州、宿遷、淮安、連云港)2025屆高三第一次調(diào)研考試(一模)語文試卷(含答案)
- 第7課《中華民族一家親》(第一課時(shí))(說課稿)2024-2025學(xué)年統(tǒng)編版道德與法治五年級(jí)上冊(cè)
- 2024年醫(yī)銷售藥銷售工作總結(jié)
- 急診科十大護(hù)理課件
- 山東省濟(jì)寧市2023-2024學(xué)年高一上學(xué)期1月期末物理試題(解析版)
- GB/T 44888-2024政務(wù)服務(wù)大廳智能化建設(shè)指南
- 2025年上半年河南鄭州滎陽市招聘第二批政務(wù)輔助人員211人筆試重點(diǎn)基礎(chǔ)提升(共500題)附帶答案詳解
- 山東省濟(jì)南市歷城區(qū)2024-2025學(xué)年七年級(jí)上學(xué)期期末數(shù)學(xué)模擬試題(無答案)
- 國家重點(diǎn)風(fēng)景名勝區(qū)登山健身步道建設(shè)項(xiàng)目可行性研究報(bào)告
- 投資計(jì)劃書模板計(jì)劃方案
- 《接觸網(wǎng)施工》課件 3.4.2 隧道內(nèi)腕臂安裝
評(píng)論
0/150
提交評(píng)論