JavaSwing 小型学生管理系统

发布时间:2023-05-23 09:00

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

数据库表结构

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

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

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

Student.java:

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:

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,该程序只是控制数据库的连接:

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:

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:

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:

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:

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:

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:

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:

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:

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 小型学生管理系统_第1张图片

主页面Menu.java:

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 title = new JLabel("管理员:"+username);
		title.setBounds(300,500,250,40);
		title.setFont(font);
		
		panel.add(title); // 增加底部标签
		
		return panel;
	}
	public 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) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = AddPanel.getPanel(Menu.this.username,1); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			
			addmenuItem2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = AddPanel.getPanel(Menu.this.username,2); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});		
		}
		
		// 移除功能 监听器实现
		{
			removemenuItem1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = RemovePanel.getPanel(Menu.this.username,1); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			
			removemenuItem2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = RemovePanel.getPanel(Menu.this.username,2); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			
		}
		// 查找功能 监听器实现
		{
			findmenuItem1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = FindPanel.getPanel(Menu.this.username,1); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			findmenuItem2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = FindPanel.getPanel(Menu.this.username,2); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});	
		}
		
		// 修改功能 监听器实现
		{
			updatemenuItem1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = UpdatePanel.getPanel(Menu.this.username,1); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			
			updatemenuItem2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = UpdatePanel.getPanel(Menu.this.username,2); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
		}
		
		// 账号功能 监听器实现
		{
			accessmenuItem1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = UpdateUserPanel.getPanel(Menu.this.username,1); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			accessmenuItem2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = UpdateUserPanel.getPanel(Menu.this.username,2); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			accessmenuItem3.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = AccountPanel.getPanel(Menu.this.username,1); // 切换布局
					Frame.add(Panel);
					Frame.setVisible(true);
				}
			});
			
			accessmenuItem4.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Menu.Frame.remove(Menu.Panel); // 移除当前的布局
					Frame.repaint();
					Panel  = AccountPanel.getPanel(Menu.this.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 JFrame getFrame() {
		return Frame;
	}
}

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

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 title = new JLabel("学生信息查询");
			title.setBounds(200,45,90,30);
			Panel.add(title);
			
			String[] Attribute = {"姓名","性别","学号","班级"};
			// 构建搜索框
			JTextField searchtext = new JTextField();
			searchtext.setBounds(300,45,160,30);
			Panel.add(searchtext);
			JButton search = new JButton(buttonicon);
			search.setBounds(460,45,30,30);
			Panel.add(search);
			// 搜索功能 响应
			{
				search.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						String search = searchtext.getText();
						String[] deal = DealString.deal(search); // 第一个位置为对应的查找方式 第二至最后一个位置为存放的相应的信息
						int pos = Integer.parseInt(deal[0]);
						Object[][] dataVector = getStudentDateVector(new Student(deal[1],deal[2],deal[3],deal[4]),pos);
//						System.out.println(deal[1]+deal[2]+deal[3]+deal[4]+pos);
						model.setDataVector(dataVector, Attribute);
					}
				});
			}
			Object[][]  dataVector = getStudentDateVector(new Student(),0); // 默认为查看所有学生
			model.setDataVector(dataVector, Attribute);
		}else if(flag == 2) {
			

			try{
				if(Find.getAccess(FindPanel.username).equals("0")) {
					JOptionPane.showMessageDialog(null, "权限不够,无法访问!", "错误",JOptionPane.WARNING_MESSAGE);
				}else {
					DefaultTableModel model = new DefaultTableModel();
					JTable table = new JTable(model);
					JScrollPane scrollpanel = new JScrollPane(table);
					scrollpanel.setBounds(0,80,800 ,560);
					Panel.add(scrollpanel);
					
					JLabel title = new JLabel("管理员信息查询");
					title.setBounds(360,45,90,30);
					Panel.add(title);
					
					String[] Attribute = {"用户名","密码"};
					Object[][]  dataVector = getRootDateVector(new Root(FindPanel.username,"","")); // 查看所有管理员
					model.setDataVector(dataVector, Attribute);
					}
				}catch(Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "权限不够,无法访问!", "错误",JOptionPane.WARNING_MESSAGE);
				}
			
		}
	}
	
	public static String[][] getStudentDateVector(Student student,int pos){
		
		try {
			String[][] data = new String[Find.getCount()][4];
			ArrayList<Student> studentlist = Find.findStduent(student, pos);
			int i = 0;
			for(Student s : studentlist) {
				data[i][0] = s.getName();
				data[i][1] = s.getGender();
				data[i][2] = s.getId();
				data[i][3] = s.getTeam();
				i++;
			}
			return data;
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
public static String[][] getRootDateVector(Root root){
		
		try {
			String[][] data = new String[Find.getCount()][2];
			ArrayList<Root> rootlist = Find.findRoot(root);
			int i = 0;
			for(Root r : rootlist) {
				data[i][0] = r.getUsername();
				data[i][1] = r.getPassword();
				i++;
			}
			return data;
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

具体效果如下:

JavaSwing 小型学生管理系统_第2张图片
支持模糊查询,即通过 属性1=内容1&属性2=内容2 可以配合%_两个符号查询
JavaSwing 小型学生管理系统_第3张图片

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

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号