Java实现简单的学生管理系统

在java控制台上实现学生信息的管理

环境: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;
    }
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值