JAVA经典小项目-MYSQL学生成绩管理系统

工具:eclipse,MySQLWorkbench。环境:mysql8、jdk1.8
效果图:
JAVA经典小项目-MYSQL学生成绩管理系统_第1张图片
JAVA经典小项目-MYSQL学生成绩管理系统_第2张图片
JAVA经典小项目-MYSQL学生成绩管理系统_第3张图片
JAVA经典小项目-MYSQL学生成绩管理系统_第4张图片
JAVA经典小项目-MYSQL学生成绩管理系统_第5张图片
数据库部分设计:
五个表,三个视图,两个存储过程实现。成绩表和授课表级联更新删除。

create database newssms default character set gbk;
use newssms;
create table student
(
	sid  int not null,
	sname  varchar(20)   not null,
	scl  varchar(20)   not null,
	sgender varchar(20)  not null,
	smajor varchar(20)  not null ,
                primary key(sid)
);
create table teacher(
	tid  int not  null   ,
	tname  varchar(20)  not  null,
	tgender varchar(20) not null,
                primary key(tid)
);
create table  course
(
	cid  int not  null,
	cname  varchar(20)  not null,
	cre  int not null,
                primary key(cid)
);

create table score
(
	sid  int not null,
               sname varchar(20) null,
	cid int not null,
	cname varchar(20) not null,
	num varchar(20) not null,      	
	primary key(sid,cid),
	foreign key(sid) references student(sid)
	on delete cascade
	on update cascade,
	foreign key(cid) references course(cid)
	on delete cascade
	on update cascade
);

create table  teacher_has_course
(
	tid int not null,
               tname varchar(20) not null,
	cid int not null,
               cname varchar(20) not null,
	primary key (tid,cid),
	foreign key(tid) references teacher(tid)
	on delete cascade
	on update cascade,
	foreign key(cid) references course(cid)
	on delete cascade
	on update cascade
);

新建两个mysql账户student和teacher并设置权限:

create user 'student'@'%' IDENTIFIED BY '1234';
create user 'teacher'@'%' IDENTIFIED BY '1234';

grant select on newssms.* to 'student'@'%';
grant execute on newssms.* to 'student'@'%';

GRANT INSERT, UPDATE, DELETE ON newssms.student TO 'teacher'@'%';
GRANT INSERT, UPDATE, DELETE ON newssms.score TO 'teacher'@'%';
grant execute on newssms.* to 'teacher'@'%';
grant select on newssms.* to 'teacher'@'%';

工程目录:
JAVA经典小项目-MYSQL学生成绩管理系统_第6张图片
win里主要是系统界面的设计布局,大框架采用双选项卡,每个选项卡添加JPanel面板放组件。

//大框架初始化
public void init() {
		setBounds(200, 200, 1000, 560);
		setTitle("学生成绩管理系统");
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
				
		// 分类选项卡
		tabbedPane0 = new JTabbedPane();
		tabbedPane1 = new SelectView();
		tabbedPane2 = new ViewView();
		tabbedPane3 = new InsertView();
		tabbedPane4 = new UpdateView();
		tabbedPane5 = new DeleteView();
		tabbedPane6 = new ProcedureView();
		
		//设置字体
		tabbedPane0.setFont(tpfont);

		tabbedPane0.add("查询", tabbedPane1);
		tabbedPane0.add("视图", tabbedPane2);
		tabbedPane0.add("插入", tabbedPane3);
		tabbedPane0.add("更新", tabbedPane4);
		tabbedPane0.add("删除", tabbedPane5);
		tabbedPane0.add("存储过程", tabbedPane6);

		add(tabbedPane0, BorderLayout.CENTER);
		setVisible(true);
	}
//查询选项卡初始化
public SelectView() {
		
		setFont(MainWin.tpfont);
		
		selstudent = new JPanel();
		selscore = new JPanel();
		selteacher = new JPanel();
		selcourse = new JPanel();
		selthc = new JPanel();
		
		selstudent();
		selscore();
		selteacher();
		selcourse();
		selthc();
		
		add("学生表", selstudent);
		add("成绩表", selscore);
		add("教师表", selteacher);
		add("开课表", selcourse);
		add("教师授课表", selthc);

	}

为提交按钮添加监听来触发事件。查询结果放在JTable中:

//查询学生表面板
public void selstudent() {
		
		selstudent.setLayout(new BorderLayout());
		
		JButton submit = new JButton("查询学生表信息"); //查询按钮 
		submit.setFont(MainWin.tpfont);
		
		JTable tabletest = new JTable(); //表格
		tabletest.getTableHeader().setBackground(Color.yellow);
		tabletest.getTableHeader().setFont(MainWin.tpfont);
		tabletest.setRowHeight(22);
				
		selstudent.add(submit, BorderLayout.NORTH);
		selstudent.add(new JScrollPane(tabletest), BorderLayout.CENTER);
		
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				try {
				//数据库返回结果装入JTable
				tabletest.setModel(SelectAndViewSql.getSelectStu());
				} catch (SQLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		});
	}

数据库连接比较粗暴,新建连接后就一直保持连接不关闭,方便省事。查询完成返回一个DefaultTableModel类装入JTable显示出来,PreparedStatement等对象记得释放。

public static DefaultTableModel getSelectStu() throws SQLException {    
		
		sql = "select * from student";
		stmt = LoginWin.getConnection().createStatement();
	    rs = stmt.executeQuery(sql);
	    model.setRowCount(0);
		model.setColumnIdentifiers(new Object[] { "学号", "姓名", "班级", "性别", "专业" });
		while (rs.next()) {
			sid = rs.getInt("sid");
			sname = rs.getString("sname");
			scl = rs.getString("scl");
			sgender = rs.getString("sgender");
			smajor = rs.getString("smajor");
			model.addRow(new Object[] { sid, sname, scl, sgender, smajor });			
		}
		sql = null;
		stmt.close();
		rs.close();
		return model;
	}

end

文件下载

你可能感兴趣的:(Java,mysql,java)