admin 管理员组

文章数量: 887021


2024年2月28日发(作者:软件测试速成)

Java完整的学生选课系统源码

需求分析:写一个较为严谨的学生选课系统,实现学生可以选择多门选课课程,并将有效选课结果保存到数据库。学生需要登录后,才能选课。让学生可以在选课系统通过多种方式查询到要选的课程信息。

//选课规则:1、每个学生可以选多门课程,多次选课的总学分不能超过6学分;2、不能重复选择一门课程;3、每一门课程的选课人数都有数量限制,当某门课程的选课名额满时,则应另选课程。4、凭用户名和密码登录,通过提交某一课程号来选课

//总体设计方案:建立三个类:登录类register,选课类studentChooseCourse,数据库工具类JDBCUtil;一个SQL脚本文件用于生成数据库表结构和初始记录,以搭建数据库环境。

登录类register类,负责对用户的身份进行验证;工具类JDBCUtil用于实现连接,你可调用JDBCUtil的getConnection()方法等到链接。

选课类studentChooseCourse用于,实现选课功能。其中包括几个主要方法:

1、actionPerformed(ActionEvent) 用于监听用户“查询”和“提交”操作,并负责调用各种方法对其进行处理

2、createSearchCourse()用于产生图形用户界面

3、processBeforeCommit()用于对用户的“提交”查找进行验证,剔除无效的用户操作

4、tryCommit()负责对有效的“提交”操作,进一步处理,并将有效的操作结果时时保存到数据库,并更新数据库原有信息

//本程序用到的知识点:数据库连接JDBC;SQL建表、插入输入、动态查询;图形用户界面的产生以及处理查询结果集并较好显示;程序设计基础知识。

//代码如下:

import yout;

import Event;

import Listener;

import .*;

import .*;

import tTableModel;

import il;

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 = 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;

//在构造方法中添加两个参数。以便在"提交"时,将学生的身份连同所选的课程,一同记录在学生选课表中。

public StudentChooseCourse(String usrName, String passwrd) {

createSearchCourse();

e = usrName;

d = passwrd;

}

public String getUsrName() {

return usrName;

}

public void setUsrName(String usrName) {

e = usrName;

}

public String getPasswrd() {

return passwrd;

}

public void setPasswrd(String passwrd) {

d = passwrd;

}

//根据用户的时间,做出相应的反映

public void actionPerformed(ActionEvent e) {

String str = ionCommand();

//清空结果显示区中的内容,如果有的话。

if ("查询".trim().equals(str)) {

int k = 1;

while (k < 10) {

for (int i = 1; i < 7; i++) {

ueAt(null, k - 1, i - 1);

}

k++;

}

//调用下面的这个方法,在数据库中进行查找,并将结果显示在表格中。

searchAvailableCourse();

} else if ("提交".equals(str)) {

//processBeforeCommit()对用户选课操作进行有效性检验;

//剔除无效操作:如输入无效的课程号,或已经选择了某一课程,已经选满的6学分等各种情况

boolean effect=processBeforeCommit();

//如果课程存在,且该学生具有选择该课程的资格,即effect为true,进入正式提交程序(tryCommit())

if(effect==true)

{tryCommit();}

}

}

//对用户选课操作进行有效性检验;

public boolean processBeforeCommit(){

//清空原结果显示区中的内容,如果有的话。

int k = 1;

while (k < 10) {

for (int i = 1; i < 7; i++) {

ueAt(null, k - 1, i - 1);

}

k++;

}

//取得用户输入的课程号

String userInput = t().toString().trim()

.toLowerCase();

//无效操作1:在数据库中的coursexx表中查询该课程号。如果不存在该课程,给出提示。

String sql = "select cno from coursexx where cno=? ";

boolean flagCourseExist = false;

try {

ps = eStatement(sql);

ing(1, userInput);

rs = eQuery();

flagCourseExist = ();

} catch (Exception eC) {

tackTrace();

}

if (!flagCourseExist) {

ssageDialog(null, "该课程不存在,请重新输入");

return false;

}

//判断该学生选修课已选课程的总学分是否小于6;

//无效操作2:如果已有选课记录,并且总学分大于6学分,该学生不能在选了。

PreparedStatement ps = null;

sql = "select sum(grade) "

+ "from (select , , grade "

+ "from coursexx k join choicesxx x "

+ "on = and =?) result";

String grade =null;

try {

ps = eStatement(sql);

ing(1, usrName);

rs = eQuery();

while (()) {

grade = ing(1);

if(grade==null){grade="0";}

}

} catch (Exception rrr) {

tackTrace();

}

n("总学分:" + grade);

if (nt(grade) > 6) {

ssageDialog(null, "你已经选满6学分,系统将退出");

ible(false);

return false;

}

//无效操作3:课程该学生已经选择了某课程,则不能再选该课程了。

sql = "select * from choicesxx where sname=? and cno=?";

boolean flag = false;

try {

ps = eStatement(sql);

ing(1, Name());

ing(2, userInput);

rs = eQuery();

flag = ();

} catch (Exception eaa) {

tackTrace();

}

if (flag) {

ssageDialog(null, "你已经选择了该课程。请另选课程");

return false;}

//如果以上无效操作都不存在,则返回true,意为这是一个准有效操作

return true;

}

//对有效的提交操作的进行处理

public void tryCommit() {

// userInput为用户输入的课程ID.

String userInput = t().toString().trim()

.toLowerCase();

// if course still available(count

// else if course not available,show Message to student.

PreparedStatement ps;

String sql = "select (Max-selectedCount) as RemainedCount "

+ "from Coursexx where cno=?";

try {

ps = eStatement(sql);

// 取得学生ID或名字,将课程ID存入学生选课表choicesxx

ing(1, userInput);

rs = eQuery();

ResultSetMetaData meta = aData();

int cols = umnCount();

int RemainedCount = -1;

while (()) {

RemainedCount = (1);

n("RemainedCount:" + RemainedCount);

}

//如果该课程还有选择的名额,提示单项选课操作成功。

if (RemainedCount > 0) {

// save studentId and courseId to student-course table.

// Name();userInput

sql = "insert into choicesxx values(?,?)";

ps = eStatement(sql);

ing(1, Name());

ing(2, userInput);

eUpdate();

ssageDialog(null, "选课成功: " + Name()

+ " 选了" + userInput + "." + "" + " 还有 " + RemainedCount

+ " 人可以选该课程。");

// 更新课程中已选该课程的人数:即将可选该课程的人数减去1个人。

sql = "update CourseXX set selectedCount=selectedCount+1 where cno=?";

ps = eStatement(sql);

ing(1, userInput);

eUpdate();

();

//如果该课程已经没有选择名额,提示重新选课

}

} catch (Exception es) {

tackTrace();

try {

ck();

} catch (Exception ey) {

tackTrace();

}

}

}

//对用户查询课程信息,进行处理,并显示查询结果

public void searchAvailableCourse() {

// 让程序自动选择连接的是Oracle或SqlServer.

if (nection() != null) {

n(nection());

con = nection();

} else {

con = nection();

}

//userInput取得用户输入的信息,selectedItem取得用户选择的查询方式

String userInput = t().toString().trim().toLowerCase();

String selectedItem = ectedItem().toString().trim();

n("User search:" + userInput);

n("selectedItem:" + selectedItem);

String sql = null;

//按用户查询方式,如按课程名,课程ID或学时的查询进行处理;并在表格中实现结果

try {

if ("课程名".equals(selectedItem)) {

sql = "select cno,cname,hour,grade,term,isNeed from CourseXX where cname = ?";

ps = eStatement(sql);

ing(1, userInput);

} else if ("课程ID".equals(selectedItem)) {

sql = "select cno,cname,hour,grade,term,isNeed from CourseXX where cno = ?";

ps = eStatement(sql);

ing(1, userInput);

} else if ("学时".equals(selectedItem)) {

sql = "select cno,cname,hour,grade,term,isNeed from CourseXX where hour = ?";

ps = eStatement(sql);

(1, nt(userInput));

} else if ("学分".equals(selectedItem)) {

sql = "select cno,cname,hour,grade,term,isNeed from CourseXX where grade = ?";

ps = eStatement(sql);

(1, nt(userInput));

} else if ("学期".equals(selectedItem)) {

sql = "select cno,cname,hour,grade,term,isNeed from CourseXX where term = ?";

ps = eStatement(sql);

ing(1, userInput);

}

n(sql);

rs = eQuery();

();

ResultSetMetaData meta = aData();

int cols = umnCount();

String result = null;

int k = 1;

boolean flag = false;

//将查询结果以表格的形式显示出来

while (()) {

for (int i = 1; i <= cols; i++) {

result = ing(i);

n(result);

ueAt(result, k - 1, i - 1);

}

k++;

flag = true;

}

//如果查询结果集为空,提示用户没有该课程

if (flag == false) {

ssageDialog(null, "该课程不存在,请重新输入");

return;

}

} catch (Exception ex) {

tackTrace();

try {

ck();

} catch (Exception er) {

tackTrace();

}

}

//产生图形用户界面,以便用户操作

public void createSearchCourse() {

out(new GridLayout(3, 1));

JPanel jp1 = new JPanel();

out(new GridLayout(4, 1));

JPanel jp2 = new JPanel();

JPanel jp3 = new JPanel();

JPanel jp10 = new JPanel();

JPanel jp11 = new JPanel();

JPanel jp12 = new JPanel();

JPanel jp13 = new JPanel();

JLabel jlSearch = new JLabel(" 学 生 选 课 系 统 ");

(jlSearch);

m(new String("课程名"));

m(new String("课程ID"));

m(new String("学时"));

m(new String("学分"));

m(new String("学期"));

(jtfSearch);

(jcbSearch);

JButton jbOK = new JButton("查询");

ionListener(this);

e(90, 20);

(jbOK);

(jp10);

(jp11);

(jp12);

(jp13);

(jspSearch);

JLabel jlSelectedCourse = new JLabel("请输入课程ID:");

JButton jbSelectedCourse = new JButton("提交");

ionListener(this);

(jlSelectedCourse);

(jtfSelectedCourse);

(jbSelectedCourse);

(jp1);

(jp2);

(jp3);

ible(true);

e(485, 600);

}

//当某学生有效登录后,启动程序(将学生的登录信息也传过来,以便保存选课操作时使用)

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;

create 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','学生');

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 varchar(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','NoNecessary',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:学生表--

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','sd0910','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学生选课表

drop table choicesxx;

create table choicesxx(

sname varchar(20) references studentxx(sname),cno varchar(20)

references coursexx(cno));

--判断某一个学生已经选的课程的总学分是否小于6分

//----------用户登录类 ---------------------------------------

//用途:验证用户是否具有登录系统的资格------------------------

package school;

import .*;

import il;

import .*;

import .*;

import .*;

public class Register implements ActionListener {

JFrame jf = new JFrame("学生成绩管理与选课系统");

JTextField jtfUserName = new JTextField(10);

JPasswordField jpfPassWord = new JPasswordField(10);

JComboBox identify = new JComboBox();

//constructor

public Register() {

CreateRegisterGUI();

}

// deal with user action, when user check:"登录","取消"or "注册"

public void actionPerformed(ActionEvent e) {

String str = ionCommand();

if (IgnoreCase("登录")) {

//当用户点击登录时,调用以下方法去数据库做匹配

processLogin();

} else if (IgnoreCase("取消")) {

t("");

t("");

} else if (IgnoreCase("注册")) {

new CreateLogin();

}

}

//当用户点击登录时,调用以下方法去数据库做匹配

public void processLogin() {

// create connection to the database.

Connection con = null;

// Connection con = nection();

//让程序自动连接相应的数据库,以避免连接数据库时频繁改动连接程序

if ( == null) {

//连接达内Oracle数据库

con = nection();

} else {

//连接本地SQLSERVER数据库

con = nection();

}

// write sql sentence

String usrName = t().trim();

String passwrd = new String(sword()).trim();

String ident = ectedItem().toString().trim();

String sql = "select * from registerXu "

+ "where userName=? and passWord=? and identify=?";

n(usrName + ":" + passwrd + ":" + ident);

// create object of PreparedStatement

try {

PreparedStatement ps = eStatement(sql);

// Prepare parameter for the sql

ing(1, usrName);

ing(2, passwrd);

ing(3, ident);

// send parameter to compiler to compile.

ResultSet rs = eQuery();

StringBuffer sb = new StringBuffer("");

ResultSetMetaData meta = aData();

int cols = umnCount();

//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 (()) {

for (int i = 1; i < cols; i++) {

(umnName(i));

(ing(i));

}

}

if (() < 1) {

ssageDialog(null, "用户名或密码错误");

} else if (() >= 1) {

if (("student")) {

//if he or she is a student, and usrName-passwrd alright, then go to 学生选课系统

new StudentChooseCourse(usrName, passwrd);

ible(false);

// new StudentEntered();

} else if (("teacher")) {

// new TeacherEntered(usrName,passwrd);

new TeacherEntered();

ible(false);

} else if (("admin")) {

// go to administrator pages.

}

}

} catch (Exception er) {

tackTrace();

}

}

//当用户点击登录时,调用以下方法去数据库做匹配

public void processLogin() {

// create connection to the database.

Connection con = null;

// Connection con = nection();

//让程序自动连接相应的数据库,以避免连接数据库时频繁改动连接程序

if ( == null) {

//连接达内Oracle数据库

con = nection();

} else {

//连接本地SQLSERVER数据库

con = nection();

}

// write sql sentence

String usrName = t().trim();

String passwrd = new String(sword()).trim();

String ident = ectedItem().toString().trim();

String sql = "select * from registerXu "

+ "where userName=? and passWord=? and identify=?";

n(usrName + ":" + passwrd + ":" + ident);

// create object of PreparedStatement

try {

PreparedStatement ps = eStatement(sql);

// Prepare parameter for the sql

ing(1, usrName);

ing(2, passwrd);

ing(3, ident);

// send parameter to compiler to compile.

ResultSet rs = eQuery();

StringBuffer sb = new StringBuffer("");

ResultSetMetaData meta = aData();

int cols = umnCount();

//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 (()) {

for (int i = 1; i < cols; i++) {

(umnName(i));

(ing(i));

}

}

if (() < 1) {

ssageDialog(null, "用户名或密码错误");

} else if (() >= 1) {

if (("student")) {

//if he or she is a student, and usrName-passwrd alright, then go to 学生选课系统

new StudentChooseCourse(usrName, passwrd);

ible(false);

// new StudentEntered();

} else if (("teacher")) {

// new TeacherEntered(usrName,passwrd);

new TeacherEntered();

ible(false);

} else if (("admin")) {

// go to administrator pages.

}

}

} catch (Exception er) {

tackTrace();

}

}

//--------- 选课系统多次调用到的本类JDBCUtil的getConnection方法--------------------------------

//---------通过该工具类选课系统连接到数据库。JdbcUtil类与次类似,连接的是Oracle数据库----------

import .*;

public class JDBCUtil {

public JDBCUtil() {

}

PreparedStatement ps = null;

ResultSet rs = null;

static {

String driverName = "verDriver";

try {

e(driverName);

} catch (ClassNotFoundException e) {

tackTrace();

}

}

public static Connection getConnection() {

Connection connection = null;

String url = "jdbc:sqlserver://localhost:1433; DatabaseName=master";

String user = "sa";

String password = "sd100301";

// String password="Foolish004";

try {

connection = nection(url, user, password);

oCommit(false);

n(connection);

} catch (SQLException e) {

tackTrace();

}

return connection;

}

public static void main(String[] args) {

new JDBCUtil().getConnection();

}

}


本文标签: 课程 选课 数据库 学生 用户