Java完整的学生选课系统源码(共15页)_第1页
Java完整的学生选课系统源码(共15页)_第2页
Java完整的学生选课系统源码(共15页)_第3页
Java完整的学生选课系统源码(共15页)_第4页
Java完整的学生选课系统源码(共15页)_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上Java完整的学生选课系统源码需求分析:写一个较为严谨的学生选课系统,实现学生可以选择多门选课课程,并将有效选课结果保存到数据库。学生需要登录后,才能选课。让学生可以在选课系统通过多种方式查询到要选的课程信息。/选课规则:1、每个学生可以选多门课程,多次选课的总学分不能超过6学分;2、不能重复选择一门课程;3、每一门课程的选课人数都有数量限制,当某门课程的选课名额满时,则应另选课程。4、凭用户名和密码登录,通过提交某一课程号来选课/总体设计方案:建立三个类:登录类register,选课类studentChooseCourse,数据库工具类JDBCUtil;一个SQL脚

2、本文件用于生成数据库表结构和初始记录,以搭建数据库环境。登录类register类,负责对用户的身份进行验证;工具类JDBCUtil用于实现连接,你可调用JDBCUtil的getConnection()方法等到链接。选课类studentChooseCourse用于,实现选课功能。其中包括几个主要方法:1、actionPerformed(ActionEvent) 用于监听用户“查询”和“提交”操作,并负责调用各种方法对其进行处理2、createSearchCourse()用于产生图形用户界面3、processBeforeCommit()用于对用户的“提交”查找进行验证,剔除无效的用户操作4、try

3、Commit()负责对有效的“提交”操作,进一步处理,并将有效的操作结果时时保存到数据库,并更新数据库原有信息/本程序用到的知识点:数据库连接JDBC;SQL建表、插入输入、动态查询;图形用户界面的产生以及处理查询结果集并较好显示;程序设计基础知识。 /代码如下:import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.*;import javax.swing.*;import javax.swing.table.Defaul

4、tTableModel;import school.schoolPicture.JdbcUtil;public class StudentChooseCourse extends JFrame implements ActionListener JTextField jtfSearch = new JTextField(11);String columnNames = new String "课程ID", "课程名", "学时", "学分", "学期", "性质" ;Defa

5、ultTableModel dtmSearch = new DefaultTableModel(columnNames, 27);JTable jtSearch = new JTable(dtmSearch);JScrollPane jspSearch = new JScrollPane(jtSearch);JComboBox jcbSearch = new JComboBox();JTextField jtfSelectedCourse = new JTextField(10);Connection con = null;PreparedStatement ps = null;ResultS

6、et rs = null;private static String usrName;private static String passwrd;/在构造方法中添加两个参数。以便在"提交"时,将学生的身份连同所选的课程,一同记录在学生选课表中。public StudentChooseCourse(String usrName, String passwrd) createSearchCourse();this.usrName = usrName;this.passwrd = passwrd;public String getUsrName() return usrName;

7、public void setUsrName(String usrName) this.usrName = usrName;public String getPasswrd() return passwrd;public void setPasswrd(String passwrd) this.passwrd = passwrd; /根据用户的时间,做出相应的反映public void actionPerformed(ActionEvent e) String str = e.getActionCommand();/清空结果显示区中的内容,如果有的话。if ("查询".tr

8、im().equals(str) int k = 1;while (k < 10) for (int i = 1; i < 7; i+) jtSearch.setValueAt(null, k - 1, i - 1);k+;/调用下面的这个方法,在数据库中进行查找,并将结果显示在表格中。searchAvailableCourse(); else if ("提交".equals(str) /processBeforeCommit()对用户选课操作进行有效性检验;/剔除无效操作:如输入无效的课程号,或已经选择了某一课程,已经选满的6学分等各种情况boolean ef

9、fect=processBeforeCommit(); /如果课程存在,且该学生具有选择该课程的资格,即effect为true,进入正式提交程序(tryCommit()if(effect=true)tryCommit();/对用户选课操作进行有效性检验;public boolean processBeforeCommit()/清空原结果显示区中的内容,如果有的话。int k = 1;while (k < 10) for (int i = 1; i < 7; i+) jtSearch.setValueAt(null, k - 1, i - 1);k+;/取得用户输入的课程号Strin

10、g userInput = jtfSelectedCourse.getText().toString().trim().toLowerCase();/无效操作1:在数据库中的coursexx表中查询该课程号。如果不存在该课程,给出提示。String sql = "select cno from coursexx where cno=? "boolean flagCourseExist = false;try ps = con.prepareStatement(sql);ps.setString(1, userInput);rs = ps.executeQuery();fla

11、gCourseExist = rs.next(); catch (Exception eC) eC.printStackTrace();if (!flagCourseExist) JOptionPane.showMessageDialog(null, "该课程不存在,请重新输入");return false;/判断该学生选修课已选课程的总学分是否小于6;/无效操作2:如果已有选课记录,并且总学分大于6学分,该学生不能在选了。PreparedStatement ps = null;sql = "select sum(grade) "+ "from

12、 (select x.sname , o,k.grade grade "+ "from coursexx k join choicesxx x "+ "on o=o and x.sname=?) result"String grade =null;try ps = con.prepareStatement(sql);ps.setString(1, usrName);rs = ps.executeQuery();while (rs.next() grade = rs.getString(1);if(grade=null)grade="0

13、" catch (Exception rrr) rrr.printStackTrace();System.out.println("总学分:" + grade);if (Integer.parseInt(grade) > 6) JOptionPane.showMessageDialog(null, "你已经选满6学分,系统将退出");this.setVisible(false);return false;/无效操作3:课程该学生已经选择了某课程,则不能再选该课程了。sql = "select * from choicesxx w

14、here sname=? and cno=?"boolean flag = false;try ps = con.prepareStatement(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, "你已经选择了该课程。请另选课程&qu

15、ot;);return false;/如果以上无效操作都不存在,则返回true,意为这是一个准有效操作return true;/对有效的提交操作的进行处理public void tryCommit() / userInput为用户输入的课程ID.String userInput = jtfSelectedCourse.getText().toString().trim().toLowerCase();/ if course still available(count<MAX_STUDENT),save result./ else if course not available,show

16、Message to student.PreparedStatement ps;String sql = "select (Max-selectedCount) as RemainedCount "+ "from Coursexx where cno=?"try ps = con.prepareStatement(sql);/ 取得学生ID或名字,将课程ID存入学生选课表choicesxxps.setString(1, userInput);rs = ps.executeQuery();ResultSetMetaData meta = rs.getMet

17、aData();int cols = meta.getColumnCount();int RemainedCount = -1;while (rs.next() RemainedCount = rs.getInt(1);System.out.println("RemainedCount:" + RemainedCount);/如果该课程还有选择的名额,提示单项选课操作成功。if (RemainedCount > 0) / save studentId and courseId to student-course table./ this.getUsrName();us

18、erInputsql = "insert into choicesxx values(?,?)"ps = con.prepareStatement(sql);ps.setString(1, this.getUsrName();ps.setString(2, userInput);ps.executeUpdate();JOptionPane.showMessageDialog(null, "选课成功: " + this.getUsrName()+ " 选了" + userInput + "." + "&qu

19、ot; + " 还有 " + RemainedCount+ " 人可以选该课程。");/ 更新课程中已选该课程的人数:即将可选该课程的人数减去1个人。sql = "update CourseXX set selectedCount=selectedCount+1 where cno=?"ps = con.prepareStatement(sql);ps.setString(1, userInput);ps.executeUpdate();mit();/如果该课程已经没有选择名额,提示重新选课 catch (Exception es)

20、es.printStackTrace();try con.rollback(); catch (Exception ey) ey.printStackTrace();/对用户查询课程信息,进行处理,并显示查询结果public void searchAvailableCourse() / 让程序自动选择连接的是Oracle或SqlServer.if (JDBCUtil.getConnection() != null) System.out.println(JDBCUtil.getConnection();con = JDBCUtil.getConnection(); else con = Jdb

21、cUtil.getConnection();/userInput取得用户输入的信息,selectedItem取得用户选择的查询方式String userInput = jtfSearch.getText().toString().trim().toLowerCase();String selectedItem = jcbSearch.getSelectedItem().toString().trim();System.out.println("User search:" + userInput);System.out.println("selectedItem:&

22、quot; + selectedItem);String sql = null;/按用户查询方式,如按课程名,课程ID或学时的查询进行处理;并在表格中实现结果try if ("课程名".equals(selectedItem) sql = "select cno,cname,hour,grade,term,isNeed from CourseXX where cname = ?"ps = con.prepareStatement(sql);ps.setString(1, userInput); else if ("课程ID".equa

23、ls(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,isNeed from CourseXX where hour = ?"ps = con

24、.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 ("学期".equals

25、(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 result

26、 = null;int k = 1;boolean flag = false;/将查询结果以表格的形式显示出来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;/如果查询结果集为空,提示用户没有该课程if (flag = false) JOptionPane.showMessageDialog(null, "该课程不存

27、在,请重新输入");return; catch (Exception ex) ex.printStackTrace();try con.rollback(); catch (Exception er) er.printStackTrace();/产生图形用户界面,以便用户操作public void createSearchCourse() this.setLayout(new GridLayout(3, 1);JPanel jp1 = new JPanel();jp1.setLayout(new GridLayout(4, 1);JPanel jp2 = new JPanel();J

28、Panel jp3 = new JPanel();JPanel jp10 = new JPanel();JPanel jp11 = new JPanel();JPanel jp12 = new JPanel();JPanel jp13 = new JPanel();JLabel jlSearch = new JLabel(" 学 生 选 课 系 统 ");jp11.add(jlSearch);jcbSearch.addItem(new String("课程名");jcbSearch.addItem(new String("课程ID")

29、;jcbSearch.addItem(new String("学时");jcbSearch.addItem(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);jp

30、1.add(jp11);jp1.add(jp12);jp1.add(jp13);jp2.add(jspSearch);JLabel jlSelectedCourse = new JLabel("请输入课程ID:");JButton jbSelectedCourse = new JButton("提交");jbSelectedCourse.addActionListener(this);jp3.add(jlSelectedCourse);jp3.add(jtfSelectedCourse);jp3.add(jbSelectedCourse);this.ad

31、d(jp1);this.add(jp2);this.add(jp3);this.setVisible(true);this.setSize(485, 600);/当某学生有效登录后,启动程序(将学生的登录信息也传过来,以便保存选课操作时使用)public static void main(String args) /String usrName = "xuliang"/String passwrd = "123"new StudentChooseCourse(usrName, passwrd);/-以下为搭建学生选课系统的SQL脚本文件。只需运行一次,就

32、可有完整的数据库表结构和初始记录。 /-/该脚本针对SQLserver;Oracle的SQL脚本类似,只需将将数据类型如varchar改为varchar2,int改为number等-table1: registerXU 登录表drop table registerXu;create table registerXu (id varchar(20),userName varchar(20),passWord varchar(20),identify varchar(20);insert into registerXu values('s001','xuliang'

33、,'123','学生');insert into registerXu values('s002','xuliang2','1234','学生');insert into registerXu values('j001','jack','12345','学生');insert into registerXu values('001','user','user','学生');

34、insert 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

35、varchar(20),hour Int,grade Int,term varchar(20),isNeed varchar(20), selectedCount Int ,Max Int);insert into Coursexx values('c001','Core',50,5,'','NoNecessary',0,50);insert into Coursexx values('c002','XML',20,2,'','NoNecessary',0,40);i

36、nsert into Coursexx values('c003','HIBERNATE',20,4,'','NoNecessary',0,30);insert into Coursexx values('c004','SQL',20,4,'','NoNecessary',0,5);insert into Coursexx values('c005','JDBC',20,2,'','NoNecessary'

37、;,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 = ?" 

38、0;-table 3:学生表-drop table studentxx;create table studentxx(sid varchar(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

39、9;);insert into studentxx values('s002','xuliang2','male','','sd0910','good');insert into studentxx values('j001','jack','male','','sd1003','good');insert into studentxx values('001','user'

40、;,'male','','sd1005','good');insert into studentxx values('s003','sisi','female','','sd1007','good');insert into studentxx values('as003','crystal','female','','asd1007','good&

41、#39;);-table 4:choices学生选课表drop table choicesxx;create table choicesxx(sname varchar(20) references studentxx(sname),cno varchar(20) references coursexx(cno);-判断某一个学生已经选的课程的总学分是否小于6分/-用户登录类 -/用途:验证用户是否具有登录系统的资格-package school;import javax.swing.*;import school.schoolPicture.JdbcUtil;import java.sql.

42、*;import java.awt.*;import java.awt.event.*;public class Register implements ActionListener JFrame jf = new JFrame("学生成绩管理与选课系统");JTextField jtfUserName = new JTextField(10);JPasswordField jpfPassWord = new JPasswordField(10);JComboBox identify = new JComboBox();/constructorpublic Register

43、() CreateRegisterGUI();/ deal with user action, when user check:"登录","取消"or "注册"public void actionPerformed(ActionEvent e) String str = e.getActionCommand();if (str.equalsIgnoreCase("登录") /当用户点击登录时,调用以下方法去数据库做匹配processLogin(); else if (str.equalsIgnoreCase(&qu

44、ot;取消") jtfUserName.setText("");jpfPassWord.setText(""); else if (str.equalsIgnoreCase("注册") new CreateLogin();/当用户点击登录时,调用以下方法去数据库做匹配public void processLogin() / create connection to the database.Connection con = null;/ Connection con = JDBCUtil.getConnection();/让

45、程序自动连接相应的数据库,以避免连接数据库时频繁改动连接程序if (JdbcUtil.class = null) /连接达内Oracle数据库con = JdbcUtil.getConnection(); else /连接本地SQLSERVER数据库con = JDBCUtil.getConnection();/ write sql sentenceString usrName = jtfUserName.getText().trim();String passwrd = new String(jpfPassWord.getPassword().trim();String ident = id

46、entify.getSelectedItem().toString().trim();String sql = "select * from registerXu "+ "where userName=? and passWord=? and identify=?"System.out.println(usrName + ":" + passwrd + ":" + ident);/ create object of PreparedStatementtry PreparedStatement ps = con.pr

47、epareStatement(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 = m

48、eta.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.getColu

49、mnName(i);sb.append(rs.getString(i); if (sb.length() < 1) JOptionPane.showMessageDialog(null, "用户名或密码错误"); else if (sb.length() >= 1) if (ident.equals("student") /if he or she is a student, and usrName-passwrd alright, then go to 学生选课系统new StudentChooseCourse(usrName, passw

50、rd);jf.setVisible(false);/ new StudentEntered(); else if (ident.equals("teacher") / new TeacherEntered(usrName,passwrd);new TeacherEntered();jf.setVisible(false); else if (ident.equals("admin") / go to administrator pages. catch (Exception er) er.printStackTrace();/当用户点击登录时,调用以下方

51、法去数据库做匹配public void processLogin() / create connection to the database.Connection con = null;/ Connection con = JDBCUtil.getConnection();/让程序自动连接相应的数据库,以避免连接数据库时频繁改动连接程序if (JdbcUtil.class = null) /连接达内Oracle数据库con = JdbcUtil.getConnection(); else /连接本地SQLSERVER数据库con = JDBCUtil.getConnection();/ wri

52、te sql sentenceString usrName = jtfUserName.getText().trim();String passwrd = new String(jpfPassWord.getPassword().trim();String ident = identify.getSelectedItem().toString().trim();String sql = "select * from registerXu "+ "where userName=? and passWord=? and identify=?"System.o

53、ut.println(usrName + ":" + passwrd + ":" + ident);/ create object of PreparedStatementtry PreparedStatement 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.getMe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论