👨‍🎓作者简介:一位喜欢写作,计科专业大三菜鸟

🏡个人主页:starry陆离

如果文章有帮到你的话记得点赞👍+收藏💗支持一下哦

『Java课设』JavaSwing+MySQL实现学生成绩管理系统

  • 前言
  • 1.开发工具
  • 2.项目包结构
  • 3.数据库表
  • 4.JDBC数据库连接类
  • 5.管理员登录界面(部分)
  • 6.管理员增删改查页面(部分)
  • 8.项目演示
    • 8.1登录
    • 8.2查询学生
    • 8.3增加数据
    • 8.4修改数据
    • 8.5删除数据

前言

不久前整理资料,看到了自己大二刚学完Java为了写的课设项目敲的一个课设雏形(后来课设没有用这个),因为是个半成品一共就一个界面而且很烂,但不舍得删,发出来留作纪念吧。

1.开发工具

Eclipse

Java JDK1.8

MySQL精简版

SQLyog

mysql-connector-java-5.1.41 MySQL :: Download Connector/J

sqljdbc

2.项目包结构

3.数据库表

数据库名:studentgrade

数据库表:manager、student

4.JDBC数据库连接类

连接mysql和sqlserver的方法有一点点不一样,这里笔者的机器没有sqlserver就不维护sqlserver的了,读者有需要可自行解决

//连接mysql5.7
	private static final String USER = "***";//数据库用户名,一般是root
	private static final String PWD = "******";//数据库密码
//	private static final String URL="jdbc:mysql://localhost:3306/studentgrade";
//	private static final String DRIVER="com.mysql.jdbc.Driver"; //mysql5.7
	//如果用的是mysql8.0,
	private static final String DRIVER="com.mysql.cj.jdbc.Driver";
	private static final String URL= "jdbc:mysql://localhost:3306/studentgrade?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true"; 
	

5.管理员登录界面(部分)

package view;

import handler.LoginHandler;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
/**
 * 管理员登录界面
 * 也是项目运行的起始界面
 * @author starry
 * 测试用户名:lx
 * 密码:lx0411
 */
public class LoginView extends JFrame {

    JLabel titleNameLabel=new JLabel("学生成绩管理系统",JLabel.CENTER);
    
    SpringLayout spLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(spLayout);
    
    JLabel userNameLabel=new JLabel("用户名:");
    JLabel userPwdLabel=new JLabel("登录密码:");

    JTextField nameTxd=new JTextField();
    JPasswordField pwdFiled=new  JPasswordField();

    JButton loginBtn=new JButton("登录");
    JButton resetBtn=new JButton("重置");

    //声明桌面系统托盘SystemTray
    SystemTray systemTray;
    TrayIcon trayIcon;
    LoginHandler loginHandler;

    public LoginView(){

        super("学生成绩管理系统");

        loginHandler=new LoginHandler(this);
        //获取内容面板
        Container contentPane =getContentPane();
        //设置标题的字体,格式,大小
        titleNameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
        titleNameLabel.setPreferredSize(new Dimension(0,80));
        //设置中间各个组件的字体,大小,格式
        Font centerFont=new Font("华文行楷",Font.PLAIN,20);
        userNameLabel.setFont(centerFont);
        nameTxd.setPreferredSize(new Dimension(200,30));
        userPwdLabel.setFont(centerFont);
        pwdFiled.setPreferredSize(new Dimension(200,30));
        loginBtn.setFont(centerFont);
        resetBtn.setFont(centerFont);

        contentPane.setBackground(Color.pink);

        //把组件加入到面板上
        centerPanel.add(userNameLabel);
        centerPanel.add(userPwdLabel);
        centerPanel.add(nameTxd);
        centerPanel.add(pwdFiled);
        centerPanel.add(loginBtn);
        centerPanel.add(resetBtn);

        loginBtn.addActionListener(loginHandler);//增加登录按键的事件监听
        loginBtn.addKeyListener(loginHandler);//增加键盘按键监听事件
        resetBtn.addActionListener(loginHandler);//增加重置按键的事件监听

        contentPane.add(titleNameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);

        //设置中间组件的位置,弹簧布局
        layoutCenter();

        //判断当前系统是否支持系统托盘
        if(SystemTray.isSupported()){
            systemTray=SystemTray.getSystemTray();//初始化系统托盘
            URL imgUrl = LoginView.class.getClassLoader().getResource("LoginView.jpg");
            trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());//初始化托盘图标
            trayIcon.setImageAutoSize(true);//托盘图标可以自动缩放大小
            try {
                systemTray.add(trayIcon);//将托盘图标加入到系统托盘组件中,并抛出异常
            } catch (AWTException e) {
                e.printStackTrace();
            }
            //创建一个事件响应,最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowIconified(WindowEvent e) {
                    LoginView.this.dispose();
                }
            });

            //托盘事件监听(鼠标单击一次托盘,窗口将会正常显示)
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCont=e.getClickCount();
                    if(clickCont==1){
                        LoginView.this.setExtendedState(JFrame.NORMAL);
                    }
                    LoginView.this.setVisible(true);
                }
            });
        }
        //设置loginBtn为默认按钮,按下回车键时默认响应loginBtn的键盘响应
        getRootPane().setDefaultButton(loginBtn);

        //设置窗口图片
        URL imgUrl=LoginView.class.getClassLoader().getResource("LoginView.jpg");
        setIconImage(new ImageIcon(imgUrl).getImage());
        //设置窗口基本参数
        setSize(600,400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    private void layoutCenter() {
        //获取组件的宽度Spring.width(组件名)用户名和用户名文本框组件
        Spring titleLabelWidth=Spring.width(userNameLabel);
        Spring titleTextWidth=Spring.width(nameTxd);
        Spring spaceWidth=Spring.constant(20);//userNameLabel和nameTxd的间距
        Spring totalWidth=Spring.sum(Spring.sum(titleLabelWidth,titleTextWidth),spaceWidth);
        int offSetX=totalWidth.getValue()/2;

         /*
        SpringLayout:布局管理器
        SpringLayout.Constraints:使用弹簧布局的容器里面的组件的布局约束,每个组件对应一个
        Spring:能够进行四则运算的整数
         */
        /*
        窗口相当于一个左顶角为原点的第四象限的坐标轴

         */
        //1.设置好用户名标签的位置(约束)
        SpringLayout.Constraints titleLabelCon=spLayout.getConstraints(userNameLabel);
        //用户名标签userNameLabel的西边参考centerPanel组件水平中心点方向左偏移offSetX的距离
        spLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offSetX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
        //设置用户名标签的垂直标签
        spLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
        //or使用setY()设置垂直偏移量
        // titleLabelCon.setY(Spring.constant(50));//垂直偏移量

        //2.设置用户名文本框nameTxd的位置(约束)
        spLayout.putConstraint(SpringLayout.WEST,nameTxd,20,SpringLayout.EAST,userNameLabel);
        spLayout.putConstraint(SpringLayout.NORTH,nameTxd,0,SpringLayout.NORTH,userNameLabel);

        //3.设置密码标签userPwdLabel的位置
        spLayout.putConstraint(SpringLayout.NORTH,userPwdLabel,20,SpringLayout.SOUTH,userNameLabel);
        spLayout.putConstraint(SpringLayout.EAST,userPwdLabel,0,SpringLayout.EAST,userNameLabel);

        //4.设置密码文本框pwdFiled的位置(约束)
        spLayout.putConstraint(SpringLayout.WEST,pwdFiled,20,SpringLayout.EAST,userPwdLabel);
        spLayout.putConstraint(SpringLayout.NORTH,pwdFiled,0,SpringLayout.NORTH,userPwdLabel);

        //5.设置登录按钮loginBtn的位置
        spLayout.putConstraint(SpringLayout.EAST,loginBtn,-20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
        spLayout.putConstraint(SpringLayout.NORTH,loginBtn,50,SpringLayout.SOUTH,pwdFiled);

        //6.设置重置按钮resetBtn的位置
        spLayout.putConstraint(SpringLayout.WEST,resetBtn,20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
        spLayout.putConstraint(SpringLayout.NORTH,resetBtn,50,SpringLayout.SOUTH,pwdFiled);
    }

    public static void main(String[] args){
        new LoginView();
    }


    public JTextField getNameTxd() {
        return nameTxd;
    }

    public JPasswordField getPwdFiled() {
        return pwdFiled;
    }
}

6.管理员增删改查页面(部分)

package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.net.URL;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import ext.MainViewTable;
import ext.MainViewTableModel;
import handler.AdminMainHandler;
import req.StudentRequest;
import res.TableDTO;
import service.StudentService;
import service.StudentServiceImpl;
import util.DimensionUtil;
/**
 * 管理员管理学生的主界面
 * 可在此实现增删改查
 * @author starry
 *
 */
public class MainView extends JFrame{
	
	
	public String lastPressKey;
	
	JPanel northPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));//JPanel默认流布局,组件居左
	public JButton btnAdd=new JButton("增加");
	public JButton updataBtn=new JButton("修改");
	public JButton delBtn=new JButton("删除");
	public JButton btnSave = new JButton("保存");
	JTextField searchTxt=new JTextField(15);
	JButton searchBtn=new JButton("查询");
	
	//搜索框与搜索按钮
    public JTextField searchField = new JTextField();
	
	JPanel southPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));//组件居右
	JButton preBtn=new JButton("上一页");
	JButton nextBtn =new JButton("下一页");
	
	
	//创建一个MainViewTable对象
  	public MainViewTable mianViewTable=new MainViewTable();
  	public MainViewTableModel mainViewTableModel;
	//声明一个MainHandler对象
	AdminMainHandler adminMainHandler;
	
	//
	private int pageNow=1;//默认显示第一页
	private int pageSize=10;//默认每页显示十条记录
	//构造方法
	public MainView() {
		
		super("主界面-学生成绩管理系统");
		Container contentPane=getContentPane();
		
		//创建一个MainHandler对象
		adminMainHandler = new AdminMainHandler(this);
		
		//放置北边的组件
		layoutNorth(contentPane);
		//放置中间的组件
		layoutCenter(contentPane);
		//放置南边的组件
		layoutSouth(contentPane);
		//基础设置
	    init();
	}

	private void layoutCenter(Container contentPane) {
		
		//调用数据库数据,创建一个StudentService和StudentRequest实例
		//获取从数据库中查询到的data与totalCount
		StudentService studentService=new StudentServiceImpl();
		StudentRequest request=new StudentRequest();
		request.setPageNow(pageNow);
		request.setPageSize(pageSize);
		request.setSearchKey(searchTxt.getText().trim());
		TableDTO tableDTO = studentService.retrieveStudents(request);
		Vector<Vector<Object>> data = tableDTO.getData();
		showPreNext(tableDTO.getTotalCount());
		
		
        //自定义的MainViewTableModel
		mainViewTableModel=MainViewTableModel.assembleModel(data);
		//将Table与TableModel关联
		mianViewTable.setModel(mainViewTableModel);
		//设置渲染方式
		mianViewTable.renderRule();
		
		
		
		 JScrollPane jScrollPane = new JScrollPane(mianViewTable);
		 contentPane.add(jScrollPane,BorderLayout.CENTER);
	}



	private void layoutSouth(Container contentPane) {
		
		//增加按钮事件监听
		preBtn.addActionListener(adminMainHandler);
		nextBtn.addActionListener(adminMainHandler);
		
		southPanel.add(preBtn);
		southPanel.add(nextBtn);
		contentPane.add(southPanel,BorderLayout.SOUTH);
	}
	
	/*
	 * 设置上一页与下一页按钮是否可见
	 */
	private void showPreNext(int totalCount) {
		if(pageNow==1) {
			preBtn.setVisible(false);
		}else {
			preBtn.setVisible(true);
		}
		//总共的页数
		int pageCount=0;
		if(totalCount%pageSize==0) {
			pageCount=totalCount/pageSize;
		}else {
			pageCount=totalCount/pageSize+1;
		}
		if(pageCount==pageNow) {
			nextBtn.setVisible(false);
		}else {
			nextBtn.setVisible(true);
		}
	}

	private void layoutNorth(Container contentPane) {
		
		//增加按钮事件监听
		btnAdd.addActionListener(adminMainHandler);
		btnSave.addActionListener(adminMainHandler);
		updataBtn.addActionListener(adminMainHandler);
		delBtn.addActionListener(adminMainHandler);
		searchBtn.addActionListener(adminMainHandler);

		
		northPanel.add(btnAdd);
		northPanel.add(btnSave);
		northPanel.add(updataBtn);
		northPanel.add(delBtn);
		northPanel.add(searchTxt);
		northPanel.add(searchBtn);
		btnAdd.setVisible(true);
		btnSave.setVisible(false);
		contentPane.add(northPanel,BorderLayout.NORTH);
	}
	
	private void init() {
		//设置窗口图片
        URL imgUrl=LoginView.class.getClassLoader().getResource("LoginView.jpg");
        setIconImage(new ImageIcon(imgUrl).getImage());
        //设置窗口基本参数
        setBounds(DimensionUtil.getBounds());
        //设置窗体完全充满整个屏幕
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(true);
        setVisible(true);
	}


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}
	
	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNow() {
		return pageNow;
	}

	
	public void reloadTable() {
		
		/*
		 *调用数据库数据,创建一个StudentService和StudentRequest实例
		 *获取从数据库中查询到的data与totalCount
		 *查询完后要更新Model的数据
		 */
		StudentService studentService=new StudentServiceImpl();
		StudentRequest request=new StudentRequest();
		request.setPageNow(pageNow);
		request.setPageSize(pageSize);
		request.setSearchKey(searchTxt.getText().trim());
		TableDTO tableDTO = studentService.retrieveStudents(request);
		Vector<Vector<Object>> data = tableDTO.getData();
		
		
		// 更新调用updataModel实现更新Model
		MainViewTableModel.uptadaModel(data);
		// 设置渲染方式
		mianViewTable.renderRule();
		// 调用上一页与下一页按钮是否可见
		showPreNext(tableDTO.getTotalCount());
	}
	

}

8.项目演示

8.1登录

8.2查询学生

8.3增加数据

8.4修改数据

8.5删除数据

更多推荐

『Java课设』JavaSwing+MySQL实现学生成绩管理系统