Java實(shí)現(xiàn)學(xué)生選課管理系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)學(xué)生選課管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
需求分析
本數(shù)據(jù)庫的用戶主要是學(xué)生,通過對用戶需求的收集和分析,獲得用戶對數(shù)據(jù)庫的如下要求。
1.信息需求
1.學(xué)生信息:學(xué)號,姓名,性別,專業(yè)
2.登陸信息:賬號,密碼
3.課程信息:課程號,課程名,選課人數(shù),選課容量,任課老師
4.選課信息:課程號,學(xué)生學(xué)號
5.登錄信息:賬號、密碼
2.功能需求
1.系統(tǒng)為學(xué)生建立登陸信息,學(xué)生進(jìn)入系統(tǒng)前需要身份驗(yàn)證,用戶名、密碼輸入正確后方可進(jìn)入系統(tǒng)。
2.在系統(tǒng)中,用戶可以在界面中看到本人的基本信息,也可以對課程信息表和個(gè)人選課信息表進(jìn)行查看、以及選課。
3.使用數(shù)據(jù)庫存儲讀取數(shù)據(jù)內(nèi)容
3.系統(tǒng)需求
學(xué)生信息管理系統(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è)計(jì)項(xiàng)目結(jié)構(gòu)
1.項(xiàng)目包結(jié)構(gòu)
2.項(xiàng)目UML類圖
運(yùn)行效果展示
1.登錄
2.查看全部課程信息
3.查看個(gè)人選課信息
4.學(xué)生選課
開放源代碼
1、學(xué)生用戶信息類:
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、學(xué)生數(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、功能實(shí)現(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("暫時(shí)沒有可選的課程!"); ? ? ? ? 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)+"次輸入]-密碼輸入錯(cuò)誤:剩余輸入機(jī)會"+(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)查看個(gè)人選課 ? ?*************"); ? ? ? ? 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("******** ? 學(xué)生選課管理-登錄 ? ***********"); ? ? ? ? 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++)+"次輸入]輸入錯(cuò)誤:用戶賬號為你的學(xué)號\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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用spring boot如何快速啟動一個(gè)web項(xiàng)目詳解
這篇文章主要給大家介紹了關(guān)于利用spring boot如何快速啟動一個(gè)web項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧、2017-12-12深入理解Java class文件格式_動力節(jié)點(diǎn)Java學(xué)院整理
對于理解JVM和深入理解Java語言, 學(xué)習(xí)并了解class文件的格式都是必須要掌握的功課2017-06-06深入學(xué)習(xí)Hibernate持久化對象的三個(gè)狀態(tài)
Hibernate中的對象有3中狀態(tài),瞬時(shí)對象(TransientObjects)、持久化對象(PersistentObjects)和離線對象(DetachedObjects也叫做脫管對象),下面通過本文給大家分享Hibernate持久化對象的三個(gè)狀態(tài),一起看看吧2017-09-09聊聊springboot2.2.3升級到2.4.0單元測試的區(qū)別
這篇文章主要介紹了springboot 2.2.3 升級到 2.4.0單元測試的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10