🍅程序员小王的博客:程序员小王的博客
🍅 欢迎点赞 👍 收藏 ⭐留言 📝
🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕
🍅java自学的学习路线:java自学的学习路线
一、前言
前几天有一位学妹让我做一个毕业设计选题管理系统,使用java Swing+JDBC+mysql实现,然后功能大概是:
能够导入分配后的名单;
能够从中分离出学生、教师名单,存入学生表和对应关系表中;
教师第一次登录后要补充完整自己的信息:职称、学历、学位。
教师可以输入自己带的学生的毕设题目信息,如每个学生的题目、课题性质、
然后我这几天也没什么事嘛就给他做了一下,后期他说又不要了,然后我就将这个项目拿出来和大家一起学习,如果对这个项目感兴趣,可以自己下载数据库设计+源码:
https://download.csdn.net/download/weixin_44385486/85665117
- 开发工具
用途 | 工具名 |
---|---|
代码编写工具 | IDEA 2021.1.1 x64 |
数据库 | mysql8 |
Java环境 | JDK-1.8 |
二、数据库设计
- 数据库模型
数据库名: studentassignment
文档版本: V1.0.0
文档描述: 毕业设计选题管理系统数据库表设计描述
表名 | 说明 |
---|---|
account | |
student | |
studentnototopicno | |
teacher | |
topic |
表名: account
说明:
数据列:
序号 | 名称 | 数据类型 | 长度 | 小数位 | 允许空值 | 主键 | 默认值 | 说明 |
---|---|---|---|---|---|---|---|---|
1 | id | int | 10 | 0 | N | Y | ||
2 | userNo | varchar | 45 | 0 | Y | N | ||
3 | password | varchar | 45 | 0 | Y | N | ||
4 | role | varchar | 45 | 0 | Y | N |
表名: student
说明:
数据列:
序号 | 名称 | 数据类型 | 长度 | 小数位 | 允许空值 | 主键 | 默认值 | 说明 |
---|---|---|---|---|---|---|---|---|
1 | id | int | 10 | 0 | N | Y | ||
2 | studentNo | varchar | 45 | 0 | Y | N | ||
3 | name | varchar | 45 | 0 | Y | N | ||
4 | sex | varchar | 45 | 0 | Y | N | ||
5 | age | int | 10 | 0 | Y | N | ||
6 | birthday | date | 10 | 0 | Y | N | ||
7 | timeOfEnrollment | date | 10 | 0 | Y | N | ||
8 | department | varchar | 45 | 0 | Y | N |
表名: studentnototopicno
说明:
数据列:
序号 | 名称 | 数据类型 | 长度 | 小数位 | 允许空值 | 主键 | 默认值 | 说明 |
---|---|---|---|---|---|---|---|---|
1 | id | int | 10 | 0 | N | Y | ||
2 | studentNo | varchar | 45 | 0 | Y | N | ||
3 | topicNo | varchar | 45 | 0 | Y | N |
表名: teacher
说明:
数据列:
序号 | 名称 | 数据类型 | 长度 | 小数位 | 允许空值 | 主键 | 默认值 | 说明 |
---|---|---|---|---|---|---|---|---|
1 | id | int | 10 | 0 | N | Y | ||
2 | teacherNo | varchar | 45 | 0 | Y | N | ||
3 | name | varchar | 45 | 0 | Y | N | ||
4 | age | int | 10 | 0 | Y | N | ||
5 | sex | varchar | 45 | 0 | Y | N | ||
6 | birthday | date | 10 | 0 | Y | N | ||
7 | department | varchar | 45 | 0 | Y | N |
表名: topic
说明:
数据列:
序号 | 名称 | 数据类型 | 长度 | 小数位 | 允许空值 | 主键 | 默认值 | 说明 |
---|---|---|---|---|---|---|---|---|
1 | id | int | 10 | 0 | N | Y | ||
2 | topicNo | varchar | 45 | 0 | Y | N | ||
3 | topicName | varchar | 45 | 0 | Y | N | ||
4 | content | text | 65535 | 0 | Y | N | ||
5 | teacherNo | varchar | 45 | 0 | Y | N | ||
6 | teacherName | varchar | 45 | 0 | Y | N | ||
7 | studentCount | int | 10 | 0 | Y | N |
- jdbc实现
public class DBHelper {
private static final String url = "jdbc:mysql://127.0.0.1/studentassignment?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
private static final String name = "com.mysql.cj.jdbc.Driver";
private static final String user = "root";
private static final String password = "root";
private Connection conn = null;
public PreparedStatement pst = null;
public DBHelper(String sql) {
try {
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库连接错误", "提示", JOptionPane.ERROR_MESSAGE);
}
}
public void close() {
try {
this.pst.close();
this.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
三、项目功能介绍及实现代码
1、java项目结构
使用的mysql8,然后使用Swing+JDBC+mysql实现
2、项目具体实现及源码
(1)登录页面
public class Login extends JFrame implements ActionListener {
private JTextField usernameField;//文本框
private JPasswordField passwordField;//密码框
private JRadioButton teacherRadioButton;
private JRadioButton studentRadioButton;
private JRadioButton adminRadioButton;
public Login() {
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JPanel jp5 = new JPanel();
JLabel titleLabel = new JLabel("毕设学生分配系统");
titleLabel.setFont(new Font("楷体", Font.BOLD, 30));
titleLabel.setForeground(Color.BLUE);
JLabel usernameLabel = new JLabel("用户名");
usernameLabel.setFont(new Font("楷体", Font.BOLD, 15));
usernameLabel.setForeground(Color.BLUE);
JLabel passwordLabel = new JLabel("密码 ");
passwordLabel.setFont(new Font("楷体", Font.BOLD, 15));
passwordLabel.setForeground(Color.BLUE);
JButton loginButton = new JButton("login");
loginButton.setForeground(Color.BLUE);
JButton cancelButton = new JButton("cancel");
cancelButton.setForeground(Color.BLUE);
usernameField = new JTextField(15);
passwordField = new JPasswordField(15);
adminRadioButton = new JRadioButton("管理员");
adminRadioButton.setFont(new Font("楷体", Font.BOLD, 15));
adminRadioButton.setForeground(Color.BLUE);
teacherRadioButton = new JRadioButton("教师");
teacherRadioButton.setFont(new Font("楷体", Font.BOLD, 15));
teacherRadioButton.setForeground(Color.BLUE);
studentRadioButton = new JRadioButton("学生");
studentRadioButton.setFont(new Font("楷体", Font.BOLD, 15));
studentRadioButton.setForeground(Color.BLUE);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(teacherRadioButton);
buttonGroup.add(studentRadioButton);
this.setLayout(new GridLayout(5, 1));
jp1.add(titleLabel);
jp2.add(usernameLabel);
jp2.add(usernameField);
jp3.add(passwordLabel);
jp3.add(passwordField);
jp4.add(teacherRadioButton);
jp4.add(studentRadioButton);
jp4.add(adminRadioButton);
jp5.add(loginButton);
jp5.add(cancelButton);
this.add(jp1);
this.add(jp2);
Component add = this.add(jp3);
this.add(jp4);
this.add(jp5);
this.setBackground(Color.gray);
this.setTitle("欢迎进入毕设学生分配系统");
this.setSize(400, 250);
this.setLocation(400, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
loginButton.addActionListener(this);//事件监听
loginButton.setActionCommand("login");//命令
cancelButton.addActionListener(this);
cancelButton.setActionCommand("close");
}
(2)老师登录系统
- 登录首页
- 可以发布论文题目
- 发布的选题
- 老师可以修改个人信息
(3)学生登录后
- 可以修改个人信息
- 可以选题
public class ChooseTopicModel extends AbstractTableModel {
private Vector<Vector<String>> rowData;
private Vector<String> columnNames;
private DBHelper dbHelper;
private ResultSet resultSet;
void init() {
initTable();
}
private void initTable(){
columnNames = new Vector<>();
columnNames.add("课题号");
columnNames.add("课题名");
columnNames.add("内容");
columnNames.add("教师号");
columnNames.add("教师姓名");
columnNames.add("学生数");
rowData = new Vector<>();
String sql = "select * from topic";
try {
dbHelper = new DBHelper(sql);
resultSet = dbHelper.pst.executeQuery();
while (resultSet.next()) {
Vector<String> row = new Vector<>();
row.add(resultSet.getString(2));
row.add(resultSet.getString(3));
row.add(resultSet.getString(4));
row.add(resultSet.getString(5));
row.add(resultSet.getString(6));
row.add(resultSet.getString(7));
rowData.add(row);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
dbHelper.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 如果选题之后,有人选了,就提示
- 如果选择了一门再选择就提示,不要多选
//学生选了某一门课程设计(使课程待选人数-1 并且一个人只能选一门)
boolean isChoose(String topicNo, String studentNo){
boolean success=false;
String sql="select * from topic where topicNo=?";
try {
dbHelper=new DBHelper(sql);
dbHelper.pst.setString(1,topicNo);
resultSet = dbHelper.pst.executeQuery();
while (resultSet.next()){
String studentCount=resultSet.getString(7);
if (Integer.parseInt(studentCount)>0){
sql="select * from studentNoToTopicNo where studentNo=?";
dbHelper=new DBHelper(sql);
dbHelper.pst.setString(1,studentNo);
resultSet=dbHelper.pst.executeQuery();
if (!resultSet.next()){
sql="update topic set studentCount=studentCount-1 where topicNo=?";
try {
dbHelper=new DBHelper(sql);
dbHelper.pst.setString(1,topicNo);
dbHelper.pst.executeUpdate();
success=true;
} catch (SQLException e) {
e.printStackTrace();
}finally {
dbHelper.close();
}
}else {
JOptionPane.showMessageDialog(null,"你已经选过一门课程设计,请勿选择多门");
}
}else {
JOptionPane.showMessageDialog(null,"该门课程已被选完,请重新选课");
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
dbHelper.close();
}
return success;
}
- 之后查看自己的毕业选题
- 选题目之后,那个老师发布的题目,就相当于选了那个老师
(4)管理员登录
- 添加学生信息
void init(String teacherNo) {
columnNames = new Vector<>();
columnNames.add("课题号");
columnNames.add("课题名");
columnNames.add("内容");
columnNames.add("教师号");
columnNames.add("教师姓名");
columnNames.add("学生数");
rowData = new Vector<>();
String sql;
try {
if (teacherNo.equals("all")) {
sql = "select * from topic";
dbHelper = new DBHelper(sql);
} else {
sql = "select * from topic where teacherNo=?";
dbHelper = new DBHelper(sql);
dbHelper.pst.setString(1, teacherNo);
}
resultSet = dbHelper.pst.executeQuery();
while (resultSet.next()) {
Vector<String> row = new Vector<>();
row.add(resultSet.getString(2));
row.add(resultSet.getString(3));
row.add(resultSet.getString(4));
row.add(resultSet.getString(5));
row.add(resultSet.getString(6));
row.add(resultSet.getString(7));
rowData.add(row);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
dbHelper.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 添加删除老师信息
- 删除老师信息
public boolean deleteStudentInfo(String teacherNo) {
boolean success = false;
String sql = "delete from teacher where teacherNo=?";
DBHelper dbHelper = new DBHelper(sql);
try {
dbHelper.pst.setString(1, teacherNo);
dbHelper.pst.executeUpdate();
success = true;
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除教师信息失败");
} finally {
dbHelper.close();
}
return success;
}
- 更新老师信息
boolean updateTeacherInfo(String name, int age, String sex, String birthday) {
boolean success = false;
String sql = "update teacher set name=?,age=?,sex=?,birthday=? where teacherNo=?";
DBHelper dbHelper1 = new DBHelper(sql);
try {
dbHelper1.pst.setString(1, name);
dbHelper1.pst.setInt(2, age);
dbHelper1.pst.setString(3, sex);
dbHelper1.pst.setString(4, birthday);
dbHelper1.pst.setString(5, this.teacherNo);
dbHelper1.pst.executeUpdate();
success = true;
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "更新失败请重试");
} finally {
dbHelper1.close();
}
init(teacherNo);
return success;
}
- 查看选题列表及那我同学选了那个毕业设计
四、总结
以上就是毕业分配吸引的全部功能和部分代码了,大家可以参考一下,或者直接模拟创建一个项目,然后代码复制进去。然后源代码实在太多,并且各个结构也比较乱,如有需求可与我联系
🧐 微信公众号:搜索《程序员小王》
📒原创不易,如果觉得不错请给我一个,三连、收藏、加关注,需要资料的同学可以私聊我!
😉 有问题可以点击下方私信交流 😆
如果我的博客对您有帮助 再次希望大家点赞 👍 收藏 ⭐留言 📝 啦 、感谢大家对我支持,让我有了更多的创作动力,希望我的博客能帮助到你
- 如果需要项目源码可以自己下载数据库设计+源码:
https://download.csdn.net/download/weixin_44385486/85665117
本文含有隐藏内容,请 开通VIP 后查看