Java實現簡單的學生管理系統

在java控制檯上實現學生信息的管理

不懂可以+Q562004191交流表明來意
環境:ide + mysql
數據庫:
創建數據庫命令

create database ***;
use ***;
然後分別創建兩張表即可
(當初學的比較呆,建了兩個庫,可以自己改成一個庫)

學生的一張表
在這裏插入圖片描述

create table student
(
    id      int(10) auto_increment
        primary key,
    stuid   varchar(30)     null,
    stuname varchar(30)     null,
    pwd     varchar(30)     null,
    math    int default '0' null,
    eng     int default '0' null,
    java    int default '0' null,
    class   int default '0' null
)
    charset = utf8;

老師的一張表:
在這裏插入圖片描述

create table teacher
(
    id        int(10) auto_increment
        primary key,
    teacherid varchar(30)     null,
    pwd       varchar(30)     null,
    class     int default '0' null
)
    charset = utf8;

目錄建成後是這樣的:
在這裏插入圖片描述
實現的功能有:

  • 教師登陸
    1、學生信息添加
    2、學生信息查看
    3、學生信息刪除
    4、學生信息查詢
    5、學生信息排名
    6、教師修改密碼

  • 教師註冊

  • 學生登陸
    1、信息查看
    2、信息修改

  • 退出

denglu->studentdenglu

package StudentServer.denglu;

import StudentServer.mysql.JDBCUnity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;


public class studentdenglu {
    Map map = new HashMap();
    String pwd1 = null;
    String id1 = null;
    int ID ;
    public void read() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = JDBCUnity.getconnection("studentdata");
            ps = con.prepareStatement("select stuid,pwd from student where id>=?");
            ps.setObject(1, 0);

            rs = ps.executeQuery();

            while (rs.next()) {
                map.put(rs.getObject("stuid"),rs.getObject("pwd"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public int denglu() throws SQLException, ClassNotFoundException {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int f = 0;

        Scanner scanner = new Scanner(System.in);
        read();
        if (map.isEmpty()){
            System.out.println("\t\t\t\t目前還沒有學生!");
        }else {
            System.out.print("\t\t\t\t請輸入學號:");
            id1 = scanner.nextLine();
            Set set = map.keySet();
            Iterator it = set.iterator();
            while (it.hasNext()){
                if (it.next().equals(id1)){
                    while (true) {
                        System.out.print("\t\t\t\t請輸入密碼:");
                        pwd1 = scanner.nextLine();
                        if (pwd1.equals(map.get(id1))) {
                            con = JDBCUnity.getconnection("studentdata");
                            ps = con.prepareStatement("select id from student where stuid=? and pwd=?");
                            ps.setString(1, id1);
                            ps.setString(2, pwd1);
                            rs = ps.executeQuery();
                            while (rs.next())
                                ID = rs.getInt(1);
                            f = ID;
                            break;
                        }
                        System.out.println("\t\t\t\t密碼錯誤!!請重新輸入!!!");
                    }
                }
            }
        }
        if (f == 0){
            System.out.println("\t\t\t\t\t\t無此賬號!");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return f;
    }
}

denglu->tecaherdenglu

package StudentServer.denglu;

import StudentServer.mysql.JDBCUnity;
import StudentServer.server.TeacherDemo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

public class teacherdenglu {
    List<TeacherDemo> list = new ArrayList<>();

    public void read() {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = JDBCUnity.getconnection("teacherdata");
            ps = con.prepareStatement("select teacherid,pwd,class from teacher where id>=0");

            rs = ps.executeQuery();
            while (rs.next()) {
                TeacherDemo teacherDemo = new TeacherDemo(rs.getString("teacherid"),rs.getString("pwd")
                ,rs.getInt("class"));
                list.add(teacherDemo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public int denglu() throws InterruptedException {
        list.clear();
        read();
        int f = 0;
        String pwd1 = null;
        if (list.isEmpty()){
            System.out.println("\t\t\t\t目前還沒有教師註冊!");
        }else{
        Scanner scanner = new Scanner(System.in);
        System.out.print("\t\t\t\t請輸入賬號:");
        String id = scanner.nextLine();
            for (TeacherDemo t:list
                 ) {
                if (t.getId().equals(id)){
                    while (true) {
                        System.out.print("\t\t\t\t請輸入密碼:");
                        pwd1 = scanner.nextLine();
                        if (pwd1.equals(t.getPwd())) {
                            f = t.getClassid();
                            break;
                        }else {
                            System.out.println("\t\t\t\t密碼錯誤,請重新輸入!");
                        }
                    }
                }
            }

        if (f == 0) {
            System.out.println("\t\t\t\t無此賬號!");
            Thread.sleep(1500);
        }
    }
    return f;
    }
}

denglu->zhucetecaher

package StudentServer.denglu;

import StudentServer.menu.Checkid;
import StudentServer.mysql.JDBCUnity;

import java.sql.*;
import java.util.Scanner;

public class zhuceteacher {
    public void zhuce() {
        System.out.println("\t\t\t\t註冊教師賬號");
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = JDBCUnity.getconnection("teacherdata");

            ps = con.prepareStatement("insert into teacher (teacherid,pwd,class) values (?,?,?)");
            System.out.print("\t\t\t\t請輸入賬號:");
            String id;
            while (true) {
                id = scanner.nextLine();
                boolean f = new Checkid(id).checkteacher();
                if (f) {
                    System.out.println("\t\t\t\t賬號重複,請重新輸入!");
                    System.out.print("\t\t\t\t請輸入賬號:");
                }
                else break;
            }
            ps.setObject(1, id);
            System.out.print("\t\t\t\t請輸入密碼:");
            String pwd = scanner.nextLine();
            ps.setObject(2, pwd);
            System.out.print("\t\t\t\t\t班級:");
            int classid = scanner.nextInt();
            ps.setObject(3, classid);

            ps.execute();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

menu->Checkid

package StudentServer.menu;

import StudentServer.mysql.JDBCUnity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class Checkid {
    private String id;
    public Checkid(String id){
        this.id = id;
    }
    List<String> checklist = new ArrayList<>();
    public boolean checkstudent(){
        checklist.clear();
        boolean f = false;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = JDBCUnity.getconnection("studentdata");
            ps = con.prepareStatement("select stuid from student where id>=?");
            ps.setObject(1, 0);

            rs = ps.executeQuery();

            while (rs.next()) {
                checklist.add(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (String t:checklist
             ) {
            if (t.equals(id)){
                f = true;
                break;
            }
        }

        return f;
    }
    public boolean checkteacher(){
        checklist.clear();
        boolean f = false;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = JDBCUnity.getconnection("teacherdata");
            ps = con.prepareStatement("select teacherid from teacher where id>=?");
            ps.setObject(1, 0);

            rs = ps.executeQuery();

            while (rs.next()) {
                checklist.add(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (String t:checklist
                ) {
            if (t.equals(id)){
                f = true;
                break;
            }
        }

        return f;
    }

}

menu->Stumenu

package StudentServer.menu;

import StudentServer.mysql.JDBCUnity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

@SuppressWarnings("all")
public class Stumenu {
    int ID;
    public Stumenu(Integer ID){
        this.ID = ID;
    }
    public void menu() {

        int choice;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\t\t\t\t----------------1.信息查看----------------");
            System.out.println("\t\t\t\t----------------2.信息修改----------------");
            System.out.println("\t\t\t\t----------------0.退出    ----------------");
            System.out.print("\t\t\t\t\t\t請輸入你要進行的操作:");
            choice = scanner.nextInt();
            if (choice == 0) break;
            switch (choice) {
                case 1:
                    see();
                    break;
                case 2:
                    change();
                    break;
            }
        }
    }


    public void see(){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

            try {
                con = JDBCUnity.getconnection("studentdata");
                ps = con.prepareStatement("select * from student where id=?");
                ps.setInt(1,ID);
                rs = ps.executeQuery();
                System.out.println("*********************************************************************************************");
                while (rs.next()) {
                    int sum = rs.getInt(7)+rs.getInt(5)+rs.getInt(6);
                    System.out.println("\t\t|學號:" + rs.getString(2) + "|" + "姓名:" + rs.getString(3) + "|" + "密碼:" +
                            rs.getString(4) + "|" + "數學:" + rs.getInt(5) + "|" + "英語:" + rs.getInt(6) + "|" +
                            "Java:" + rs.getInt(7) + "|" + "Sum:" + sum+ "|"+ "班級:" + rs.getInt(8)+ "|");
                }
                System.out.println("*********************************************************************************************");
            } catch (Exception e) {
                e.printStackTrace();
            }

    }

    public void change() {
        int choice;
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

            try {
                con = JDBCUnity.getconnection("studentdata");

                    System.out.println("\t\t\t\t----------------1.修改姓名----------------");
                    System.out.println("\t\t\t\t----------------2.修改學號----------------");
                    System.out.println("\t\t\t\t----------------3.修改密碼----------------");
                    System.out.println("\t\t\t\t----------------0.退出    ----------------");
                    System.out.println("\t\t\t\t\t\t請輸入你要進行的操作:");
                    choice = scanner.nextInt();
                    scanner.nextLine();
                    switch (choice) {
                        case 1:
                            System.out.print("\t\t\t\t姓名改爲:");
                            String name = scanner.nextLine();
                            ps = con.prepareStatement("update student set stuname=? where id=ID;");
                            ps.setString(1,name);
                            ps.execute();
                            break;
                        case 2:
                            System.out.print("\t\t\t\t學號改爲:");
                            String id = scanner.nextLine();
                            ps = con.prepareStatement("update student set stuid=? where id=ID;");
                            ps.setString(1,id);
                            ps.execute();
                            break;
                        case 3:
                            System.out.print("\t\t\t\t密碼改爲:");
                            String pwd = scanner.nextLine();
                            ps = con.prepareStatement("update student set pwd=? where id=ID");
                            ps.setString(1,pwd);
                            ps.execute();
                            break;
                    }

            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

menu->Teachermenu

package StudentServer.menu;

import StudentServer.mysql.JDBCUnity;
import StudentServer.server.StudentDemo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@SuppressWarnings("all")
public class Teachermenu {
     private int classid;
     List< StudentDemo> list = new ArrayList<>();
     public Teachermenu(int classid){
         this.classid = classid;
     }


    public void menu() {
        int choice;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            read();
            System.out.println("\t\t\t\t----------------1.信息添加----------------");
            System.out.println("\t\t\t\t----------------2.信息查看----------------");
            System.out.println("\t\t\t\t----------------3.信息修改----------------");
            System.out.println("\t\t\t\t----------------4.信息刪除----------------");
            System.out.println("\t\t\t\t----------------5.信息查詢----------------");
            System.out.println("\t\t\t\t----------------6.成績排名----------------");
            System.out.println("\t\t\t\t--------------7.修改教師密碼--------------");
            System.out.println("\t\t\t\t----------------0.退出    ----------------");
            System.out.print("\t\t\t\t\t\t請輸入你要進行的操作:");
            choice = scanner.nextInt();
            if (choice == 0) break;
            switch (choice) {
                case 1:
                    add();
                    break;//添加學生信息
                case 2:
                    show();
                    break;
                case 3:
                    change();
                    break;//修改學生信息
                case 4:
                    delete();
                    break;//刪除學生信息
                case 5:
                    find();
                    break;//查詢學生信息
                case 6:
                    sort();
                    break;
                case 7:
                    changeteacher();
                    break;
            }
        }
    }
    public void changeteacher(){
        int choice;
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = JDBCUnity.getconnection("teacherdata");

            System.out.print("\t\t\t\t教師賬號:");
            String tid = scanner.nextLine();
            if (tid.equals("")){
                System.out.println("\t\t\t\t賬號不能爲空!");
                tid = scanner.nextLine();
            }
            System.out.print("\t\t\t\t密碼改爲:");
            String pwd = scanner.nextLine();
            if (pwd.equals("")){
                System.out.println("\t\t\t\t密碼不能爲空!");
                pwd = scanner.nextLine();
            }
            ps = con.prepareStatement("update teacher set pwd=? where teacherid=?;");
            ps.setString(1,pwd);
            ps.setString(2,tid);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("修改成功!");
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void find(){
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        System.out.print("\t\t\t\t請輸入要查詢的學生學號:");
        String id = scanner.nextLine();

        try {
            con = JDBCUnity.getconnection("studentdata");
            ps = con.prepareStatement("select * from student where stuid=?");
            ps.setString(1,id);
            rs = ps.executeQuery();
            System.out.println("*********************************************************************************************");
            while (rs.next()) {
                int sum = rs.getInt(7)+rs.getInt(5)+rs.getInt(6);
                System.out.println("\t\t\t\t|學號:" + rs.getString(2) + "|" + "姓名:" + rs.getString(3) + "|" + "密碼:" +
                        rs.getString(4) + "|" + "數學:" + rs.getInt(5) + "|" + "英語:" + rs.getInt(6) + "|" +
                        "Java:" + rs.getInt(7) + "|" + "Sum:" + sum+ "|"+"班級:" + rs.getInt(8) + "|");
            }
            System.out.println("*********************************************************************************************");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void change(){
        int choice;
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean f = false;
        System.out.print("\t\t\t\t請輸入學號:");
        String id = scanner.nextLine();
        for (StudentDemo t:list
             ) {
            if (t.getId().equals(id)){
                f = true;
                break;
            }
        }
        if (f == false){
            System.out.println("\t\t\t\t此老師名下無此學生!或者此學生沒有註冊!");
            return;
        }
        try {
            con = JDBCUnity.getconnection("studentdata");

            System.out.println("\t\t\t\t----------------1.修改姓名----------------");
            System.out.println("\t\t\t\t----------------2.修改學號----------------");
            System.out.println("\t\t\t\t----------------3.修改密碼----------------");
            System.out.println("\t\t\t\t----------------4.修改數學----------------");
            System.out.println("\t\t\t\t----------------5.修改英語----------------");
            System.out.println("\t\t\t\t----------------6.修改Java----------------");
            System.out.println("\t\t\t\t----------------7.修改班級----------------");
            System.out.println("\t\t\t\t----------------0.退出    ----------------");
            System.out.print("\t\t\t\t\t\t請輸入你要進行的操作:");
            choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    System.out.print("\t\t\t\t姓名改爲:");
                    String name = scanner.nextLine();
                    ps = con.prepareStatement("update student set stuname=? where stuid=? and class=?;");
                    ps.setString(1,name);
                    ps.setString(2,id);
                    ps.setInt(3,classid);
                    ps.execute();
                    break;
                case 2:
                    System.out.print("\t\t\t\t學號改爲:");
                    String id1 = scanner.nextLine();
                    ps = con.prepareStatement("update student set stuid=? where stuid=? and class=?;");
                    ps.setString(1,id1);
                    ps.setString(2,id);
                    ps.setInt(3,classid);
                    ps.execute();
                    break;
                case 3:
                    System.out.print("\t\t\t\t密碼改爲:");
                    String pwd = scanner.nextLine();
                    ps = con.prepareStatement("update student set pwd=? where stuid=? and class=?");
                    ps.setString(1,pwd);
                    ps.setString(2,id);
                    ps.setInt(3,classid);
                    ps.execute();
                    break;
                case 4:
                    System.out.print("\t\t\t\t數學改爲:");
                    String math1 = scanner.nextLine();
                    ps = con.prepareStatement("update student set math=? where stuid=? and class=?;");
                    ps.setString(1,math1);
                    ps.setString(2,id);
                    ps.setInt(3,classid);
                    ps.execute();
                    break;
                case 5:
                    System.out.print("\t\t\t\t英語改爲:");
                    String eng1 = scanner.nextLine();
                    ps = con.prepareStatement("update student set eng=? where stuid=? and class=?;");
                    ps.setString(1,eng1);
                    ps.setString(2,id);
                    ps.setInt(3,classid);
                    ps.execute();
                    break;
                case 6:
                    System.out.print("\t\t\t\tJava改爲:");
                    String java1 = scanner.nextLine();
                    ps = con.prepareStatement("update student set java=? where stuid=? and class=?;");
                    ps.setString(1,java1);
                    ps.setString(2,id);
                    ps.setInt(3,classid);
                    ps.execute();
                    break;
                case 7:
                    System.out.print("\t\t\t\t班級改爲:");
                    int classid1 = scanner.nextInt();
                    ps = con.prepareStatement("update student set class=? where stuid=? and class=?;");
                    ps.setInt(1,classid1);
                    ps.setString(2,id);
                    ps.setInt(3,classid);
                    ps.execute();
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sort(){

        int choice;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            read();
            System.out.println("\t\t\t\t----------------1.數學排名----------------");
            System.out.println("\t\t\t\t----------------2.英語排名----------------");
            System.out.println("\t\t\t\t----------------3.Java排名----------------");
            System.out.println("\t\t\t\t----------------4.總分排名----------------");
            System.out.println("\t\t\t\t----------------0.退出    ----------------");
            System.out.print("\t\t\t\t\t\t請輸入你要進行的操作:");
            choice = scanner.nextInt();
            if (choice == 0) break;
            switch (choice) {
                case 1:
                    Collections.sort(list, new Comparator<StudentDemo>() {
                        @Override
                        public int compare(StudentDemo o1, StudentDemo o2) {
                            return -1 * ((Integer)(o1.getMath())).compareTo((Integer)o2.getMath());
                        }
                    });
                    break;
                case 2:
                    Collections.sort(list, new Comparator<StudentDemo>() {
                        @Override
                        public int compare(StudentDemo o1, StudentDemo o2) {
                            return -1 * ((Integer)(o1.getEnglish())).compareTo((Integer)o2.getEnglish());
                        }
                    });
                    break;
                case 3:
                    Collections.sort(list, new Comparator<StudentDemo>() {
                        @Override
                        public int compare(StudentDemo o1, StudentDemo o2) {
                            return -1 * ((Integer)(o1.getJava())).compareTo((Integer)o2.getJava());
                        }
                    });
                    break;
                case 4:
                    Collections.sort(list, new Comparator<StudentDemo>() {
                        @Override
                        public int compare(StudentDemo o1, StudentDemo o2) {
                            return -1 * ((Integer)(o1.getSum())).compareTo((Integer)o2.getSum());
                        }
                    });
                    break;
            }
            show();
        }


    }

    public void show(){
        System.out.println("*********************************************************************************************");
        for ( StudentDemo t:list
             ) {
            System.out.println("\t\t|學號:"+t.getId()+"|"+"姓名:"+t.getName()+"|"+"密碼:"+
                    t.getPwd()+"|"+"數學:"+t.getMath()+"|"+"英語:"+t.getEnglish()+"|"+"Java:"+t.getJava()+
                    "|"+"Sum:"+t.getSum()+"|"+"Class:"+t.getClassid()+"|");
        }
        System.out.println("*********************************************************************************************");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void add() {
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        int choice;
        while (true) {
            try {
                con = JDBCUnity.getconnection("studentdata");
                ps = con.prepareStatement("insert into student (stuid,stuname,pwd,math,eng,java,class) values (?,?,?,?,?,?,?)");
                System.out.print("\t\t\t\t請輸入學生id:");
                String id;
                while (true) {
                    id = scanner.nextLine();
                    if (id.equals("")){
                        System.out.println("學號不能爲空!");
                        return;
                    }
                    boolean f = new Checkid(id).checkstudent();
                    if (f) {
                        System.out.println("\t\t\t\t學號重複,請重新輸入!");
                        System.out.print("\t\t\t\t請輸入學生id:");
                    }
                    else break;
                }
                ps.setObject(1, id);
                System.out.print("\t\t\t\t請輸入學生name:");
                String name = scanner.nextLine();
                if (name.equals("")){
                    System.out.println("\t\t\t\t姓名不能爲空!");
                    return;
                }
                ps.setObject(2, name);
                System.out.print("\t\t\t\t請輸入學生密碼:");
                String pwd = scanner.nextLine();
                if (pwd.equals("")){
                    System.out.println("\t\t\t\t密碼不能爲空!");
                    return;
                }
                ps.setObject(3, pwd);
                System.out.print("\t\t\t\t數學:");int math = scanner.nextInt();
                System.out.print("\t\t\t\t英語:");int eng = scanner.nextInt();
                System.out.print("\t\t\t\tJava:");int java = scanner.nextInt();
                ps.setInt(4,math);
                ps.setInt(5,eng);
                ps.setInt(6,java);
                int classid1 = classid;
                ps.setInt(7,classid1);

                ps.execute();

            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("\t\t\t\t添加學生,按0退出,按1繼續");
            choice = scanner.nextInt();
            if (choice == 0) break;
        }
    }

    public void delete() {
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
            try {
                con = JDBCUnity.getconnection("studentdata");
                System.out.println("\t\t\t\t按名稱刪除-->'1'    按學號刪除-->'2'");
                int choice = scanner.nextInt();
                if (choice == 1) {
                    ps = con.prepareStatement("delete from student where stuname=? and class=?");
                    System.out.println("\t\t\t\t請輸入學生name");
                    scanner.nextLine();
                    String name = scanner.nextLine();
                    if (name.equals("")){
                        System.out.println("\t\t\t\t姓名不能爲空!");
                        return;
                    }
                    ps.setObject(1, name);
                    ps.setObject(2, classid);
                }else {
                    ps = con.prepareStatement("delete from student where stuid=? and class=?");
                    System.out.println("\t\t\t\t請輸入學生id");
                    scanner.nextLine();
                    String id = scanner.nextLine();
                    if (id.equals("")){
                        System.out.println("\t\t\t\t學號不能爲空!");
                        return;
                    }
                    ps.setObject(1, id);
                    ps.setObject(2, classid);
                }

                ps.execute();

            } catch (Exception e) {
                e.printStackTrace();
            }

    }

    public void read() {
        list.clear();
        Scanner scanner = new Scanner(System.in);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = JDBCUnity.getconnection("studentdata");
            ps = con.prepareStatement("select stuid,stuname,pwd,math,eng,java,class from student where id>=? and class=?");
            ps.setObject(1, 0);
            ps.setInt(2,classid);
            rs = ps.executeQuery();

            while (rs.next()) {
                int sum = rs.getInt(4)+rs.getInt(5)+rs.getInt(6);
                StudentDemo stu =new StudentDemo(rs.getString(1),rs.getString(2),
                        rs.getString(3),rs.getInt(4),
                        rs.getInt(5),rs.getInt(6),sum,rs.getInt(7));

                list.add(stu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

mysql->JDBCUtil

package StudentServer.mysql;

import java.sql.*;

public class JDBCUnity {
    public static Connection getconnection(String type) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //建立連接
       return DriverManager.getConnection("jdbc:mysql://localhost:3306/"+type+"?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8","root","qqq1234");
    }
    public static void close(PreparedStatement ps, ResultSet rs,Connection con){
        try {
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close( ResultSet rs,Connection con){
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Connection con){
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

server->mainserver

package StudentServer.server;

import StudentServer.denglu.studentdenglu;
import StudentServer.denglu.teacherdenglu;
import StudentServer.denglu.zhuceteacher;
import StudentServer.menu.Teachermenu;
import StudentServer.menu.Stumenu;

import java.util.Scanner;

@SuppressWarnings("all")
public class mainserver {
    public static void main(String[] args) throws Exception {
        int choice;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\t\t\t\t----------------1.教師登陸----------------");
            System.out.println("\t\t\t\t----------------2.教師註冊----------------");
            System.out.println("\t\t\t\t----------------3.學生登陸----------------");
            System.out.println("\t\t\t\t----------------0.退出    ----------------");
            System.out.print("\t\t\t\t\t\t請輸入你要進行的操作:");
            choice = scanner.nextInt();
            if (choice == 0) break;
            switch (choice) {
                case 1:
                    dengluteacher();
                    break;
                case 2:
                    new zhuceteacher().zhuce();
                    break;
                case 3:
                    denglustudent();
                    break;
            }
        }


    }
    public static void dengluteacher() throws InterruptedException {
        int t = new teacherdenglu().denglu();
        if (t != 0) {
            Teachermenu teachermenu = new Teachermenu(t);
            teachermenu.menu();
        }
    }

    public static void denglustudent() throws Exception {
        int t = new studentdenglu().denglu();
        if (t != 0) {
            Stumenu stumenu = new Stumenu(t);
            stumenu.menu();
        }
    }
}

server->StudentDemo

package StudentServer.server;

public class StudentDemo {
    private String  id;
    private String name;
    private String pwd;
    private int Math = 0;
    private int Java = 0;
    private int English = 0;
    private int Sum;
    private int Classid;

    public StudentDemo(String id, String name, String pwd, int math, int china, int english, int sum,int Classid) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        Math = math;
        Java = china;
        English = english;
        Sum = sum;
        this.Classid = Classid;
    }

    public int getClassid() {
        return Classid;
    }

    public void setClass(int aClass) {
        Classid = aClass;
    }

    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 getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getMath() {
        return Math;
    }

    public void setMath(int math) {
        Math = math;
    }

    public int getJava() {
        return Java;
    }

    public void setJava(int china) {
        Java = china;
    }

    public int getEnglish() {
        return English;
    }

    public void setEnglish(int english) {
        English = english;
    }

    public int getSum() {
        return English+Math+Java;
    }

    public void setSum(int sum) {
        Sum = sum;
    }

    public StudentDemo(int math, int china, int english) {
        Math = math;
        Java = china;
        English = english;
    }


}

server->TeacherDemo

package StudentServer.server;

public class TeacherDemo {
    private String  id;
    private String pwd;
    private int classid;

    public TeacherDemo(String id, String pwd, int classid) {
        this.id = id;
        this.pwd = pwd;
        this.classid = classid;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getClassid() {
        return classid;
    }

    public void setClassid(int classid) {
        this.classid = classid;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章