這裏使用JAVA語言編寫的簡易的學生選課系統,展示的都是這個系統核心代碼。java
其中有不足歡迎批評和指正!算法
連接數據庫的代碼sql
package connection;
//鏈接數據庫student數據庫
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.Statement;
import java.sql.SQLException; sqlserver
public class xu{
private static Connection conn = null;
static {
try {
// 註冊驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 得到一個數據庫鏈接
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=student","sa","123456");
}catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
}設計
選課的操做代碼指針
package connection;orm
import java.sql.*;
import java.util.*;server
public class test1{
private static String ID; // 此處的ID設爲全局變量,在下面某些方法裏會用到
private static Scanner console = new Scanner(System.in);
private static String managerPass = "123456";
private static Connection conn = xu.getConnection();
public static void main(String [] args) throws SQLException {
System.out.println("Welcome to the student selection system!\n\n"
+"if you are maneger please choose 六、七、八、九、10\n"
+"if you are student Please choose 一、二、三、四、五、10\n");
ShowMenu();
}
public static void ShowMenu() throws SQLException {
System.out.println("-----------option-------------\n");
System.out.println("1--Student login"); // 學生登陸
System.out.println("2--Change password"); // 學生修改密碼
System.out.println("3--Choose course"); // 學生選課
System.out.println("4--Get course list"); // 學生查看本身的課程表
System.out.println("5--Student rigistration"); // 學生註冊帳號
System.out.println("6--Create student"); // 管理員建立學生
System.out.println("7--Create course"); // 管理員建立課表
System.out.println("8--Get student list"); // 管理員查看學生表
System.out.println("9--Get course list1"); // 管理員查看選課表裏的課程
System.out.println("10--exit\n"); // 退出
Test();
}
public static void Test() throws SQLException {
System.out.println("manager choose 1 ,student choose 0");
int choose1 = console.nextInt(); //int 用nextInt()
if(choose1 == 1) {
System.out.println("Please input your password :");
String password = console.next(); //String 用next()
if(password.equals(managerPass)) {
System.out.println("Welcome manager!\n");
ManagerMenu1();
}
else {
System.out.println("Your password is worry!"); }
}
else if(choose1 == 0) {
System.out.println("Welcome Student!");
StudentMenu1();
}
}
/*管理員操做菜單*/
public static void ManagerMenu1() throws SQLException {
System.out.println("----------Manager----------\n");
System.out.println("Please choose option");
int choose2 = console.nextInt();
switch(choose2) {
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
System.out.println("This is student`s option,try again!");
ManagerMenu1();
break;
case 6 : CreateStu();break;
case 7 : CreateCourse();break;
case 8 : GetStudentList();break;
case 9 : GetCourseList1();break;
case 10 : System.exit(0);break;
default : System.out.println("Please input an right number , try again!"); ManagerMenu1();
}
}
/*學生操做菜單*/
public static void StudentMenu1() throws SQLException {
System.out.println("----------Student----------\n");
System.out.println("Please input option");
int choose3 = console.nextInt();
switch(choose3) {
case 1 : StuLogin();break;
case 2 : ChangePass();break;
//在進行Choose course,Get course list以前都要求學生進行StuLogin操做
case 3 :
case 4 : System.out.println("you must login before you can do it!\n");StuLogin();break;
case 5 : StuRigistration();break;
case 6 :
case 7 :
case 8 :
case 9 : System.out.println("This is manager`s option,try again!");StudentMenu1();break;
case 10 : System.exit(0);break;
default : System.out.println("Please input an right number,try again!");StudentMenu1();
}
}對象
/*管理員建立課程*/
public static void CreateCourse() throws SQLException{
try {
/* 經過connection數據庫連接對象 建立一個statement對象數據庫操做對象,stat對象再獲取一個執行sql的語句。
* stat對象就是java程序與Database的一個鏈接通道*/
Statement stat = conn.createStatement();
String sql1 = "insert into course(couID,couName,couTeacher)values(1,'軟件工程','黃偉國')";
String sql2 = "insert into course(couID,couName,couTeacher)values(2,'數據庫','韋美雁')";
String sql3 = "insert into course(couID,couName,couTeacher)values(3,'算法設計','黎明')";
String sql4 = "insert into course(couID,couName,couTeacher)values(4,'大學英語','章琴')";
String sql5 = "insert into course(couID,couName,couTeacher)values(5,'Java','唐雅媛')";
int a = stat.executeUpdate(sql1);
int b = stat.executeUpdate(sql2);
int c = stat.executeUpdate(sql3);
int d = stat.executeUpdate(sql4);
int e = stat.executeUpdate(sql5);
int sum = a + b + c + d + e;
System.out.println("成功添加"+ sum + "行課程數據");
ManagerMenu1();
} catch (SQLException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("Course already exists");
ManagerMenu1();
}
}
/*管理員建立學生*/
public static void CreateStu() throws SQLException {
try {
Statement stat = conn.createStatement();
String sql1 = "insert into student(stuID,stuName) Values(20160501,'徐明正')";
// 這裏對學生信息的輸入也必須一個一個輸入,這是系統的缺陷,問題待解決
int a = stat.executeUpdate(sql1);
System.out.println("成功添加"+ a + "行學生數據");
ManagerMenu1();
}catch(SQLException e) {
// TODO Auto-generated catch block
System.out.println("The student already exists");
ManagerMenu1();
}
}
/*學生登陸*/
public static void StuLogin() throws SQLException {
System.out.println("Please input your stuID"); // 輸入帳號
ID = console.next();
System.out.println("Please input your Password"); // 輸入密碼
String stuPassword = console.next();
String stuID = null;
String Password = null;
Statement stat ;
ResultSet rs ;
try {
stat = conn.createStatement();
String sql1 = "select * from login "; /* 從login表中查詢其全部屬性值,則stuID 和 Password都有了。
rs是結果集。查詢出的記錄是一個列表,初始時指針指向的是第一條記錄以前的。*/
boolean flag;
rs = stat.executeQuery(sql1);
while(flag = rs.next()) { // rs.next(); //返回值爲true or false
stuID = rs.getString("stuID");
Password = rs.getString("Password");
if( ID.equals(stuID) && stuPassword.equals(Password)) // 注意這裏,跳出循環的條件很重要
break;
// 若是在數據庫裏沒有找到與用戶輸入的stuID和Password相匹配的stuID和Password,flag則變爲false
}
if(!flag){ // 當flag 爲 false 時
System.out.println("The ID or Password is worry!Please input your ID and Password again\n");
StuLogin();
}
else {
System.out.println("***"+ID+"***"+stuID +"***"+stuPassword+"***"+Password+"***");
System.out.println("Login Successful!\n");
System.out.println("------------------Please choose option------------------\n"
// +"If you want to Change Password ,Please choose 2\n"
+"If you want to Choose Course,Please choose 3\n"+"If you want to Get Course List,Please choose 4\n"
+"If you want to exit ,Please choose 8\n");
int choose4 = console.nextInt();
switch(choose4) {
// case 2 : ChangePass();break;
case 3 : ChooseCourse();break;
case 4 : GetCourseList();break;
case 10 : System.exit(0);break;
default : System.out.println("You input an invalid number!");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*在這裏說明一下 方法execute 、executeQuery、executeUpdate三者的區別
* 方法executeQuery 用於單個結果集的語句,如select。他會把查詢的結果放入ResultSet類對象中供使用。
* 方法executeUpdate 用於執行insert、update或delete語句<返回值爲整數>以及SQL DDL(數據定義語言),如create table 和 drop table <返回值爲零>。
* 方法execute 用於執行返回多個結果集、多個更新計數或兩者組合的語句。*/
/*學生修改密碼*/
public static void ChangePass() throws SQLException {
// System.out.println(ID);
/*此處的ID應該必需要從新輸入,不然當選擇對數據庫中某個ID更新的Password進行修改時,沒有對應的ID*/
System.out.println("Please input your ID");
String ID = console.next();
System.out.println("Please input your new Password");
String NewPassword = console.next();
Statement stat ;
try {
stat = conn.createStatement(); // 兩種寫法均可以
// stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql ="UPDATE login SET Password =" + "" + NewPassword + " where stuID ="+ ID;
//注意,此處的NewPassword 和 ID 沒有要用''括起來
// String sql1 ="UPDATE login SET Password = 111 where stuID = 20156022";
int a = stat.executeUpdate(sql);
System.out.println(a);
if(a != 0) {
System.out.println("成功修改"+a+"行記錄");
System.out.println("Your new Password is "+ NewPassword);
StudentMenu1();
}
else System.out.println("\n"+"Changing the Password is failed!");
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*學生選課*/
public static void ChooseCourse() throws SQLException {
System.out.println(ID);
String couID = null;
String couName = null;
String couTeacher = null;
Statement stat;
ResultSet rs;
try {
stat = conn.createStatement();
String sql = "select * from course";
rs = stat.executeQuery(sql);
System.out.println("--------可選課程的信息-------");
while(rs.next()) {
couID = rs.getString("couID");
couName = rs.getString("couName");
couTeacher = rs.getString("couTeacher");
System.out.println(couID + " " + couName + " " + couTeacher);
}
System.out.println("\n Please choose your course \n");
stat= conn.createStatement();
// 此處stuID固定位學生登陸時所用的ID,這樣能夠保證登陸本身的帳號卻能選別的帳號的課程
System.out.println("input CourseID");
int ID1 = console.nextInt();
int ID2 = console.nextInt();
int ID3 = console.nextInt();
// 這樣寫 ,學生選課的數目必須固定爲3門,問題待解決
String sql1 = "insert into stuCourse(stuID,couID1,couID2,couID3)VALUES(" + ID + ","+ ID1 + "," + ID2 + "," +ID3 +")";
int i = stat.executeUpdate(sql1);
if(i != 0) {
System.out.println("成功選入課程");
GetCourseList();
}
else System.out.println("選課錯誤");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*學生獲取已選課程列表*/
public static void GetCourseList() throws SQLException {
System.out.println("\nHere is your course that you have choose\n");
Statement stat = null;
ResultSet rs = null;
try {
stat = conn.createStatement();
String sql = "select * from stuCourse ";
rs = stat.executeQuery(sql);
String stuID = null;
String couID1 = null;
String couID2 = null;
String couID3 = null;
String couID4 = null;
while(rs.next()) {
stuID = rs.getString("stuID");
couID1 = rs.getString("couID1");
couID2 = rs.getString("couID2");
couID3 = rs.getString("couID3");
couID4 = rs.getString("couID4");
if(stuID.equals(ID))
System.out.println(stuID + " " + couID1 + " " + couID2 + " " + couID3 + " " + couID4 + "\n");
}
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*學生註冊*/
public static void StuRigistration() throws SQLException{
System.out.println("Please input your ID ");
String ID = console.next();
System.out.println("Please input your Password");
String Password = console.next();
try {
Statement stat = conn.createStatement();
String sql1 = "insert into login(stuID,Password)Values(" + ID + ", " + Password + ")";
//這裏密碼只能輸入數字,問題待解決
int a = stat .executeUpdate(sql1);
System.out.println("成功註冊"+ a +"個帳號");
StudentMenu1();
}catch(SQLException e) {
// TODO Auto-generated catch block
System.out.println("ID already exists");
}
}
/* 管理員查看學生表 */
public static void GetStudentList() {
System.out.println("\n Here is student` information \n");
Statement stat = null;
ResultSet rs = null;
try {
stat = conn.createStatement();
String sql = "select * from student ";
rs = stat.executeQuery(sql);
String stuID = null;
String stuName = null;
while(rs.next()) { stuID = rs.getString("stuID"); stuName= rs.getString("stuName"); System.out.println(stuID + " " + stuName + "\n"); ManagerMenu1(); } }catch(SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* 管理員查看選課表裏的課程*/ public static void GetCourseList1() { System.out.println("\n Here is course`information \n"); Statement stat = null; ResultSet rs = null; try { stat = conn.createStatement(); String sql = "select * from course "; rs = stat.executeQuery(sql); String couID = null; String couName = null; String couTeacher = null; while(rs.next()) { couID = rs.getString("couID"); couName = rs.getString("couName"); couTeacher = rs.getString("couTeacher"); System.out.println(couID + " " + couName + " " + couTeacher + "\n"); ManagerMenu1(); } }catch(SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }