JAVA课程实验报告单(12)---数据库系统设计
一、实验目的
- 掌握MVC设计方法。
- 利用JDBC连接数据库,并对数据库进行操作。
二、实验内容
(1)设计一个数据库Student,包含成绩表Score,其中属性包含学号,姓名,专业,班级,平均成绩。字段名和类型自行定义,但需合理。
(2)设计一个界面利用Jtable组件和其对应的Model,将数据库Student中的数据读出并按照平均成绩降序排序。
实现界面源代码:
package ui;
import component.EditComponent;
import component.ScoreComponent;
import service.impl.StudentServiceImpl;
import service.StudentService;
import utils.ScreenUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 主界面
*/
public class MainFrame {
public static StudentService studentService = new StudentServiceImpl();
JFrame jf = new JFrame("学生成绩管理系统");
/**
* 定义窗口的宽度
*/
final int WIDTH = 700;
/**
* 定义窗口的高度
*/
final int HEIGHT = 500;
/**
* 创建工具条
*/
JToolBar jToolBar;
JButton showButton;
JButton editButton;
JPanel mainPanel;
/**
* 当前是成绩模块还是编辑模块
*/
String module = "学生成绩";
/**
* 默认是成绩模块
*/
ScoreComponent scoreComponent;
EditComponent editComponent;
public void init(){
//设置关闭窗口为结束程序
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口相关的属性
jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
//设置窗口大小不可变
jf.setResizable(false);
/*
* 组装工具条
*/
jToolBar = new JToolBar();
showButton = new JButton("学生成绩");
editButton = new JButton("编辑");
//对 点击 学生成绩按钮 的处理 --> 刷新数据
showButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(module)){
//说明原本就是在学生成绩模块
//那么只需要重新获取数据
scoreComponent.requestData();
}else{
//修改 module
module = "学生成绩";
//去除editComponent
mainPanel.remove(editComponent);
//加入scoreComponent
mainPanel.add(scoreComponent);
scoreComponent.requestData();
//刷新页面,重绘面板
mainPanel.repaint();
// //使重绘的面板确认生效
mainPanel.validate();
}
}
});
//对 点击 编辑模块 的处理
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!module.equals("编辑")) {
//修改 module
module = "编辑";
//移除面板中的组件
mainPanel.remove(scoreComponent);
//添加要切换的面板
mainPanel.add(editComponent);
//刷新页面,重绘面板
mainPanel.repaint();
//使重绘的面板确认生效
mainPanel.validate();
}
}
});
jToolBar.add(showButton);
jToolBar.addSeparator();
jToolBar.add(editButton);
//设置工具条背景颜色
jToolBar.setBackground(new Color(197,255,103));
//向窗口中添加工具条
jf.add(jToolBar, BorderLayout.NORTH);
scoreComponent = new ScoreComponent(jf);
editComponent = new EditComponent(jf);
mainPanel = new JPanel(new BorderLayout());
//默认是成绩面板
mainPanel.add(scoreComponent);
jf.add(mainPanel);
jf.setVisible(true);
}
}
(3)设计如图1.1所示界面,向数据库中添加,删除,修改数据。(执行相应操作前应进行有效性检查,即数据库中是否有与学号相一致的主键,如果有曾不能添加,并提示系统中已有该生数据,删除时则提示是否删除,点击确定删除。修改也做类似操作,在添加时必须保证所有选项不为空,删除和判断是仅需判断学号是否为空即可。)
代码运行结果截图:
实现的类源代码:
StudentService接口代码:
package service;
import javabean.Student;
import java.util.List;
public interface StudentService {
/**
* 查询学生信息,按照平均成绩降序排列
*
* @return 学生信息
*/
List<Student> queryByScoreDesc();
/**
* 添加学生信息
*
* @param name 姓名
* @param stuId 学号
* @param major 专业
* @param classNum 班级编号
* @param avgScore 平均成绩
*/
void add(String name, Integer stuId, String major, Integer classNum, Double avgScore);
/**
* 通过学号查询学生信息
*
* @param id 学号
* @return 学生信息
*/
Student queryByStuId(Integer id);
/**
* 修改学生信息
*
* @param name 姓名
* @param stuId 学号
* @param major 专业
* @param classNum 班级编号
* @param avgScore 平均成绩
*/
void updateByStuId(String name,Integer stuId,String major,Integer classNum,Double avgScore);
/**
* 通过学号删除学生信息
*
* @param id 学号
*/
void delByStuId(Integer id);
}
StudentServiceImpl代码:
package service.impl;
import dao.impl.StudentDaoImpl;
import dao.StudentDao;
import javabean.Student;
import service.StudentService;
import java.util.List;
/**
* Student服务层实现
*/
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
@Override
public List<Student> queryByScoreDesc() {
return studentDao.queryByScoreDesc();
}
@Override
public void add(String name, Integer stuId, String major, Integer classNum, Double avgScore) {
studentDao.add(name, stuId, major, classNum, avgScore);
}
@Override
public Student queryByStuId(Integer id) {
return studentDao.queryByStuId(id);
}
@Override
public void updateByStuId(String name, Integer stuId, String major, Integer classNum, Double avgScore) {
studentDao.updateByStuId(name, stuId, major, classNum, avgScore);
}
@Override
public void delByStuId(Integer stuId) {
studentDao.delByStuId(stuId);
}
}
三、实验心得
使用不同的数据库驱动版本可能会导致数据库和系统时区差异,或者低版本的警告。动版本可能会导致数据库和系统时区差异,或者低版本的警告。
在使用数据库驱动版本时要充分考虑,结合需求去使用。