最新Java完整的学生选课系统源码.docx

上传人:1595****071 文档编号:33835020 上传时间:2022-08-12 格式:DOCX 页数:34 大小:410.93KB
返回 下载 相关 举报
最新Java完整的学生选课系统源码.docx_第1页
第1页 / 共34页
最新Java完整的学生选课系统源码.docx_第2页
第2页 / 共34页
点击查看更多>>
资源描述

《最新Java完整的学生选课系统源码.docx》由会员分享,可在线阅读,更多相关《最新Java完整的学生选课系统源码.docx(34页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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、tryCommit(

3、)负责对有效的“提交”操作,进一步处理,并将有效的操作结果时时保存到数据库,并更新数据库原有信息/本程序用到的知识点:数据库连接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.DefaultTableMo

4、del;import school.schoolPicture.JdbcUtil;public class StudentChooseCourse extends JFrame implements ActionListener JTextField jtfSearch = new JTextField(11);String columnNames = new String 课程ID, 课程名, 学时, 学分, 学期, 性质 ;DefaultTableModel dtmSearch = new DefaultTableModel(columnNames, 27);JTable jtSearch

5、 = new JTable(dtmSearch);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;/在构造方法中

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() ret

7、urn passwrd;public void setPasswrd(String passwrd) this.passwrd = passwrd;/根据用户的时间,做出相应的反映public void actionPerformed(ActionEvent e) String str = e.getActionCommand();/清空结果显示区中的内容,如果有的话。if (查询.trim().equals(str) int k = 1;while (k 10) for (int i = 1; i 7; i+) jtSearch.setValueAt(null, k - 1, i - 1);

8、k+;/调用下面的这个方法,在数据库中进行查找,并将结果显示在表格中。searchAvailableCourse(); else if (提交.equals(str) /processBeforeCommit()对用户选课操作进行有效性检验;/剔除无效操作:如输入无效的课程号,或已经选择了某一课程,已经选满的6学分等各种情况boolean effect=processBeforeCommit(); /如果课程存在,且该学生具有选择该课程的资格,即effect为true,进入正式提交程序(tryCommit()if(effect=true) tryCommit();/对用户选课操作进行有效性检验

9、;public boolean processBeforeCommit()/清空原结果显示区中的内容,如果有的话。int k = 1;while (k 10) for (int i = 1; i 6) JOptionPane.showMessageDialog(null, 你已经选满6学分,系统将退出);this.setVisible(false);return false;/无效操作3:课程该学生已经选择了某课程,则不能再选该课程了。sql = select * from choicesxx where sname=? and cno=?;boolean flag = false;try p

10、s = 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, 你已经选择了该课程。请另选课程);return false;/如果以上无效操作都不存在,则返回true,意为这是一个准有效操作return true;/对有

11、效的提交操作的进行处理public void 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 =

12、 con.prepareStatement(sql);ps.setString(1, this.getUsrName();ps.setString(2, userInput);ps.executeUpdate();JOptionPane.showMessageDialog(null, 选课成功: + this.getUsrName()+ 选了 + userInput + . + + 还有 + RemainedCount+ 人可以选该课程。);/ 更新课程中已选该课程的人数:即将可选该课程的人数减去1个人。sql = update CourseXX set selectedCount=selec

13、tedCount+1 where cno=?;ps = con.prepareStatement(sql);ps.setString(1, userInput);ps.executeUpdate();mit();/如果该课程已经没有选择名额,提示重新选课 catch (Exception es) es.printStackTrace();try con.rollback(); catch (Exception ey) ey.printStackTrace();/对用户查询课程信息,进行处理,并显示查询结果public void searchAvailableCourse() / 让程序自动选择

14、连接的是Oracle或SqlServer.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 selected

15、Item = jcbSearch.getSelectedItem().toString().trim();System.out.println(User search: + userInput);System.out.println(selectedItem: + selectedItem);String sql = null;/按用户查询方式,如按课程名,课程ID或学时的查询进行处理;并在表格中实现结果try if (课程名.equals(selectedItem) sql = select cno,cname,hour,grade,term,isNeed from CourseXX whe

16、re cname = ?;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

17、,grade,term,isNeed 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

18、); else if (学期.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();Str

19、ing result = 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, 该课程不存

20、在,请重新输入);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();JPanel

21、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);jcbSearch.addItem(new String(学时);jcbSearc

22、h.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);jp1.add(jp11);jp1.add(jp12);jp1.add(jp13);jp2.add(jspSearch);JLabel jlSelectedCourse = new J

23、Label(请输入课程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);/-以下为搭建学生选课系统的SQL脚本文件。只需运行一次,就可有完整的数据库表结构和初始记录。 /-/该脚本针对SQLserver;Oracle的SQL脚本类似,只需将将数据类型如varchar改为varchar2,int改为number等-table1: registerXU 登录表drop table registerXu;c

25、reate 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,

26、学生);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 varchar(20),hour Int,grade Int,term varchar(20),isNeed varc

27、har(20), selectedCount Int ,Max Int);insert into Coursexx values(c001,CoreJava,50,5,201007,NoNecessary,0,50);insert into Coursexx values(c002,XML,20,2,201008,NoNecessary,0,40);insert into Coursexx values(c003,HIBERNATE,20,4,201008,NoNecessary,0,30);insert into Coursexx values(c004,SQL,20,4,201006,No

28、Necessary,0,5);insert into Coursexx values(c005,JDBC,20,2,201008,NoNecessary,0,3);insert into Coursexx values(c006,AJAX,20,2,201010,NoNecessary,0,1);insert into Coursexx values(c007,JSP,100,8,201009,NoNecessary,0,1-sql = select cno,cname,hour,grade,term,isNeed from CourseXX where cname = ?; -table 3

29、:学生表-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,19901124,sd1003,good);insert into studentxx values(s002,xuliang2,male,19851124,s

30、d0910,good);insert into studentxx values(j001,jack,male,19881124,sd1003,good);insert into studentxx values(001,user,male,19901124,sd1005,good);insert into studentxx values(s003,sisi,female,19880124,sd1007,good);insert into studentxx values(as003,crystal,female,19880124,asd1007,good);-table 4:choices

31、学生选课表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.*;import java.awt.*;im

32、port 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() CreateRegisterGUI();/ deal with

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

34、w CreateLogin();/当用户点击登录时,调用以下方法去数据库做匹配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 =

35、 JDBCUtil.getConnection();/ write 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=?;

36、System.out.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

37、= ps.executeQuery();StringBuffer sb = new StringBuffer();ResultSetMetaData meta = rs.getMetaData();int cols = 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=fla

38、se,showMessage(Input ERROR)while (rs.next() for (int i = 1; i cols; i+) sb.append(meta.getColumnName(i);sb.append(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 学生选课系统new StudentChooseCourse(usrName, passwrd);jf.set

39、Visible(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();/产生图形用户界面public void CreateRegisterGUI() jf.setLa

40、yout(new GridLayout(5, 1);JPanel jp1 = new JPanel();JLabel jl1 = new JLabel(学生成绩管理系统);jp1.add(jl1);jf.add(jp1);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 = ne

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁