记录足迹---java小型教务系统设计(GUI+Mysql数据库)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30780133/article/details/80726307

本人目前大一计算机专业,一年即将结束了,想给自己留下一点东西,所以趁着期末考试复习期间有时间来完成目前对我来说最大的一个工程吧--教务管理系统,主要也是为了熟悉使用数据库和以前学过的一些知识,项目目前还在完成中,所以博客会一直更新到完成为止。

时间:2018/6/18  20:38

使用开发工具:Mysql数据库和idea(或者eclipse)

以前我是用的elipse不过用了idea后爱上了它,idea是使用的破解版(穷学生用不起正版,只能盗版了),直接去官网下载就行,破解教程在这里:idea破解教程转载https://www.jianshu.com/p/ad3830095fb3,不过有钱人还是尽量用正版滴;

Mysql直接百度官网下载然后看教程:Mysql安装配置链接https://www.cnblogs.com/xch-yang/p/7661069.html

在正式开发之前借鉴了一下这位大哥的博客:https://blog.csdn.net/qq_38330148/article/details/78785842

我自己在开发之前也写了一份主要的功能需求如下:
功能需求:
1.登录模块 
·学生登录(已完成)
·教师登录(管理学生信息)
·管理员登录(管理所有人信息)
·忘记密码,注册,重置 
2.管理模块
学生:
·查看班级信息
·查看个人信息 
·查看课表信息
·查看成绩信息
教师:        
·查看个人信息
·修改学生信息   
·增加学生信息
管理员:
·可以修改且查看全部人信息  
通用模块
·注销退出
·修改密码

一切都准备就绪后就进入正式的开发了

1.首先是数据库建表

一共建立了五张表:分别是学生,课程,成绩,教师,管理员

                

CREATE TABLE stuinfo		  /*学生表*/
(
   id int(11) NOT NULL,       /*学号*/
   name char(25) NOT NULL,    /*姓名*/
   sex char(25) NOT NULL,     /*性别*/
   city char(25) NOT NULL,    /*籍贯*/
   birthday char(25) NOT NULL,/*出生日期*/
   major char(25) NOT NULL,   /*专业*/
   user char(25) NOT NULL,    /*登录账号*/
   PRIMARY KEY(id)	      /*将id设为主键*/
);
CREATE TABLE lesson	    /*课程表*/
(
   id int(11) NOT NULL,       /*课程号*/
   name char(25) NOT NULL,    /*课程名字*/
   teacher char(25) NOT NULL,  /*授课教师*/
   credit char(25) NOT NULL,    /*学分*/
   PRIMARY KEY(id)	        /*将id设为主键*/
);
CREATE TABLE teacher          /*教师表*/
(
   id int(11) NOT NULL,       /*教授课程名*/
   name char(25) NOT NULL,    /*姓名*/
   sex char(25) NOT NULL,     /*性别*/
   user char(25) NOT NULL,    /*登录账号*/
   PRIMARY KEY(id)			  /*将id设为主键*/
);
CREATE TABLE score                    /*成绩表*/
(
   student_id int(11) NOT NULL,       /*学号*/
   course_id char(25) NOT NULL,       /*课程号*/
   grade char(25) NOT NULL,           /*成绩*/
   PRIMARY KEY(student_id),	      /*将学号设为主键*/
   PRIMARY KEY(course_id)	      /*将课程号设为主键*/
);
CREATE TABLE user
(
   id int(11) NOT NULL,       /*登录账号*/
   password char(25) NOT NULL,/*登录密码*/
   PRIMARY KEY(id)	      /*将id设为主键*/
);

今天告一段落再见再见,写的时候发生了好几次网页未响应,偏偏我又没保存,导致我重写了好几遍大哭大哭,过两天考试完接着写

 

1.登录功能的实现:

  先来看一下效果图:

效果图就是这样的,不过我还进行了一些样式改变,首先就是没有采用Java默认的GUI风格,而是用了一个好像是09年出的一个风格

代码如下,只要加入这一行代码就可以了,也不止这一种风格,想换的话可以去网上找一下。

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

登录模块总体来说还是比较简单的,就是对一些组件的整合,然后设置合适的大小既可以了

有几个需要注意的

      1.窗口改变背景图,这是我封装的一个改变背景图的方法。这个原理就是将JFrame的       contentPan(本质上即使一个JLabel)层设置为透明的,然后在JFrame上添加一个添加了背景图片的JLabel,label大小设置与frame重合,这是JFrame窗口View的分层

         

 /**
     * 设置窗口的背景颜色
     * @param frame 窗口
     * @param filename 图片路径
     */
    public static void backGroup(JFrame frame, String filename){
        ImageIcon background = new ImageIcon(filename);
        JLabel label = new JLabel(background);
        label.setBounds(0, 0, frame.getWidth(), frame.getHeight());
        JPanel imagePanel =  (JPanel) frame.getContentPane();
        imagePanel.setOpaque(false);
        frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
    }
}

         2.数据库的封装,因为这个系统肯定是会经常用到数据库增删改查,所以建议将数据库封装成一个类,这样就不必写重复的代码了(,我是做完后才想起要封装的,所以我这里就没有那样封装)

 一些和登录有关的代码

 /**
     * 连接到数据库
     * @return Connection
     */
    public static Connection getconnect(){
        String url = "jdbc:mysql://localhost/student?user=root&password=jiang.141201&useSSL=false";
        Connection connection = null;
        try {
            connection = (Connection) DriverManager.getConnection(url);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    
 /**
     * 判断是老师还是学生登录
     * @param user 用户名
     * @return
     * @throws SQLException
     */
    public boolean isTeacher(String user) throws SQLException {
        Connection connection = getconnect();
        String sql = "select * from teacher where user=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,user);
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()){
            getName = resultSet.getString(2);
            courseId = resultSet.getString(1);
            return true;
        }else{
            return false;
        }
    }

/**
     * 判断账号密码是否正确
     * @return true or false
     */
    public boolean isLogin(){
        Connection connection = getconnect();
        String user = UserFile.getText().trim();
        String password = new String(PassWordFile.getPassword());
        String sql = "select * from user where user ='" +user+"'and password='"+password.trim()+"'";
        Statement statement = null;
        ResultSet resultSet = null;
        try {
             statement = connection.createStatement();
             resultSet = statement.executeQuery(sql);
             if(resultSet.next()){
                 getUser = user;
                 getPassword = password;
                 return true;
             }else {
                 return false;
             }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

3.这里就是判断登录,先是判断账号密码是否正确,然后再查询判断是老师账号还是学生账号,登陆后分别进入不同的界面

 if(label.equals("登录")){
            boolean istrue = isLogin();
            if(UserFile.getText().equals("") || PassWordFile.getPassword().equals("")){
                JOptionPane.showMessageDialog(null,"用户名或密码不能为空,请重新输入",
                        "警告",JOptionPane.WARNING_MESSAGE);
            } else if(istrue){
                if(Loginselection.getSelectedItem().equals("学生")){
                    try {
                        boolean isTeacher = isTeacher(UserFile.getText().trim());
                        if(!isTeacher){
                            getStudentName(UserFile.getText().trim());
                            new StuManangeFrame();
                            this.dispose();
                        }else{
                            JOptionPane.showMessageDialog(null,"权限错误!",
                                    "警告",JOptionPane.WARNING_MESSAGE);
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }else if (Loginselection.getSelectedItem().equals("教师")){
                    try {
                        boolean isTeacher = isTeacher(UserFile.getText().trim());
                        if(isTeacher){
                            try {
                                new TeacherFrame();
                                this.dispose();
                            } catch (Exception e1) {
                                e1.printStackTrace();
                            }
                        }else{
                            JOptionPane.showMessageDialog(null,"权限错误!",
                                    "警告",JOptionPane.WARNING_MESSAGE);
                        }
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }
            }else{
                JOptionPane.showMessageDialog(null, "用户名或者密码错误,请重新输入",
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        }

登录功能基本上就是这样的了,还有一些其他的小功能可以自己很轻松的完成。

2.学生界面

     主要就五个功能:1.查看自己个人的信息

                                  2.查看自己班级信息

                                  3.查看自己个人选课

                                  4.查看自己个人成绩

                                  5.修改自己个人密码

        先上效果图把

                  

                                   

                                            

学生界面的完成很简单就是一些设计样式然后加上监听,然后就是获取学生信息和班级信息,主要要完成的就是用一个二维字符串数组(用ArrayList更好,不用考虑大小)接收数据库里面查询的信息,还有一个就是对班级信息进行分页(重点)

  1.这是个人信息的查询

public static String[][] getMessage() throws SQLException {
        Connection connection = getConnection();
        String sql = "select * from stuinfo where stuinfo.user='"+getUser+"'";
        Statement Statement = (Statement) connection.createStatement();
        ResultSet resultSet = Statement.executeQuery(sql);

        while(resultSet.next()){
            id =resultSet.getInt(1);
            name = resultSet.getString(2);
            sex = resultSet.getString(3);
            city = resultSet.getString(4);
            birthday = resultSet.getString(5);
            major = resultSet.getString(6);
        }
        String[][] stuImforation = {{String.valueOf(id),name, sex, city, birthday, major}};
        return  stuImforation;
    }

2.对班级信息的查询

分页查询

分页查询首先就是设置好每一页的大小,然后就是就计算出一共有多少页

  计算出有多少页

//获取总信息的条数和总页数
    public void getSelectCount() throws SQLException {
        count = 0;
        Connection connection = StuImformationFrame.getConnection();
        String sql = "select * from stuinfo";
        Statement Statement = (Statement) connection.createStatement();
        ResultSet resultSet = Statement.executeQuery(sql);
        while(resultSet.next()){
            count++;
        }
        //计算页数,如果不能整除总页数则要额外+1  
        if(count % max_size != 0){
            pageCount = (count / max_size) +1;
        }else{
            pageCount = count / max_size;
        }
    }

分页数据的查询就是有好几种方式这里采用的就是mysql数据库自带的分页查询语句

/首先按照id顺序排序,然后查询从下标x开始,一共查询y条数据
select * from stuinfo order by id asc limit x, y

具体代码

  //获取一页的数据信息
       public static String[][] classImformation() throws Exception{
           Connection connection = StuImformationFrame.getConnection();
           String sql = "select * from stuinfo order by stu_id asc limit " + max_size * count2 +", 10";
           Statement Statement = (Statement) connection.createStatement();
           ResultSet resultSet = Statement.executeQuery(sql);
           String id = null,name=null,sex = null,city = null,birthday = null,major = null;
           String[][] imformation = new String[40][6];
           int i = 0;
           while(resultSet.next()){
               id = String.valueOf(resultSet.getInt(1));
               name = resultSet.getString(2);
               sex = resultSet.getString(3);
               city = resultSet.getString(4);
               birthday = resultSet.getString(5);
               major = resultSet.getString(6);
               imformation[i][0]=id;
               imformation[i][1]=name;
               imformation[i][2]=sex;
               imformation[i][3]=city;
               imformation[i][4]=birthday;
               imformation[i][5]=major;
               i++;
           }
           return imformation;
    }

未完待续....................................

猜你喜欢

转载自blog.csdn.net/qq_30780133/article/details/80726307