动态化、分组件java开发简单学生管理系统

学生管理系统(Java)

  • 学生管理系统(分析)
    • 视图层(View)
      • 用户登录页面(LoginFrame)
      • 主面板(mainFrame)
    • 模型层(Model)
      • 数据库基本操作
      • 实体类
      • 数据库具体操作
    • 控制层
  • 学生管理系统(实现)

学生管理系统(分析)

视图层(View)

用户登录页面(LoginFrame)

动态化、分组件java开发简单学生管理系统_第1张图片
这没有什么好分析的,就是一个JFrame框架,两个面板(一是存放主体的主面板以及存放按钮的面板),三个标签,两个文本域以及两个按钮。
这里需要注意的是把按钮所在的面板设置为透明。
采用GridBagLayout布局设置成如图所示。

主面板(mainFrame)

动态化、分组件java开发简单学生管理系统_第2张图片
主界面需要菜单栏(JMenuBar)、菜单(JMenu)、子菜单(JMenuItem)、工具栏、三个JLabel、两个JPanel、两个JButton。
这里需要注意的是在主面板部分需要传入一个 对象用以获取登录者的用户名。还有最重要的一点是菜单栏部分以及菜单下的子菜单都是动态的,通过数据库配置来获取的。这样的话,菜单部分可以通过数据库随意更改。

模型层(Model)

数据库基本操作

数据库分为查询操作和更新操作(增、删、改)。在这一部分只要完成数据库的连接、数据库的查询(executeQuery)、更新(executeUpdate)以及数据库的关闭等基本操作。具体步骤交由后面的来完成。

实体类

实体类视情况而定。由于我采用通过数据库来配置菜单以及子菜单相对应的操作,所以除了必须的用户实体之外,还需要菜单的实体,以及功能实体。
动态化、分组件java开发简单学生管理系统_第3张图片

数据库具体操作

定义接口,将需要的操作在接口中定义好,后续程序再实现接口来完成具体操作。
例如我的程序中的IStudentDao接口
动态化、分组件java开发简单学生管理系统_第4张图片
我的具体实现

package com.mpf.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mpf.model.entity.Student;
import com.mpf.model.util.DBUtil;

public class IStudentDaoImpl implements IStudentDao {

   @Override
   public boolean add(Student stu) {
   	boolean flag = false;
   	
   	String sno = stu.getSNO();
   	String name = stu.getSname();
   	String sex = stu.getSex();
   	int age = stu.getAge();
   	String dept = stu.getDept();
   	
   	String sql = "insert into s(sno,sname,ssex,sage,sdept) "
   			+ "values('"+sno+"','"+name+"','"+sex+"','"+age+"','"+dept+"')";
   	System.out.println("add:"+sql);
   	
   	DBUtil db = new DBUtil();
   	int n = db.DBUpdate(sql);
   	if(n>0)
   	{
   		flag = true;
   	}
   	db.getClose();
   	return flag;
   }

   @Override
   public boolean remove(String SNO) {
   	boolean flag = false;
   	String sql = "delete from s where sno='"+SNO+"'";
   	DBUtil db = new DBUtil();
   	int n = db.DBUpdate(sql);
   	System.out.println("remove:"+sql);
   	if(n>0)
   	{
   		flag = true;
   	}
   	db.getClose();
   	return flag;
   }

   @Override
   public boolean modify(Student stu) {
   	boolean flag = false;
   	String sno = stu.getSNO();
   	String name = stu.getSname();
   	String sex = stu.getSex();
   	int age = stu.getAge();
   	String dept = stu.getDept();
   	
   	String sql = "update s set sname='"+name+"',ssex='"+sex+"',"
   			+ "sage='"+age+"',sdept='"+dept+"' where sno='"+sno+"'";
   	System.out.println("modify:"+sql);
   	
   	DBUtil db =new DBUtil();;
   	int n = db.DBUpdate(sql);
   	if(n>0)
   	{
   		flag = true;
   	}
   	db.getClose();	
   	return flag;
   }

   @Override
   public Student findBySno(String SNO) {
   	Student stu = null;
   	String sql = "select * from s where sno='"+SNO+"'";
   	DBUtil db = new DBUtil();
   	ResultSet rs = db.Query(sql);
   	try {
   		while(rs.next())
   		{
   			stu = new Student();
   			stu.setSNO(rs.getString(1));
   			stu.setSname(rs.getString(2));
   			stu.setSex(rs.getString(3));
   			stu.setAge(rs.getInt(4));
   			stu.setDept((rs.getString(5)));
   		}
   	} catch (SQLException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
   	}finally
   	{
   		db.getClose();
   	}
   	return stu;
   }

   @Override
   public List<Student> findByLike(Student stu) {
   	List<Student> list = new ArrayList<Student>();
   	DBUtil db = new DBUtil();
   	IStudentDao dao = new IStudentDaoImpl();
   	String select = "select * from s";
   	String where = " where 1=1";
   	if(stu.getSNO() != null)
   	{
   		String sno = stu.getSNO();
   		where = where+" and sno like '%"+sno+"%'";
   	}
   	if(stu.getSname() != null)
   	{
   		String sname = stu.getSname();
   		where = where+" and sname like '%"+sname+"%'";
   	}
   	if(stu.getSex() != null)
   	{
   		String sex = stu.getSex();
   		where = where+" and ssex like '%"+sex+"%'";
   	}	
   	if(stu.getAge() != 0)
   	{
   		int sage = stu.getAge();
   		where = where+" and sage like '%"+sage+"%'";
   	}	
   	if(stu.getDept() != null)
   	{
   		String sdept = stu.getDept();
   		where = where+" and sdept like '%"+sdept+"%'";
   	}
   	String sql = select + where;
   	System.out.println(sql);
   	ResultSet rs = db.Query(sql);
   	try {
   		while(rs.next())
   		{
   			Student temp = new Student();
   			
   			temp.setSNO(rs.getString(1));
   			temp.setSname(rs.getString(2));
   			temp.setSex(rs.getString(3));
   			temp.setAge(rs.getInt(4));
   			temp.setDept(rs.getString(5));
   			
   			list.add(temp);
   		}
   	} catch (SQLException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
   	}finally
   	{
   		db.getClose();
   	}
   	return list;
   }

}

控制层

菜单监听事件
动态化、分组件java开发简单学生管理系统_第5张图片
动态化、分组件java开发简单学生管理系统_第6张图片
按钮监听事件
动态化、分组件java开发简单学生管理系统_第7张图片
目前只是简单显示对应页面,具体操作后续还需完成
动态化、分组件java开发简单学生管理系统_第8张图片
目前没有完成对应逻辑操作

学生管理系统(实现)

视图层(登录)
下面展示一些 内联代码片

package com.mpf.view;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.mpf.model.dao.IUsersDao;
import com.mpf.model.dao.IUsersDaoImpl;
import com.mpf.model.entity.Users;

public class LoginFrame extends JFrame implements ActionListener {
	private Users user;
	
	private JPanel PanelBody = null;
	private JPanel PanelButton = null;
	
	private JLabel LabelTitle = null;
	private JLabel LabelAccount = null; 
	private JTextField filedAccount = null;
	private JLabel LabelPassword = null;
	private JPasswordField passwordField = null;
	
	private JButton LoginButton = null;
	private JButton ResetButton = null; 
	
	private boolean checkUser(String account,String password)
	{
		boolean flag = false;
		IUsersDao dao = new IUsersDaoImpl();
		Users tempUser = new Users();
		tempUser = dao.findBySno(account);
		if(tempUser != null&&tempUser.getPassword().equals(password))
		{
			flag = true;
			this.user = tempUser;
		}
		return flag;
	}
	
	private void init() {
		Container container = this.getContentPane();
		this.PanelBody = new JPanel(new GridBagLayout());
		container.add(this.PanelBody);

		
		GridBagConstraints gbc = new GridBagConstraints();
		this.LabelTitle = new JLabel("学籍管理登录");
		gbc.gridx = 1;
		gbc.gridy = 0;		
		this.PanelBody.add(this.LabelTitle,gbc);
		
		this.LabelAccount = new JLabel("账      号:");
		gbc.gridx = 0;
		gbc.gridy = 1;
		this.PanelBody.add(this.LabelAccount,gbc);
		this.filedAccount = new JTextField(16);
		gbc.gridx = 1;
		gbc.gridy = 1;
		this.PanelBody.add(this.filedAccount,gbc);
		
		this.LabelPassword = new JLabel("密      码:");
		gbc.gridx = 0;
		gbc.gridy = 2;
		this.PanelBody.add(LabelPassword,gbc);
		this.passwordField = new JPasswordField(16);
		gbc.gridx = 1;
		gbc.gridy = 2;
		this.PanelBody.add(this.passwordField,gbc); 
		
		this.PanelButton = new JPanel();
		this.PanelButton.setOpaque(false);							//设置面板透明
		this.LoginButton = new JButton("登录");
		this.LoginButton.addActionListener(this);
		this.ResetButton = new JButton("重置");
		this.PanelButton.add(LoginButton);
		this.PanelButton.add(ResetButton);
		
		gbc.gridx = 1;
		gbc.gridy = 3;
		this.PanelBody.add(this.PanelButton,gbc);
		this.PanelBody.setBackground(new Color(195, 231, 138));
		this.PanelBody.setBorder(BorderFactory.createMatteBorder(3, 0, 0, 0, new Color(41,107,80)));	//设置边框
		this.setTitle("学籍管理");
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public LoginFrame()
	{
		this.init();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.LoginButton)
		{
			String account = this.filedAccount.getText();
			String password = new String(this.passwordField.getPassword());
			if(account == null || account.length() <= 0 || password == null || password.length() <= 0)
			{
				JOptionPane.showMessageDialog(this, "用户名或密码不能为空!!!");
				return;
			}
			else if(checkUser(account, password))
			{
				JOptionPane.showMessageDialog(this, "登录成功!");
				this.dispose();
				MainFrame mainFrame = new MainFrame(this.user);
			}else {
				JOptionPane.showMessageDialog(this,"用户名或密码错误!");
			}
		}
	}
	
	public static void main(String[] args) {
		LoginFrame loginFrame = new LoginFrame();
		loginFrame.setBounds(650,370,350,250);
	}


}

视图层(主面板)

package com.mpf.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.BorderFactory;
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.JPanel;
import javax.swing.JToolBar;

import com.mpf.control.listener.MenuListener;
import com.mpf.model.dao.IMenuDao;
import com.mpf.model.dao.IMenuDaoImpl;
import com.mpf.model.entity.Menu;
import com.mpf.model.entity.Users;

public class MainFrame extends JFrame {
	private Users user = null;
	
	private JMenuBar menuBar = null;
	private JMenu helpMenu = null;
	private JMenuItem aboutItem = null;
	
	private JToolBar toolBar = null; 
	private JLabel welcomLable = null;
	private JLabel textLable = null;
	private JButton buttonExit = null;
	private JButton buttonRelog = null;
	
	private JPanel panelWelcom = null;
	
	private JLabel lableState = null;
	
	private JPanel paneBody = null;
	
	private void createMenu()
	{
		IMenuDao dao = new IMenuDaoImpl();
		String sql1 = "select * from menu where menuId like'__'";
		List<Menu> list1 = dao.findBySql(sql1);
		for (Menu tempmenu : list1) {
			JMenu menu = new JMenu();
			menu.setActionCommand(tempmenu.getMenuId());
			menu.setText(tempmenu.getMenuName());
			menu.setToolTipText(tempmenu.getMenuMemo());
			
			String sql2 = "select * from menu where menuId like '"+menu.getActionCommand()+"__'";
			List<Menu> list2 = dao.findBySql(sql2);
			for (Menu tempmenu2 : list2) {
				JMenuItem item = new JMenuItem();
				item.addActionListener(new MenuListener(this,this.user));
				item.setActionCommand(tempmenu2.getMenuId());
				item.setText(tempmenu2.getMenuName());
				
				menu.add(item);
			}
			this.menuBar.add(menu);
		}
	}
	public void init()
	{
		this.menuBar = new JMenuBar();
		this.createMenu();
		this.helpMenu = new JMenu("帮助");
		this.aboutItem = new JMenuItem("关于");
		this.helpMenu.add(this.aboutItem);
		this.menuBar.add(this.helpMenu);
		this.setJMenuBar(this.menuBar);
		
		this.paneBody = (JPanel)this.getContentPane();
		this.paneBody.setLayout(new BorderLayout());
		
		this.toolBar = new JToolBar();
		this.toolBar.setLayout(new FlowLayout(FlowLayout.CENTER));
		this.toolBar.setFloatable(false);
		this.welcomLable = new JLabel("欢迎:");
		this.textLable = new JLabel(user.getSno());
		this.buttonExit = new JButton("退出");
		this.buttonRelog = new JButton("重新登录");
		this.toolBar.add(this.welcomLable);
		this.toolBar.add(this.textLable);
		this.toolBar.add(this.buttonExit);
		this.toolBar.add(this.buttonRelog);
		this.paneBody.add(this.toolBar,BorderLayout.NORTH);
		
		this.panelWelcom = new JPanel();
		this.panelWelcom.setBorder(BorderFactory.createMatteBorder(3, 0, 1, 0, new Color(41,107,80)));
		this.panelWelcom.setBackground(new Color(195, 231, 138));
		this.paneBody.add(this.panelWelcom,BorderLayout.CENTER);
		
		this.lableState = new JLabel("学籍管理系统1.0",JLabel.CENTER);
		this.paneBody.add(this.lableState,BorderLayout.SOUTH);
		
		this.setTitle("学籍管理系统主界面");
		this.setVisible(true);
		this.setBounds(500, 500, 800, 600);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	}
	
	public MainFrame(Users user)
	{
		this.user = user;
		this.init();
	}
}

数据库基本操作就不粘贴出来了,太多了

最最主要的是控制层代码== 其中最重要的一段代码,通过反射获取对应逻辑操作,开始是通过 多个if-else完成对应操作,后来发现太傻了,需要完成的代码太多了。而通过反射只需几行代码就可以完成多个操作。==

package com.mpf.control.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import com.mpf.control.action.IFunctionAction;
import com.mpf.control.action.addStudent;
import com.mpf.view.MainFrame;

public class FunctionButtonListener implements ActionListener {
	
	private MainFrame mainFrame = null;
	private JPanel workPanel = null;

	public FunctionButtonListener() {

	}

	public FunctionButtonListener(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		JPanel bodyPanel = (JPanel)this.mainFrame.getContentPane();
		JPanel welcomPanel = (JPanel)bodyPanel.getComponent(1);
		this.workPanel = (JPanel)welcomPanel.getComponent(0);
		this.workPanel.removeAll();
		this.workPanel.repaint();
		
		String  command = e.getActionCommand();
		String [] commands = command.split("//");
		String className = commands[1];
		System.out.println(" "+className);
			try {
				IFunctionAction action = null;
				Class myClass = Class.forName(className);
				action = (IFunctionAction)myClass.newInstance();
				action.execute(this.workPanel);
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		this.mainFrame.setVisible(true);
	}

}

== 仅是巩固所学==

你可能感兴趣的:(java,数据库)