服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JavaSwing实现小型学生管理系统

JavaSwing实现小型学生管理系统

2022-07-28 11:43一夜星尘 Java教程

这篇文章主要为大家详细介绍了JavaSwing实现小型学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JavaSwing实现小型学生管理系统的具体代码,供大家参考,具体内容如下

在项目中建立四个包,分别是com.wu.JavaBean、com.wuJavaDao、com.wu.JavaService、com.wu.JavaView

数据库表结构

学生表只有四个属性:学生姓名、学生性别、学生学号(主键)、学生班级
管理员表只有两个属性:管理员用户名(主键)、管理员密码

这里笔者为了简单,学生表只写了四个属性,管理员表只写了两个属性。

在JavaBean新建Student和Root类,如下:

Student.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.wu.JavaBean;
/**
 
* @date 2020年12月15日下午9:49:51
* @author 一夜星尘
 */
public class Student {
    private String name;
    private String gender;
    private String id;
    private String team;
    public Student() {}
    public Student(String name,String gender,String id,String team) {
        this.name = name;
        this.id = id;
        this.team = team;
        this.gender = gender;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTeam() {
        return team;
    }
    public void setTeam(String team) {
        this.team = team;
    }
}

Root.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.wu.JavaBean;
/**
 
* @date 2020年12月15日下午9:50:30
* @author 一夜星尘
 */
public class Root {
    private String username; // 账号
    private String password; // 密码
    private String  superroot ; // 超级管理员身份 唯一一个
    public Root(String username) {
        this.username = username;
    }
    public Root(String username,String password,String superroot) {        
        this.username = username;
        this.password = password;
        this.superroot = superroot;
    }
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
    public boolean isSuperRoot() {
        return superroot.equals("1"); // 1代表超级管理员
    }
}

建立数据库连接DAO层,即在JavaDao包下建立JDBC.java,该程序只是控制数据库的连接:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.wu.JavaDao;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
 
/**
* @date 2020年12月15日下午9:58:11
* @author 一夜星尘
*/
public class JDBC {
    private Connection sqllink = null;
    /**
     * 获取数据库连接对象
     * @return
     * @throws Exception
     */
    public  Connection  getConnection()  throws Exception{
        String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver"
        String DATABASE_URL = "jdbc:mysql://127.0.0.1:3306/jdbc_db"+
        "?charcterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"
        String DATABASE_USER = "root";
        String DATABASE_PASSWORD = "root";
        
        try {
            Class.forName(DATABASE_DRIVER); // 注册驱动
            sqllink = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
            return this.sqllink;
        }catch(SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库异常"); // 错误信息显示到控制台
            return this.sqllink;
        }
    }
    /**
     * 关闭数据库连接对象
     * @throws Exception
     */
    public void closeConnection() throws Exception{
        try {
            if(this.sqllink != null) {
                this.sqllink.close();
            }
        }catch(SQLException e) {
            System.out.println("关闭数据库连接异常");
        }
    }
}

com.wu.JavaBean和com.wu.JavaDao已经全部完成了,接下来就是完成业务逻辑JavaService包下的实现

对于增添数据的业务方法Add.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.wu.JavaService;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
import com.wu.JavaBean.Root;
import com.wu.JavaBean.Student;
import com.wu.JavaDao.JDBC;
 
/**
* @date 2020年12月15日下午9:59:09
* @author 一夜星尘
*/
public class Add {
    /**
     * 添加信息
     * @param element 学生或者管理员
     * @return
     * @throws Exception
     */
    public static boolean add(Object  element) throws Exception{ // 多态
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        if(element instanceof Student) {
            String sql = "insert into student(name,gender,id,team) values(?,?,?,?)"; // mysql插入语句
            Student student  = (Student) element; // 向下转型
            try {
                sqlaction = sqllink.prepareStatement(sql); // 操作对象
                sqlaction.setString(1,student.getName());
                sqlaction.setString(2,student.getGender());
                sqlaction.setString(3,student.getId());
                sqlaction.setString(4,student.getTeam());
                
                int count = sqlaction.executeUpdate(); // 执行操作
                return (count == 1) ? true : false;
            }catch(SQLException e) {
                return false;
            }finally{
                jdbc.closeConnection(); // 关闭数据库连接
                if(sqlaction != null) {
                    sqlaction.close();
                }
            }
        }else if(element instanceof Root) {
            
            
            String sql = "insert into root(username,password,superroot) values(?,?,0)"; // mysql插入语句
            Root root  = (Root) element; // 向下转型
            // 超级管理员权限
            if(!root.isSuperRoot()) {
                return false;
            }
            
            try {
                sqlaction = sqllink.prepareStatement(sql); // 操作对象
                sqlaction.setString(1,root.getUsername());
                sqlaction.setString(2,root.getPassword());
                
                int count = sqlaction.executeUpdate(); // 执行操作
                return (count == 1) ? true : false;
            }catch(SQLException e) {
                return false;
            }finally{
                jdbc.closeConnection();  // 关闭数据库连接
                if(sqlaction != null) {
                    sqlaction.close();
                }
            }
        }else {
            System.out.println("对象传入错误");
            return false;
        }
    }
}

对于删除Remove.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.wu.JavaService;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
import com.wu.JavaBean.Root;
import com.wu.JavaBean.Student;
import com.wu.JavaDao.JDBC;
 
/**
* @date 2020年12月15日下午10:00:30
* @author 一夜星尘
*/
public class Remove {
    /**
     * 移除学生信息
     * @param student 待移除的学生
     * @param pos 移除方式
     * @return
     * @throws Exception
     */
    public static boolean removeStudent(Student student ,String username,int pos) throws Exception{ // 部分修改或者全部修改
        // 权限判断 只有超级管理员才能实现全部学生删除
        if (pos == 0 && !Find.getAccess(username).equals("1")) {
            return false;
        }
        
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        String sql = "";
        String[] info = new String[4];
        /**
         * 0代表删除所有学生
         * 1代表删除所有姓名为name的学生
         * 2代表删除所有性别为gender的学生
         * 3代表删除一个学号为id的学生
         * 4代表删除所有班级为team的学生
         * 5代表删除所有姓名为name性别为gender的学生
         * 6代表删除一个学号为id姓名为name的学生
         * 7代表删除所有姓名为name的班级为team的学生
         * 8代表删除性别为gender学号为id的一个学生
         * 9代表删除所有性别为gender班级为team的学生
         * 10代表删除一个学号为id班级为team的学生
         * 11代表删除一个姓名为name性别为gender学号为id的学生
         * 12代表删除所有姓名为name性别为gender班级为team的学生
         * 13代表删除删除一个姓名为name学号为id班级为team的学生
         * 14代表删除一个性别为gender学号为id班级为team的学生
         * 15代表删除一个姓名为name性别为gender学号为id班级为team的学生
         */
        switch(pos) {
        case 0:
            sql = "delete from student";
            try {
                sqlaction = sqllink.prepareStatement(sql);
                sqlaction.executeUpdate();
                return true;
            }catch(SQLException e) {
                e.printStackTrace();
                return false;
            }finally {
                jdbc.closeConnection();
                if(sqlaction != null) {
                    sqlaction.close();
                }
            }
        case 1:
            sql = "delete from student where name = ?";
            info[0] = student.getName();
            break;
        case 2:
            sql = "delete from student where gender = ?";
            info[0] = student.getGender();    
            break;
        case 3:
            sql = "delete from student where id = ?";
            info[0] = student.getId();
            break;
        case 4:    
            sql = "delete from student where team = ?";
            info[0] = student.getTeam();
            break;
        case 5:
            sql = "delete from student where name = ? and gender = ?";
            info[0] = student.getName();
            info[1] = student.getGender();
            break;
        case 6:
            sql = "delete from student where name = ? and id = ?";
            info[0] = student.getName();
            info[1] = student.getId();
            break;
        case 7:
            sql = "delete from student where name = ? and team = ?";
            info[0] = student.getName();
            info[1] = student.getTeam();
            break;
        case 8:
            sql = "delete from student where gender = ? and id = ?";
            info[0] = student.getGender();
            info[1] = student.getId();
            break;
        case 9:
            sql = "delete from student where gender = ? and team = ?";
            info[0] = student.getId();
            info[1] = student.getTeam();
            break;
        case 10:
            sql = "delete from student where id = ? and team = ?";
            info[0] = student.getName();
            info[1] = student.getGender();
            break;
        case 11:
            sql = "delete from student where name = ? and gender = ? and id = ?";
            info[0] = student.getName();
            info[1] = student.getGender();
            info[2] = student.getId();
             break;
        case 12:
            sql = "delete from student where name = ? and gender = ? and team = ?";
            info[0] = student.getName();
            info[1] = student.getGender();
            info[2] = student.getTeam();
            break;
        case 13:
            sql = "delete from student where name = ? and id = ? and team = ?";
            info[0] = student.getName();
            info[1] = student.getId();
            info[2] = student.getTeam();
            break;
        case 14:
            sql = "delete from student where gender = ? and id = ? and team = ?";
            info[0] = student.getGender();
            info[1] = student.getId();
            info[2] = student.getTeam();
            break;
        case 15:
            sql = "delete from student where name = ? and gender = ? and id = ? and team = ?";
            info[0] = student.getName();
            info[1] = student.getGender();
            info[2] = student.getId();
            info[3] = student.getTeam();
        }
    
        try {
            sqlaction = sqllink.prepareStatement(sql);
            switch(pos) {
            case 1:
            case 2:
            case 3:
            case 4:
                sqlaction.setString(1, info[0]);
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                sqlaction.setString(1, info[0]);
                sqlaction.setString(2, info[1]);
                break;
            case 11:
            case 12:
            case 13:
            case 14:
                sqlaction.setString(1, info[0]);
                sqlaction.setString(2, info[1]);
                sqlaction.setString(3, info[2]);
                break;
            case 15:
                sqlaction.setString(1, info[0]);
                sqlaction.setString(2, info[1]);
                sqlaction.setString(3, info[2]);
                sqlaction.setString(4, info[3]);
                break;
            }
            sqlaction.executeUpdate();
            return true;
        }catch(SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            jdbc.closeConnection();
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
                
    }
    /**
     * 删除管理员信息
     * @param root 待删除管理员
     * @return
     * @throws Exception
     */
    public static boolean removeRoot(Root root) throws Exception{ // 完全删除
        // 权限判断
        if(!root.isSuperRoot()) {
            return false;
        }
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        
        String sql = "delete from root where username = ? ";
        try {
            sqlaction  = sqllink.prepareStatement(sql);
            sqlaction.setString(1,root.getUsername());
            
            int count = sqlaction.executeUpdate();
            return count == 1?true : false;
        }catch(SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            jdbc.closeConnection();
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
    }
}

对于查找Find.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.wu.JavaService;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
 
import com.wu.JavaBean.Root;
import com.wu.JavaBean.Student;
import com.wu.JavaDao.JDBC;
 
/**
* @date 2020年12月15日下午10:01:05
* @author 一夜星尘
*/
public class Find {
    /**
     * 查找学生信息
     * @param student 待查找的学生
     * @param pos 查找方式
     * @return
     * @throws Exception
     */
    public static ArrayList<Student> findStduent(Student student,int pos) throws Exception{ // 查询所有学生或者部分学生
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        ResultSet result =  null; // 结果集
        String sql = "";
        ArrayList<Student> studentlist = new ArrayList<Student>(); // 返回的结果
        /**
         * 0 代表查询全部
         * 1 代表查询所有姓名为name的学生
         * 2 代表查询所有性别为gender的学生
         * 3 代表查询一个学号为id的学生
         * 4 代表查询所有班级为team的学生
         * 5 代表查询...同删除操作
         */
        switch(pos) {
        case 0:
            sql = "select * from student";
            try {
                sqlaction = sqllink.prepareStatement(sql);
                result = sqlaction.executeQuery(); // 执行查询操作
                while(result.next()) {
                    String name = result.getString("name");
                    String gender = result.getString("gender");
                    String id = result.getString("id");
                    String team = result.getString("team");
                    studentlist.add(new Student(name,gender,id,team)); // 添加至返回结果中
                }
                
                return studentlist;
            }catch(SQLException e) {
                e.printStackTrace();
                return null;
            }finally {
                jdbc.closeConnection();
                if(sqlaction != null) {
                    sqlaction.close();
                }
            }
        case 1:
            sql = "select * from student where name like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            break;
        case 2:
            sql = "select * from student where gender like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getGender());
            break;
        case 3:
            sql = "select * from student where id like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getId());
            break;
        case 4:    
            sql = "select * from student where team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getTeam());
            break;
        case 5:
            sql = "select * from student where name like ?  and gender like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            sqlaction.setString(2,student.getGender());
            break;
        case 6:
            sql = "select * from student where name like ? and id like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            sqlaction.setString(2,student.getId());
            break;
        case 7:
            sql = "select * from student where name like ? and team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            sqlaction.setString(2,student.getTeam());
            break;
        case 8:
            sql = "select * from student where gender like ? and id like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getGender());
            sqlaction.setString(2,student.getId());
            break;
        case 9:
            sql = "select * from student where gender like ? and team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getGender());
            sqlaction.setString(2,student.getTeam());
            break;
        case 10:
            sql = "select * from student where id like ? and team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getId());
            sqlaction.setString(2,student.getTeam());
            break;
        case 11:
            sql = "select * from student where name like ? and gender like ? and id like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            sqlaction.setString(2,student.getGender());
            sqlaction.setString(3,student.getId());
            break;
        case 12:
            sql = "select * from student where name like ? and gender like ? and team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            sqlaction.setString(2,student.getGender());
            sqlaction.setString(3,student.getTeam());
            break;
        case 13:
            sql = "select * from student where name like ? and id like ? and team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            sqlaction.setString(2,student.getId());
            sqlaction.setString(3,student.getTeam());
            break;
        case 14:
            sql = "select * from student where  gender like ? and id like ? and team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getGender());
            sqlaction.setString(2,student.getId());
            sqlaction.setString(3,student.getTeam());
            break;
        case 15:
            sql = "select * from student where  name like ? and gender like ? and id like ? and team like ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,student.getName());
            sqlaction.setString(2,student.getGender());
            sqlaction.setString(3,student.getId());
            sqlaction.setString(4,student.getTeam());
            break;
        }
        
        try {
            
            result = sqlaction.executeQuery(); // 执行查询操作
            while(result.next()) {
                String name = result.getString("name");
                String gender = result.getString("gender");
                String id = result.getString("id");
                String team = result.getString("team");
                studentlist.add(new Student(name,gender,id,team)); // 添加至返回结果中
            }
            return studentlist;
        }catch(SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            jdbc.closeConnection();
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
    }
    /**
     * 超级管理员权限
     * 查找所有管理员
     * @param root 验证属性
     * @return
     * @throws Exception
     */
    public static ArrayList<Root> findRoot(Root root) throws Exception{ // 完全查找权限
        
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        ResultSet result = null;
        ArrayList<Root> rootlist = new ArrayList<Root>();
         String sql = "select * from root";
        try {
            sqlaction = sqllink.prepareStatement(sql);
            
            result = sqlaction.executeQuery();
            while(result.next()) {
                String username = result.getString("username");
                String password = result.getString("password");
                String superroot = result.getString("superroot");
                rootlist.add(new Root(username,password,superroot));
            }
            return rootlist;
        }catch(SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            jdbc.closeConnection();
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
    }
    
    /**
     * 获取权限信息
     * @param username 用户名
     * @return
     * @throws Exception
     */
    public static String getAccess(String username) throws Exception{
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        ResultSet result = null;
        
        String sql = "select superroot from root where username = ?";
        try {
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1, username);
            result= sqlaction.executeQuery();
            if(result.next()) {
                return result.getString("superroot");
            }else {
                return "0";
            }
        }catch(SQLException e) {
            e.printStackTrace();
            return "0";
        }finally {
            jdbc.closeConnection();
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
    }
    
    public static int getCount() throws Exception{
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        ResultSet result = null;
        
        String sql = "select count(*) from student";
        try {
            sqlaction = sqllink.prepareStatement(sql);
            result  = sqlaction.executeQuery();
            if(result.next()) {
                return Integer.parseInt(result.getString(1));
            }else {
                return 0;
            }
        }catch(Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    
}

对于修改Update.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.wu.JavaService;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
import com.wu.JavaBean.Root;
import com.wu.JavaBean.Student;
import com.wu.JavaDao.JDBC;
 
/**
* @date 2020年12月15日下午10:01:22
* @author 一夜星尘
*/
public class Update {
    /**
     * 更新学生信息
     * @param oldstudent 待修改的学生
     * @param newstudent 修改后的学生
     * @param pos 修改方式
     * @return
     * @throws Exception
     */
    public static boolean updateStudent(Student oldstudent,Student newstudent,int pos) throws Exception{  // 部分或者完全更新模式
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        String sql = "";
        
        int count = 0;
        switch(pos) {
        case 0:
            sql = "update student set name = ?,gender  = ?,id = ?,team = ?  where id = ?"; // id一定要存在
            try {
                sqlaction  = sqllink.prepareStatement(sql);
                sqlaction.setString(1,newstudent.getName());
                sqlaction.setString(2,newstudent.getGender());
                sqlaction.setString(3,newstudent.getId());
                sqlaction.setString(4,newstudent.getTeam());
                sqlaction.setString(5,oldstudent.getId());
                
                count = sqlaction.executeUpdate(); //执行操作
                return count==1?true:false;
            }catch(SQLException e) {
                e.printStackTrace();
                return false;
            }finally {
                jdbc.closeConnection();
                if(sqlaction != null) {
                    sqlaction.close();
                }
            }
        case 1:
            sql = "update student set name = ? where name = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getName());
            sqlaction.setString(2, oldstudent.getName());
            break;
        case 2:
            sql = "update student set name = ? where gender = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getName());
            sqlaction.setString(2, oldstudent.getGender());
            break;
        case 3:
            sql = "update student set name = ? where id = ?";  
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getName());
            sqlaction.setString(2, oldstudent.getId());
            break;
        case 4:
            sql = "update student set name = ? where team = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getName());
            sqlaction.setString(2, oldstudent.getTeam());
            break;
        case 5:
            sql = "update student set gender = ? where name = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getGender());
            sqlaction.setString(2, oldstudent.getName());
            break;
        case 6:
            sql = "update student set gender = ? where gender = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getGender());
            sqlaction.setString(2, oldstudent.getGender());
            break;
        case 7:
            sql = "update student set gender = ? where id = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getGender());
            sqlaction.setString(2, oldstudent.getId());
            break;
        case 8:
            sql = "update student set gender = ? where team = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getGender());
            sqlaction.setString(2, oldstudent.getTeam());
            break;
        case 9:
            sql = "update student set id = ? where id = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getId());
            sqlaction.setString(2, oldstudent.getId());
            break;
        case 10:
            sql = "update student set team = ? where name = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getTeam());
            sqlaction.setString(2, oldstudent.getName());
            break;
        case 11:
            sql = "update student set team = ? where gender = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getTeam());
            sqlaction.setString(2, oldstudent.getGender());
            break;
        case 12:
            sql = "update student set team = ? where id = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getTeam());
            sqlaction.setString(2, oldstudent.getId());
            break;
        case 13:
            sql = "update student set team = ? where team = ?";
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,newstudent.getTeam());
            sqlaction.setString(2, oldstudent.getTeam());
            break;
        }
        
        try {
            count = sqlaction.executeUpdate();
            return count >= 1 ? true:false;
        }catch(SQLException e) {
            e.printStackTrace(); 
            return false;
        }finally {
            jdbc.closeConnection();
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
    }
    /**
     * 超级管理员权限
     * @param root 待更新的管理员
     * @param info 更新信息
     * @param pos 更新方式
     * @return
     * @throws Exception
     */
    public static boolean updateRoot(Root root ,String info,int pos) throws Exception{  // 完全更新模式
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        String sql = "";
        
        switch(pos){
            case 1:
                sql = "update root set username = ? where username =?";
                break;
            case 2:
                sql = "update root set password = ? where username =?";
                break;
        }
        try {
            sqlaction  = sqllink.prepareStatement(sql);
            
            sqlaction.setString(1,info);
            sqlaction.setString(2, root.getUsername());
            
            int count = sqlaction.executeUpdate();
            return count == 1?true:false;
        }catch(SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            jdbc.closeConnection();
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
    }
}

加上管理员登录认证Exist.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.wu.JavaService;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.wu.JavaBean.Root;
import com.wu.JavaDao.JDBC;
 
/**
* @date 2020年12月15日下午10:41:32
* @author 一夜星尘
*/
public class Exist {
    /**
     * 管理员登录认证
     * @param root 管理员
     * @return
     * @throws Exception
     */
    public static boolean rootIsExist(Root root) throws Exception {
        // 获取数据库对象
        JDBC jdbc = new JDBC();
        Connection sqllink = jdbc.getConnection();
        
        PreparedStatement sqlaction = null; // 创建一个数据库操作对象
        ResultSet result = null;
        String sql = "select count(*) from root where username = ? and password = ?";
        
        try {
            sqlaction = sqllink.prepareStatement(sql);
            sqlaction.setString(1,root.getUsername());
            sqlaction.setString(2,root.getPassword());
            
            result = sqlaction.executeQuery();
            if(result.next()) {
                int count = Integer.parseInt(result.getString(1));
                return count == 1 ? true:false;
            }else {
                return false;
            }
        }catch(SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            jdbc.closeConnection();  // 关闭数据库连接
            if(sqlaction != null) {
                sqlaction.close();
            }
        }
    }
}

处理模糊查询的DealString.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.wu.JavaService;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
* @date 2020年12月15日下午1:48:05
* @author 一夜星尘
*/
public class DealString {
    public static String[]  deal(String search) {
        
        String[] searchs = search.split("&");
        String regex = "([\\u4e00-\\u9fa5]+)=([ % _ a-z 0-9 \\u4e00-\\u9fa5]+)"; // 匹配中文或者数字模式
        
        String[] result = new String[5];
        result[0] = "0"; // 默认为全部
        boolean nameflag = false;
        boolean genderflag = false;
        boolean idflag = false;
        boolean teamflag = false;
        HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
        for(String str : searchs) {
            Matcher mattcher = Pattern.compile(regex).matcher(str);
            if(mattcher.find()) {
                if(mattcher.group(1).equals("姓名")) {
                    nameflag = true;
                    hashmap.put(1,mattcher.group(2));
                }else if(mattcher.group(1).equals("性别")){
                    genderflag = true;
                    hashmap.put(2,mattcher.group(2));
                }else if(mattcher.group(1).equals("学号")) {
                    idflag = true;
                    hashmap.put(3,mattcher.group(2));
                }else if(mattcher.group(1).equals("班级")) {
                    teamflag = true;
                    hashmap.put(4,mattcher.group(2));
                }else {
                    
                }
                
            }
        }
        // 对应位置放置相关信息
        Iterator<?> iter = hashmap.entrySet().iterator(); 
        while (iter.hasNext()) { 
            Map.Entry entry = (Map.Entry) iter.next(); 
            int  key = (int) entry.getKey(); 
            String val =(String) entry.getValue(); 
            result[key] = val;
        
        if(nameflag && !genderflag && !idflag && !teamflag) {
            result[0] = "1";
            return result;
        }else if(!nameflag && genderflag && !idflag && !teamflag) {
            result[0] = "2";
            return result;
        }else if(!nameflag && !genderflag && idflag && !teamflag) {
            result[0] = "3";
            return result;
        }else if(!nameflag && !genderflag && !idflag && teamflag) {
            result[0] = "4";
            return result;
        }else if(nameflag && genderflag && !idflag && !teamflag) {
            result[0] = "5";
            return result;
        }else if(nameflag && !genderflag && idflag && !teamflag) {
            result[0] = "6";
            return result;
        }else if(nameflag && !genderflag && !idflag && teamflag) {
            result[0] = "7";
            return result;
        }else if(!nameflag && genderflag && idflag && !teamflag) {
            result[0] = "8";
            return result;
        }else if(!nameflag && genderflag && !idflag && teamflag) {
            result[0] = "9";
            return result;
        }else if(!nameflag && !genderflag && idflag && teamflag) {
            result[0] = "10";
            return result;
        }else if(nameflag && genderflag && idflag && !teamflag) {
            result[0] = "11";
            return result;
        }else if(nameflag && genderflag && !idflag && teamflag) {
            result[0] = "12";
            return result;
        }else if(nameflag && !genderflag && idflag && teamflag) {
            result[0] = "13";
            return result;
        }else if(!nameflag && genderflag && idflag && teamflag) {
            result[0] = "14";
            return result;
        }else if(nameflag && genderflag && idflag && teamflag) {
            result[0] = "15";
            return result;
        }
        return result;
    }
}

接下来就是可视化界面,在JavaView包下

Home.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.wu.JavaView;
/**
* @date 2020年12月16日下午5:09:16
* @author 一夜星尘
*/
public class Hemo {
    public static void main(String[] args) {
        try {
            new Login();
        }catch(Exception e) {
            System.out.println("程序出错!");
        }
    }
}

登录界面Login.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.wu.JavaView;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
 
import com.wu.JavaBean.Root;
import com.wu.JavaService.Exist;
 
/**
* @date 2020年12月16日下午10:02:08
* @author 一夜星尘
*/
public class Login extends JFrame{
    
    private static final long serialVersionUID = 1L;
    public Login() throws Exception{
        this.setSize(450,350); // 设置宽高度
        this.setTitle("登录界面"); // 设置标题
        this.setResizable(false); // 固定窗口大小
        this.setUndecorated(true); // 去掉窗口的装饰 
        this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); //采用指定的窗口装饰风格
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 结束程序
        this.setLocationRelativeTo(null); // 使之位于主窗口的中心
        
        setBackGroundPanel();
        this.setVisible(true); // 显示
        
    }
    public void setBackGroundPanel() throws Exception{
         JPanel panel = new JPanel();
         
         
         JButton jb=new JButton("测试按钮");
         jb.setBounds(100,100,100,100);
         
         this.add(panel);
        
        panel.setLayout(null); // 空布局
        
        Font font = new Font("微软雅黑",Font.BOLD,11);
        Border border1 = BorderFactory.createLoweredBevelBorder();
        Border border2 = BorderFactory.createLineBorder(Color.BLUE);
        
        JLabel usernamelabel = new JLabel("账号: ");
        usernamelabel.setFont(font);
        usernamelabel.setForeground(Color.BLACK);
        usernamelabel.setBounds(130,100,30,15);
        JLabel passwordlabel = new JLabel("密码: ");
        passwordlabel.setFont(font);
        passwordlabel.setForeground(Color.BLACK);
        passwordlabel.setBounds(130,150,30,15);
        
        JTextField  usernametext = new JTextField("I am superroot");
        usernametext.setBounds(160,95,150,20);
        usernametext.setBorder(border1);
//        usernametext.setOpaque(false); // 透明框
        JPasswordField passwordtext = new JPasswordField("password");
        passwordtext.setBounds(160,145,150,20);
        passwordtext.setBorder(border1);
        
        JButton submit = new JButton("登录");
        JButton close = new JButton("退出");
                
        submit.setBorder(border2); // 登录键边框风格
        submit.setBounds(130,210,90,25);
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String username = usernametext.getText();
                String password = new String(passwordtext.getPassword());
                if(username.equals("")) {
                    JOptionPane.showMessageDialog(null, "用户名不能为空!", "错误",JOptionPane.WARNING_MESSAGE);
                }else if(password.equals("")){
                    JOptionPane.showMessageDialog(null, "密码不能为空!", "错误",JOptionPane.WARNING_MESSAGE);
                }else {
                    // 登录认证
                    Root root = new Root(username,password,"0"); // 新建一个虚拟管理员对象
                    try {
                        if(Exist.rootIsExist(root)) { 
                            new Menu(username);
                            dispose();  // 关闭当前的窗口
                        }else {
                            JOptionPane.showMessageDialog(null, "用户名或密码错误!", "错误",JOptionPane.WARNING_MESSAGE);
                        }
                    }catch(Exception e) {
                        e.printStackTrace();
                    }finally {
                        usernametext.setText("");
                        passwordtext.setText("");
                    }
                }
            }
        });
        
        close.setBorder(border2); // 关闭键边框风格
        close.setBounds(250,210,90,25);
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                
                int quit = JOptionPane.showConfirmDialog(null,"是否退出?","提示",JOptionPane.YES_NO_OPTION);
                if(quit == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }        
            }
        });
        
        panel.add(submit);
        panel.add(close);
        panel.add(usernametext);
        panel.add(passwordtext);
        panel.add(usernamelabel);
        panel.add(passwordlabel);
        
    }
    
}

效果如下,布局简单:

JavaSwing实现小型学生管理系统

主页面Menu.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.wu.JavaView;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRootPane;
 
/**
* @date 2020年12月16日上午10:53:39
* @author 一夜星尘
*/
public class Menu{
    
    private static JFrame Frame = new JFrame();
    private String username = null;
    private static  JPanel Panel  = null;
    
    public Menu(String username) {
        this.username = username;
        Frame.setSize(800,600); // 设置宽高度
        Frame.setTitle("菜单界面"); // 设置标题
        Frame.setResizable(false); // 固定窗口大小
        Frame.setUndecorated(true); // 去掉窗口的装饰 
        Frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); //采用指定的窗口装饰风格
        Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Frame.setLocationRelativeTo(null); // 主窗口的中心
                
        Panel  = this.getPanel();
        this.setMenu(); // 设置菜单项
        Frame.add(Panel);
        Frame.setVisible(true); // 可见
    }
    public JPanel  getPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(null);// 绝对布局
        panel.setBackground(Color.LIGHT_GRAY);
        
        Font font = new Font("微软雅黑",Font.BOLD,20);
        JLabel id="codetool">

笔者这里由于增删改查界面设计繁琐,亦限于篇幅,只展示‘查’的这一部分FindPanel.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.wu.JavaView;
 
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
 
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
 
import com.wu.JavaBean.Root;
import com.wu.JavaBean.Student;
import com.wu.JavaService.DealString;
import com.wu.JavaService.Find;
 
/**
* @date 2020年12月16日上午10:09:35
* @author 一夜星尘
*/
public class FindPanel {
    private static JPanel Panel = null;
    private static JFrame Frame = null;
    private static String username = null;
    private static Icon buttonicon = new ImageIcon("src\\images\\searchbutton.png");
    public static JPanel getPanel(String username,int flag) {
        Frame = Menu.getFrame(); // 同一Frame
        FindPanel.username = username;
        
        Panel = new JPanel();                //生成新的布局
        Panel.setLayout(null);                // 绝对布局
        Panel.setBounds(0,0,790,567); //设置布局大小
        Panel.setBackground(Color.LIGHT_GRAY);
        
        setMenu(); // 设置菜单项
        find(flag); // 增添学生或管理员 flag 1:学生 2 :管理员
        
        return Panel;
    }
    
    public static void setMenu() {
        // 菜单条
        JMenuBar menubar = new JMenuBar();
        menubar.setBounds(0,0,800,40);
        Panel.add(menubar);
        
        //菜单项
        JMenu addmenu = new JMenu("添加");
        JMenuItem addmenuItem1 = new JMenuItem("添加学生");
        JMenuItem addmenuItem2 = new JMenuItem("添加管理员");
        addmenu.add(addmenuItem1);
        addmenu.add(addmenuItem2);
        
        JMenu removemenu = new JMenu("删除");
        JMenuItem removemenuItem1 = new JMenuItem("删除学生");
        JMenuItem removemenuItem2 = new JMenuItem("删除管理员");
        removemenu.add(removemenuItem1);
        removemenu.add(removemenuItem2);
        
        JMenu findmenu = new JMenu("查找");
        JMenuItem findmenuItem1 = new JMenuItem("查找学生");
        JMenuItem findmenuItem2 = new JMenuItem("查找管理员");
        findmenu.add(findmenuItem1);
        findmenu.add(findmenuItem2);
        
        JMenu updatemenu = new JMenu("修改");
        JMenuItem updatemenuItem1 = new JMenuItem("修改学生");
        JMenuItem updatemenuItem2 = new JMenuItem("修改管理员");
        updatemenu.add(updatemenuItem1);
        updatemenu.add(updatemenuItem2);
        
        JMenu accessmenu = new JMenu("账号设置");
        JMenuItem accessmenuItem1 = new JMenuItem("修改用户名");
        JMenuItem accessmenuItem2 = new JMenuItem("修改密码");        
        JMenuItem accessmenuItem3 = new JMenuItem("退出账号");
        JMenuItem accessmenuItem4 = new JMenuItem("注销账号");
        accessmenu.add(accessmenuItem1);
        accessmenu.add(accessmenuItem2);
        accessmenu.add(accessmenuItem3);
        accessmenu.add(accessmenuItem4);
        
        
        // 添加功能 响应
        {
            addmenuItem1.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = AddPanel.getPanel(FindPanel.username,1); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
            addmenuItem2.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = AddPanel.getPanel(FindPanel.username,2); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });        
        }
        
        //移除功能 响应
        {
            removemenuItem1.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = RemovePanel.getPanel(FindPanel.username,1); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
            
            removemenuItem2.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = RemovePanel.getPanel(FindPanel.username,2); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
        }
        //查看功能 响应
        {
            findmenuItem1.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = FindPanel.getPanel(FindPanel.username,1); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
            findmenuItem2.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = FindPanel.getPanel(FindPanel.username,2); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });    
        }
        //修改功能 响应
        {
            updatemenuItem1.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = UpdatePanel.getPanel(FindPanel.username,1); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
            
            updatemenuItem2.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = UpdatePanel.getPanel(FindPanel.username,2); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
        }
        //账号功能 响应
        {
            accessmenuItem1.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = UpdateUserPanel.getPanel(FindPanel.username,1); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
            accessmenuItem2.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = UpdateUserPanel.getPanel(FindPanel.username,2); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
            accessmenuItem3.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = AccountPanel.getPanel(FindPanel.username,1); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
            accessmenuItem4.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    FindPanel.Frame.remove(FindPanel.Panel); // 移除当前的布局
                    Frame.repaint();
                    Panel  = AccountPanel.getPanel(FindPanel.username,2); // 切换布局
                    Frame.add(Panel);
                    Frame.setVisible(true);
                }
            });
        }
        menubar.add(addmenu);
        menubar.add(removemenu);
        menubar.add(findmenu);
        menubar.add(updatemenu);
        menubar.add(accessmenu);
    }
    public static void find(int flag) {
        if(flag == 1) {
            
            
            DefaultTableModel model = new DefaultTableModel();
            JTable table = new JTable(model);
            JScrollPane scrollpanel = new JScrollPane(table);
            scrollpanel.setBounds(0,80,800 ,560);
            Panel.add(scrollpanel);
            
            JLabel id="codetool">

具体效果如下:

JavaSwing实现小型学生管理系统

支持模糊查询,即通过 属性1=内容1&属性2=内容2 可以配合%_两个符号查询

JavaSwing实现小型学生管理系统

这里笔者为了简单,没有精雕细琢,读者可以根据自己的需要修改即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43914658/article/details/111501438

延伸 · 阅读

精彩推荐
  • Java教程实例讲解Java并发编程之变量

    实例讲解Java并发编程之变量

    这篇文章主要介绍了实例讲解Java并发编程之变量,本文讲解了编写线程安全需要关心的共享变量和可变变量,需要的朋友可以参考下 ...

    junjie2562019-12-16
  • Java教程如何在 Java 中优雅地分割 String 字符串

    如何在 Java 中优雅地分割 String 字符串

    相信很多同学都知道,String 类中的 split 方法可以进行字符串分割,然而日常使用起来却仅限于 str.split( "-"),其中 "-"为分隔符。其实 split 方法的功能非常...

    鸭哥聊Java7862021-04-15
  • Java教程java 使用异常的好处总结

    java 使用异常的好处总结

    这篇文章主要介绍了java 使用异常的好处总结的相关资料,需要的朋友可以参考下...

    彭呈祥3312020-08-22
  • Java教程java实现中缀表达式转后缀的方法

    java实现中缀表达式转后缀的方法

    这篇文章主要为大家详细介绍了java实现中缀表达式转后缀的表达方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    水中鱼之19999702021-06-18
  • Java教程Java获得指定区间数的方法

    Java获得指定区间数的方法

    这篇文章主要介绍了Java获得指定区间数的方法,涉及java数值运算的相关技巧,需要的朋友可以参考下 ...

    hitxueliang4372019-12-19
  • Java教程Java基础题新手练习(三)

    Java基础题新手练习(三)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以...

    保护眼睛10332021-09-26
  • Java教程基于Java实现空间滤波完整代码

    基于Java实现空间滤波完整代码

    空间滤波是一种采用滤波处理的影像增强方法。其理论基础是空间卷积和空间相关。这篇文章主要介绍了基于Java的空间滤波代码实现,需要的朋友可以参考...

    GIS前沿5522021-12-01
  • Java教程java中压缩文件并下载的实例详解

    java中压缩文件并下载的实例详解

    在本篇内容里小编给大家整理的是一篇关于java中压缩文件并下载的实例详解内容,有兴趣的朋友们可以学习下。...

    小妮浅浅8092021-08-08