Java實現(xiàn)學生選課管理系統(tǒng)
本文實例為大家分享了Java實現(xiàn)學生選課管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
需求分析
本數(shù)據(jù)庫的用戶主要是學生,通過對用戶需求的收集和分析,獲得用戶對數(shù)據(jù)庫的如下要求。
1.信息需求
1.學生信息:學號,姓名,性別,專業(yè)
2.登陸信息:賬號,密碼
3.課程信息:課程號,課程名,選課人數(shù),選課容量,任課老師
4.選課信息:課程號,學生學號
5.登錄信息:賬號、密碼
2.功能需求
1.系統(tǒng)為學生建立登陸信息,學生進入系統(tǒng)前需要身份驗證,用戶名、密碼輸入正確后方可進入系統(tǒng)。
2.在系統(tǒng)中,用戶可以在界面中看到本人的基本信息,也可以對課程信息表和個人選課信息表進行查看、以及選課。
3.使用數(shù)據(jù)庫存儲讀取數(shù)據(jù)內(nèi)容
3.系統(tǒng)需求
學生信息管理系統(tǒng)采用的編譯環(huán)境是IntelliJ IDEA,編程語言是Java,使用用MySQL數(shù)據(jù)庫
定義數(shù)據(jù)庫
CREATE DATABASE Couse ON PRIMARY(NAME = Couse, FILENAME = 'D:\JAVA\courseDesign_2020JAVA' , SIZE = 2MB, FILEGROWTH = 10%,FILERROWHT=4MB)?
CREATE TABLE user (
?? ?id char(25) IDENTITY ?NOT NULL PRIMARY KEY,
?? ?name char(25) ?NOT NULL ,
?? ?sex char(2) CHECK (性別 IN('男','女')) ,
profess char(30) ?NULL ,
)
CREATE TABLE subject (
?? ?subjectId varchar(20) ?NOT NULL PRIMARY KEY ,
?? ?name varchar(25) ?NOT NULL ,
?? ?Noss int ?NOT NULL ,
capacity int ? NULL ,
teacher varchar(25) ? NULL ,
)
CREATE TABLE PickCouse (
?? ?stuId char(25),
?? ?couseId char(25) ?NOT NULL ,
)?
CREATE TABLE login (
?? ?Id char(25) ?NOT NULL PRIMARY KEY,
?? ?pwd char(25) ?NOT NULL ,
)?設(shè)計項目結(jié)構(gòu)
1.項目包結(jié)構(gòu)

2.項目UML類圖
運行效果展示
1.登錄

2.查看全部課程信息

3.查看個人選課信息

4.學生選課

開放源代碼
1、學生用戶信息類:
package StuPickCouse_Dos.PickCouse;
public class User {
? ? String id;
? ? String pwd;
? ? String name;
? ? String sex;
? ? String pross;//專業(yè)
? ? public User(String id,String pwd){
? ? ? ? this.id=id;
? ? ? ? this.pwd=pwd;
? ? }
? ? public User(String id, String name, String sex, String pross) {
? ? ? ? this.id = id;
? ? ? ? this.name = name;
? ? ? ? this.sex = sex;
? ? ? ? this.pross = pross;
? ? }
? ? public String getPwd() {
? ? ? ? return pwd;
? ? }
? ? public void setPwd(String pwd) {
? ? ? ? this.pwd = pwd;
? ? }
? ? public User(){}
? ? public String getId() {
? ? ? ? return id;
? ? }
? ? public void setId(String id) {
? ? ? ? this.id = id;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
? ? public String getSex() {
? ? ? ? return sex;
? ? }
? ? public void setSex(String sex) {
? ? ? ? this.sex = sex;
? ? }
? ? public String getPross() {
? ? ? ? return pross;
? ? }
? ? public void setPross(String pross) {
? ? ? ? this.pross = pross;
? ? }
? ? @Override
? ? public String toString() {
? ? ? ? return "User{" +
? ? ? ? ? ? ? ? "id='" + id + '\'' +
? ? ? ? ? ? ? ? ", name='" + name + '\'' +
? ? ? ? ? ? ? ? ", sex='" + sex + '\'' +
? ? ? ? ? ? ? ? ", pross='" + pross + '\'' +
? ? ? ? ? ? ? ? '}';
? ? }
}2、課程信息類:
package StuPickCouse_Dos.PickCouse;
public class Couse {
? ? String id;
? ? String name;
? ? String num;//選課人數(shù)
? ? String capacity;//容量
? ? String teacher;
? ?public String getTeacher() {
? ? ? ? return teacher;
? ? }
? ? public void setTeacher(String teacher) {
? ? ? ? this.teacher = teacher;
? ? }
? ? public Couse(String id, String name, String num, String capacity, String teacher) {
? ? ? ? this.id = id;
? ? ? ? this.name = name;
? ? ? ? this.num = num;
? ? ? ? this.capacity = capacity;
? ? ? ? this.teacher = teacher;
? ? }
? ? public Couse(String id){this.id=id;}
? ? public String getId() {
? ? ? ? return id;
? ? }
? ? public void setId(String id) {
? ? ? ? this.id = id;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
? ? public String getNum() {
? ? ? ? return num;
? ? }
? ? public void setNum(String num) {
? ? ? ? this.num = num;
? ? }
? ? public String getCapacity() {
? ? ? ? return capacity;
? ? }
? ? public void setCapacity(String capacity) {
? ? ? ? this.capacity = capacity;
? ? }
? ? @Override
? ? public String toString() {
? ? ?? ? ? ?return id +"?? ?"+ name +"?? ?"+ num+ "/" + capacity+"?? ?" +teacher;
? ? }
}3、學生數(shù)據(jù)庫操作類
package StuPickCouse_Dos.PickMysql;
import StuPickCouse_Dos.PickCouse.User;
import javax.swing.*;
import java.sql.*;
public class UserSql {
? ? Connection con=null;
? ? Statement sql;
? ? ResultSet rs;
? ? public UserSql(){}
? ? void star(){
? ? ? ? String url="jdbc:mysql://localhost:3306/couse?user=root&password=&useSSL=true&useUnicode=true&characterEncoding=utf-8";
? ? ? ? String user="root";
? ? ? ? String passKey="";
? ? ? ? try {
? ? ? ? ? ? Class.forName("com.mysql.jdbc.Driver");
? ? ? ? ? ? con= DriverManager.getConnection(url,user,passKey);
? ? ? ? }
? ? ? ? catch(SQLException e){
? ? ? ? ? ? System.out.println(e);
? ? ? ? }catch (ClassNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public User getUser(String id){
? ? ? ? User user=new User();
? ? ? ? star();
? ? ? ? try {
? ? ? ? ? ? sql=con.createStatement();
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? rs=sql.executeQuery("select * from User where id="+id);
? ? ? ? ? ? while(rs.next()){
? ? ? ? ? ? ? ? user.setId(id);
? ? ? ? ? ? ? ? user.setName(rs.getString(2));
? ? ? ? ? ? ? ? user.setSex(rs.getString(3));
? ? ? ? ? ? ? ? user.setPross(rs.getString(4));
? ? ? ? ? ? }
? ? ? ? ? ? rs.close();
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? return user;
? ? }
? ? public String findUser(String id){
? ? ? ? String user="";
? ? ? ? star();
? ? ? ? try {
? ? ? ? ? ? sql=con.createStatement();
? ? ? ? } catch (SQLException throwables) {
// ? ? ? ? ? ?throwables.printStackTrace();
? ? ? ? ? ? JOptionPane.showMessageDialog(null,
? ? ? ? ? ? ? ? ? ? "數(shù)據(jù)庫無法連接!",
? ? ? ? ? ? ? ? ? ? "登錄失敗",
? ? ? ? ? ? ? ? ? ? JOptionPane.ERROR_MESSAGE);
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? rs=sql.executeQuery("select pwd from Login where id="+id);
? ? ? ? ? ? while(rs.next()){
? ? ? ? ? ? ? ? user=rs.getString(1);
? ? ? ? ? ? }
? ? ? ? ? ? rs.close();
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? return user.equals("")?null:user;
? ? }
}4、課程數(shù)據(jù)庫信息操作類:
package StuPickCouse_Dos.PickMysql;
import StuPickCouse_Dos.PickCouse.Couse;
import StuPickCouse_Dos.PickCouse.User;
import java.sql.*;
import java.util.ArrayList;
public class CouseSql {
? ? User user;
? ? public CouseSql(User user){
? ? ? ? this.user=user;
? ? }
? ? Connection con=null;
? ? Statement sql;
? ? ResultSet rs;
? ? void star(){
? ? ? ? String url="jdbc:mysql://localhost:3306/couse?user=root&password=&useSSL=true&useUnicode=true&characterEncoding=utf-8";
? ? ? ? String user="root";
? ? ? ? String passKey="";
? ? ? ? try {
? ? ? ? ? ? Class.forName("com.mysql.jdbc.Driver");
? ? ? ? ? ? con= DriverManager.getConnection(url,user,passKey);
? ? ? ? }
? ? ? ? catch(SQLException e){
? ? ? ? ? ? System.out.println(e);
? ? ? ? }catch (ClassNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public ArrayList<Couse> findCouse(String sqlLine){
? ? ? ? ArrayList<Couse> couses=new ArrayList<>();
? ? ? ? star();
? ? ? ? try {
? ? ? ? ? ? sql=con.createStatement();//
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? rs = sql.executeQuery(sqlLine);
? ? ? ? ? ? while (rs.next()) {
? ? ? ? ? ? ? ? couses.add(new Couse(rs.getString(1),
? ? ? ? ? ? ? ? ? ? ? ? rs.getString(2),
? ? ? ? ? ? ? ? ? ? ? ? rs.getString(3),
? ? ? ? ? ? ? ? ? ? ? ? rs.getString(4),
? ? ? ? ? ? ? ? ? ? ? ? rs.getString(5)
? ? ? ? ? ? ? ? ));
? ? ? ? ? ? }
? ? ? ? ? ? con.close();
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? return couses;
? ? }
? ? public boolean isMyCouse(String sqlLine){
? ? ? ? boolean couses=false;
? ? ? ? star();
? ? ? ? try {
? ? ? ? ? ? sql=con.createStatement();//
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? rs = sql.executeQuery(sqlLine);
? ? ? ? ? ? while (rs.next()) {
? ? ? ? ? ? ? ? couses=true;
? ? ? ? ? ? }
? ? ? ? ? ? con.close();
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? return couses;
? ? }
? ? public void chioseCouse(Couse couse) {
? ? ? ? star();
? ? ? ? try {
? ? ? ? ? ? sql=con.createStatement();//
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? sql.executeUpdate("update Subject set Noss=Noss+1 where subjectId="+couse.getId());
? ? ? ? ? ? sql.executeUpdate("INSERT INTO PickCouse VALUES ('"+this.user.getId()+"','"+couse.getId()+"')");
? ? ? ? ? ? con.close();
? ? ? ? } catch (SQLException throwables) {
? ? ? ? ? ? throwables.printStackTrace();
? ? ? ? }
? ? ? ? System.out.println("====>添加成功!");
? ? }
? ? public void addCouse(String id,String name,int x1,int x2,String tec) throws Exception{
? ? ? ? star();
? ? ? ? sql=con.createStatement();
? ? ? ? sql.executeUpdate("INSERT INTO Subject VALUES (\""+id+"\",\""+name+"\","+x1+","+x2+",\""+tec+"\")");
? ? ? ? con.close();
? ? }
}5、功能實現(xiàn)類:
package StuPickCouse_Dos;
import StuPickCouse_Dos.PickCouse.Couse;
import StuPickCouse_Dos.PickCouse.User;
import StuPickCouse_Dos.PickMysql.CouseSql;
import StuPickCouse_Dos.PickMysql.UserSql;
import java.util.ArrayList;
import java.util.Scanner;
public class Operator {
? ? User user;
? ? CouseSql couseSql;
? ? UserSql userSql;
? ? ArrayList<Couse> couses = new ArrayList<>();
? ? public void setUser(User user) {
? ? ? ? this.user = user;
? ? }
? ? public Operator(){}
? ? public Operator(User user) {
? ? ? ? this.user = user;
? ? ? ? couseSql=new CouseSql(user);
? ? ? ? userSql=new UserSql();
? ? }
? ? public void findAllCouse(){
? ? ? ? couses = couseSql.findCouse("select * from Subject");
? ? ? ? for (int i = 0; i < couses.size(); i++)
? ? ? ? ? ? System.out.println((i + 1) + " ?" + couses.get(i).toString());
? ? }
? ? public void selectCouse(){
? ? ? ? System.out.println("所有課程如下:");
? ? ? ? ArrayList<Couse> couseICanChose=new ArrayList<>();
? ? ? ? ArrayList<String> couseICanChoseId=new ArrayList<>();
? ? ? ? for (int i = 0; i < couses.size(); i++){
? ? ? ? ? ? System.out.print((i + 1) + " ?" + couses.get(i).toString());
? ? ? ? ? ? boolean tag=false,flage=false;
? ? ? ? ? ? System.out.println( ( tag= ( ( !(flage=couseSql.isMyCouse("select * from PickCouse where stuId="+user.getId()+" and couseId="+couses.get(i).getId() )
? ? ? ? ? ? )&& Integer.parseInt( couses.get(i).getCapacity() )
? ? ? ? ? ? ? ? ? ? >= Integer.parseInt( couses.get(i).getNum() ) ) ) )? " ?可選": flage?" ? ?已選":" ? 人員已滿");
? ? ? ? ? ? if(tag) {
? ? ? ? ? ? ? ? couseICanChose.add(couses.get(i));
? ? ? ? ? ? ? ? couseICanChoseId.add(couses.get(i).getId());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(couseICanChose.size() < 1)
? ? ? ? ? ? System.out.print("暫時沒有可選的課程!");
? ? ? ? else {
? ? ? ? ? ? System.out.println("====》可選課程有:");
? ? ? ? ? ? for (int i = 0; i < couseICanChose.size(); i++)
? ? ? ? ? ? ? ? System.out.println((i + 1) + " ?" + couseICanChose.get(i).toString());
? ? ? ? ? ? System.out.print("====>請輸入選擇的課程編號:");
? ? ? ? ? ? String couse = "";
? ? ? ? ? ? while(!couseICanChoseId.contains( couse = (new Scanner(System.in)).nextLine()))
? ? ? ? ? ? ? ? System.out.print("輸入不在選擇范圍,請重新輸入:");
? ? ? ? ? ? couseSql.chioseCouse(new Couse(couse));
? ? ? ? }
? ? }
? ? public void findMyCouse(){
? ? ? ? shoeArrayList(couseSql.findCouse("select * from Subject where subjectId in (select couseId from PickCouse where stuId="+user.getId()+")"));
? ? }
? ? public static void shoeArrayList(ArrayList<Couse> item){
? ? ? ? for (int i = 0; i < item.size(); i++)
? ? ? ? ? ? System.out.println((i + 1) + " ?" + item.get(i).toString());
? ? ? ? System.out.print("Tip: 按任意鍵繼續(xù):");
? ? ? ? (new Scanner(System.in)).next();
? ? }
? ? public boolean intoPwd(String pwd,String id){
? ? ? ? int inputTimes=2;
? ? ? ? while( !( userSql.findUser(id).equals((new Scanner(System.in)).nextLine()) )&&inputTimes>0)
? ? ? ? ? ? System.out.print("![第"+(3-inputTimes)+"次輸入]-密碼輸入錯誤:剩余輸入機會"+(inputTimes--)+"\n請重新輸入:");
? ? ? ? return inputTimes>=0;
? ? }
? ? public int show(){
? ? ? ? System.out.println("*****************************************");
? ? ? ? System.out.println("*****************************************");
? ? ? ? System.out.println("******** ? 1)查看課程信息 ? **************");
? ? ? ? System.out.println("******** ? 2)選課 ? ? ? ? ? *************");
? ? ? ? System.out.println("******** ? 3)查看個人選課 ? ?*************");
? ? ? ? System.out.println("*****************************************");
? ? ? ? System.out.print("請輸入選擇:");
? ? ? ? return (new Scanner(System.in)).nextInt();
? ? }
}6、程序入口:
package StuPickCouse_Dos;
import StuPickCouse_Dos.PickCouse.User;
import StuPickCouse_Dos.PickMysql.UserSql;
import java.util.Scanner;
public class Demo_1 {
? ? public static void main(String[] args) throws Exception {
? ? ? ? System.out.println("*****************************************");
? ? ? ? System.out.println("******** ? 學生選課管理-登錄 ? ***********");
? ? ? ? System.out.println("*****************************************");
? ? ? ? User user= new User();
? ? ? ? System.out.print("===》請輸入賬號:");
? ? ? ? String id="";
? ? ? ? int inputTimes=1;
? ? ? ? while(!(id=(new Scanner(System.in)).nextLine()).matches("[0-9]{9}"))
? ? ? ? ? ? System.out.print("![第"+(inputTimes++)+"次輸入]輸入錯誤:用戶賬號為你的學號\n請重新輸入:");
? ? ? ? System.out.print("====>請輸入密碼:");
? ? ? ? if((new Operator()).intoPwd("",(user=(new UserSql()).getUser(id)).getId())){
? ? ? ? ? ? System.out.println("用戶:"+user.getName()+",歡迎登錄!");
? ? ? ? ? ? while((inputTimes=(new Operator()).show())> 0 ) {
? ? ? ? ? ? ? ? switch (inputTimes) {
? ? ? ? ? ? ? ? ? ? case 1:(new Operator(user)).findAllCouse();break;
? ? ? ? ? ? ? ? ? ? case 2:(new Operator(user)).selectCouse();break;
? ? ? ? ? ? ? ? ? ? case 3:(new Operator(user)).findMyCouse();break;
// ? ? ? ? ? ? ? ? ? ?case 99:addCouse(new CouseSql(user));break;
? ? ? ? ? ? ? ? ? ? default:return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? System.out.print("按任意鍵返回主菜單:");
? ? ? ? ? ? ? ? (new Scanner(System.in)).next();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else{
? ? ? ? ? ? System.out.println("登錄失??!請重新登錄!");
? ? ? ? ? ? main(args);
? ? ? ? }
? ? }
// ? ?public static void addCouse(CouseSql sql) throws Exception{
// ? ? ? ?System.out.print("1)請輸入課程編號:");
// ? ? ? ?String id=(new Scanner(System.in)).nextLine();
// ? ? ? ?System.out.print("2)請輸入課程名稱:");
// ? ? ? ?String name=(new Scanner(System.in)).nextLine();
// ? ? ? ?System.out.print("3)請輸入已選課人數(shù):");
// ? ? ? ?int noss=(new Scanner(System.in)).nextInt();
// ? ? ? ?System.out.print("4)請輸入課程選課容量:");
// ? ? ? ?int cap=(new Scanner(System.in)).nextInt();
// ? ? ? ?System.out.print("5)請輸入課程教師姓名:");
// ? ? ? ?String tec=(new Scanner(System.in)).nextLine();
// ? ? ? ?sql.addCouse(id,name,noss,cap,tec);
// ? ? ? ?System.out.println("======》添加成功!");
// ? ? ? ?System.out.print("繼續(xù)輸入請按1,退出按其他鍵:");
// ? ? ? ?if((new Scanner(System.in)).nextLine()=="1"){
// ? ? ? ? ? ?addCouse(sql);
// ? ? ? ?}
// ? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入理解Java class文件格式_動力節(jié)點Java學院整理
對于理解JVM和深入理解Java語言, 學習并了解class文件的格式都是必須要掌握的功課2017-06-06
聊聊springboot2.2.3升級到2.4.0單元測試的區(qū)別
這篇文章主要介紹了springboot 2.2.3 升級到 2.4.0單元測試的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

