Java+MySQL+Swing实现学生信息管理系统

阅读: 评论:0

Java+MySQL+Swing实现学生信息管理系统

Java+MySQL+Swing实现学生信息管理系统

目录

简介

准备

先创建学生表并插入学生数据

代码实现

定义学生类

连接数据库和操作JDBC的工具类

JDBCUtils

 DataBaseUtil

jdbc.properties

编写学生界面StudentManagementSystem

用于操作数据库的类StudentDao

运行结果


简介

可以实现对学生和学生成绩的增删改查

准备

要有idea并且安装MySQL,安装MySQL驱动如:mysql-connector-java-8.0.17.jar  可以在官网下载MySQL

先创建学生表并插入学生数据


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`number` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`grade` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`chinese` double(255, 0) NULL DEFAULT NULL,`math` double(255, 0) NULL DEFAULT NULL,`english` double(255, 0) NULL DEFAULT NULL,`total` double(11, 0) GENERATED ALWAYS AS (((`chinese` + `math`) + `english`)) VIRTUAL NULL,`average` double(11, 0) GENERATED ALWAYS AS ((((`chinese` + `math`) + `english`) / 3)) VIRTUAL NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 23 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '哈哈', '1', '四班', 100, 49, 50, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (9, '哈哈', '2', '三班', 43, 51, 69, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (11, 'hh', '11', '1', 1, 1, 1, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (12, '哈哈', '2', '三班', 90, 50, 50, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (13, '不是', '22', '二班', 67, 56, 51, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (14, '哈哈', '22', '一班', 91, 56, 50, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (17, '嘿嘿', '22', '三班', 89, 32, 59, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (20, '1111', '11', '五班', 454, 454, 456, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (21, '11', '11', '11', 1, 1, 1, DEFAULT, DEFAULT);
INSERT INTO `student` VALUES (22, '11', '11', '11', 1, 1, 1, DEFAULT, DEFAULT);SET FOREIGN_KEY_CHECKS = 1;

代码实现

定义学生类

ust.studentSystem;public class Student {private Integer id;/****/private String name;/****/private String number;/****/private String grade;/****/private double chinese;/****/private double math;/****/private double english;/****/private double total;/****/private double average;public Student(Integer id, String name, String number, String grade, double chinese, double math, double english) {this.id = id;this.name = name;this.number = ade = grade;this.chinese = chinese;this.math = lish = english;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + ''' +", number=" + number +", grade='" + grade + ''' +", chinese=" + chinese +", math=" + math +", english=" + english +", total=" + total +", averagee=" + average +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}public String getGrade() {return grade;}public void setGrade(String grade) {ade = grade;}public double getChinese() {return chinese;}public void setChinese(double chinese) {this.chinese = chinese;}public double getMath() {return math;}public void setMath(double math) {this.math = math;}public double getEnglish() {return english;}public void setEnglish(double english) {lish = english;}public double getTotal() {return total;}public void setTotal(double total) {al = total;}public double getAverage() {return average;}public void setAverage(double average) {this.average = average;}public Student() {}public Student(Integer id, String name, String number, String grade, double chinese, double math, double english, double total, double average) {this.id = id;this.name = name;this.number = ade = grade;this.chinese = chinese;this.math = lish = al = total;this.average = average;}
}

包含了学生的学号,姓名,班级,语文,数学,英语,总成绩和平均成绩。

 

连接数据库和操作JDBC的工具类

JDBCUtils

ust.studentSystem;import java.io.InputStream;
import java.sql.*;
import java.util.Properties;/*** 操作数据库的工具类*/
public class JDBCUtils {public static Connection getConnection() throws Exception{InputStream is &#SystemClassLoader().getResourceAsStream("jdbc.properties");Properties pros = new Properties();pros.load(is);//读取配置信息String user = Property("user");String password = Property("password");String url = Property("url");String driverClass = Property("driverClass");//加载驱动Class.forName(driverClass);//获取连接Connection conn = Connection(url,user,password);System.out.println(conn);return conn;}public static void closeResource(Connection conn , PreparedStatement ps){try {if(ps != null) {ps.close();}} catch (SQLException e) {e.printStackTrace();}try {if(conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}public static void closeResource(Connection conn , PreparedStatement ps , ResultSet rs){try {if(ps != null) {ps.close();}} catch (SQLException e) {e.printStackTrace();}try {if(conn != null) {conn.close();}} catch (SQLException e) {e.printStackTrace();}try {if(rs != null) {rs.close();}} catch (SQLException e) {e.printStackTrace();}}}

 DataBaseUtil

ust.studentSystem;import flect.Field;
import java.sql.*;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;public class DatabaseUtil {public static  void update(String sql,  args) {Connection conn = null;PreparedStatement ps = null;try {//1.获取数据库的连接conn = Connection();//2.获取PreparedStatement的实例 (或:预编译sql语句)ps = conn.prepareStatement(sql);//3.填充占位符for (int i = 0; i < args.length; i++) {ps.setObject(i + 1, args[i]);}//4.执行sql语句ps.execute();} catch (Exception e) {e.printStackTrace();} finally {//5.关闭资源JDBCUtils.closeResource(conn, ps);}}public static  <T> T getInstance(Class<T> clazz, String sql,  args) {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {// 1.获取数据库连接conn = Connection();// 2.预编译sql语句,得到PreparedStatement对象ps = conn.prepareStatement(sql);// 3.填充占位符for (int i = 0; i < args.length; i++) {ps.setObject(i + 1, args[i]);}// 4.执行executeQuery(),得到结果集:ResultSetrs = ps.executeQuery();// 5.得到结果集的元数据:ResultSetMetaDataResultSetMetaData rsmd = rs.getMetaData();// 6.1通过ResultSetMetaData得到columnCount,columnLabel;通过ResultSet得到列值int columnCount = ColumnCount();if (rs.next()) {T t = wInstance();for (int i = 0; i < columnCount; i++) {// 遍历每一个列// 获取列值Object columnVal = rs.getObject(i + 1);// 获取列的别名:列的别名,使用类的属性名充当String columnLabel = ColumnLabel(i + 1);// 6.2使用反射,给对象的相应属性赋值Field field = DeclaredField(columnLabel);field.setAccessible(true);field.set(t, columnVal);}return t;}} catch (Exception e) {e.printStackTrace();} finally {
// 7.关闭资源JDBCUtils.closeResource(conn, ps, rs);}return null;}public static <T> List<T> getForList(Class<T> clazz , String sql,  args){Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {// 1.获取数据库连接conn = Connection();// 2.预编译sql语句,得到PreparedStatement对象ps = conn.prepareStatement(sql);// 3.填充占位符for (int i = 0; i < args.length; i++) {ps.setObject(i + 1, args[i]);}// 4.执行executeQuery(),得到结果集:ResultSetrs = ps.executeQuery();// 5.得到结果集的元数据:ResultSetMetaDataResultSetMetaData rsmd = rs.getMetaData();// 6.1通过ResultSetMetaData得到columnCount,columnLabel;通过ResultSet得到列值int columnCount = ColumnCount();//创建集合ArrayList<T> list = new ArrayList<>();while (rs.next()) {T t = wInstance();for (int i = 0; i < columnCount; i++) {// 遍历每一个列// 获取列值Object columnVal = rs.getObject(i + 1);// 获取列的别名:列的别名,使用类的属性名充当String columnLabel = ColumnLabel(i + 1);// 6.2使用反射,给对象的相应属性赋值Field field = DeclaredField(columnLabel);field.setAccessible(true);field.set(t, columnVal);}list.add(t);}return list;} catch (Exception e) {e.printStackTrace();} finally {
// 7.关闭资源JDBCUtils.closeResource(conn, ps, rs);}return null;}
}

jdbc.properties

user=root
password=atguigu
url=jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
driverClass&#sql.cj.jdbc.Driver

编写学生界面StudentManagementSystem

ust.studentSystem;ust.studentSystem.service.StudentDao;import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;public class StudentManagementSystem extends JFrame {private StudentDao studentDao = new StudentDao();private JTable studentTable;private DefaultTableModel tableModel;public StudentManagementSystem() {setTitle("学生管理系统");setSize(800, 600);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);JPanel contentPane = new JPanel(new BorderLayout(10, 10));contentPane.ateEmptyBorder(10, 10, 10, 10));setContentPane(contentPane);JPanel buttonPanel = new JPanel(new FlowLayout());JButton insertButton = new JButton("插入");JButton updateButton = new JButton("修改");JButton deleteButton = new JButton("删除");JButton totalSortButton = new JButton("总成绩排序");JButton averageSortButton = new JButton("平均成绩排序");JButton chineseSortButton = new JButton("语文成绩排序");JButton mathSortButton = new JButton("数学成绩排序");JButton englishSortButton = new JButton("英语成绩排序");JButton numberSortButton = new JButton("按学号排序");JButton nameSelectButton = new JButton("姓名搜索");JButton gradeSelectButton = new JButton("班级搜索");JButton numberSelectButton = new JButton("学号搜索");JButton chineseScoreButton = new JButton("语文优秀率和不及格率");JButton mathScoreButton = new JButton("数学优秀率和不及格率");JButton englishScoreButton = new JButton("英语优秀率和不及格率");buttonPanel.add(insertButton);buttonPanel.add(updateButton);buttonPanel.add(deleteButton);buttonPanel.add(totalSortButton);buttonPanel.add(averageSortButton);buttonPanel.add(chineseSortButton);buttonPanel.add(mathSortButton);buttonPanel.add(englishSortButton);buttonPanel.add(numberSortButton);buttonPanel.add(nameSelectButton);buttonPanel.add(gradeSelectButton);buttonPanel.add(numberSelectButton);buttonPanel.add(chineseScoreButton);buttonPanel.add(mathScoreButton);buttonPanel.add(englishScoreButton);contentPane.add(buttonPanel, BorderLayout.NORTH);tableModel = new DefaultTableModel(new String[]{"编号", "姓名","学号","班级", "语文成绩", "数学成绩", "英语成绩","总成绩","平均成绩"}, 0);studentTable = new JTable(tableModel);JScrollPane scrollPane = new JScrollPane(studentTable);contentPane.add(scrollPane, BorderLayout.CENTER);List<Student> students = All();updateTable(students);insertButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {
//                String id = JOptionPane.showInputDialog("请输入学号:");String name = JOptionPane.showInputDialog("请输入姓名:");String number = JOptionPane.showInputDialog("请输入学号:");String className = JOptionPane.showInputDialog("请输入班级:");String chinese = JOptionPane.showInputDialog("请输入语文成绩:");String math = JOptionPane.showInputDialog("请输入数学成绩:");String english = JOptionPane.showInputDialog("请输入英语成绩:");
//                Student student = new Student(id, name, number, className,Double.parseDouble(chinese),Double.parseDouble(math),Double.parseDouble(english));Student student = new Student(0,name, number, className,Double.parseDouble(chinese),Double.parseDouble(math),Double.parseDouble(english));studentDao.insert(student);List<Student> students = All();updateTable(students);}});deleteButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {int[] selectedRows = SelectedRows();for (int i = selectedRows.length - 1; i >= 0; i--) {int index = selectedRows[i];int id = (Integer) ValueAt(index, 0);studentDao.delete(id);}List<Student> students = All();updateTable(students);}});updateButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {int selectedRow = SelectedRow();if (selectedRow == -1) {JOptionPane.showMessageDialog(StudentManagementSystem.this, "请选择要修改的学生信息");return;}Integer id = (Integer) ValueAt(selectedRow, 0);String name = (String) ValueAt(selectedRow, 1);String number = (String) ValueAt(selectedRow, 2);String className = (String) ValueAt(selectedRow, 3);String chinese = String.ValueAt(selectedRow, 4));String math = String.ValueAt(selectedRow, 5));String english = String.ValueAt(selectedRow, 6));JTextField idField = new JTextField(id);JTextField nameField = new JTextField(name);JTextField numberField = new JTextField(number);JTextField gradeField = new JTextField(className);JTextField chineseField = new JTextField(chinese);JTextField mathField = new JTextField(math);JTextField englishField = new JTextField(english);JPanel panel = new JPanel(new GridLayout(7, 2));
//                panel.add(new JLabel("编号:"));
//                panel.add(idField);panel.add(new JLabel("姓名:"));panel.add(nameField);panel.add(new JLabel("学号:"));panel.add(numberField);panel.add(new JLabel("班级:"));panel.add(gradeField);panel.add(new JLabel("语文成绩:"));panel.add(chineseField);panel.add(new JLabel("数学成绩:"));panel.add(mathField);panel.add(new JLabel("英语成绩:"));panel.add(englishField);int result = JOptionPane.showConfirmDialog(StudentManagementSystem.this, panel, "修改学生信息",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);if (result == JOptionPane.OK_OPTION) {Student student = new Student(id, Text(),Text(), Text(),Double.Text()), Double.Text()), Double.Text()));studentDao.update(student);List<Student> students = All();updateTable(students);}}});//总成绩排序totalSortButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = alSort();updateTable(students);}});//平均成绩排序averageSortButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = studentDao.averageSort();updateTable(students);}});//语文成绩排序chineseSortButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = studentDao.chineseSort();updateTable(students);}});//数学成绩排序mathSortButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = studentDao.mathSort();updateTable(students);}});//英语成绩排序englishSortButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = lishSort();updateTable(students);}});//学号成绩排序numberSortButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = studentDao.numberSort();updateTable(students);}});//按照姓名搜索nameSelectButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JTextField jTextField= new JTextField(10);int result = JOptionPane.showConfirmDialog(StudentManagementSystem.this, jTextField, "查找学生",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);if (result == JOptionPane.OK_OPTION){String name = Text();List<Student> students = studentDao.nameSelect(name);if(students.size() == 0){JOptionPane.showMessageDialog(StudentManagementSystem.this, "学生姓名输入有误", "无学生", JOptionPane.ERROR_MESSAGE);return;}updateTable(students);}}});//按照班级搜索gradeSelectButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JTextField jTextField= new JTextField(10);int result = JOptionPane.showConfirmDialog(StudentManagementSystem.this, jTextField, "查找班级",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);if (result == JOptionPane.OK_OPTION){String grade = Text();List<Student> students = adeSelect(grade);if(students.size() == 0){JOptionPane.showMessageDialog(StudentManagementSystem.this, "班级输入有误", "无班级", JOptionPane.ERROR_MESSAGE);return;}updateTable(students);}}});//按照学号搜索numberSelectButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JTextField jTextField= new JTextField(10);int result = JOptionPane.showConfirmDialog(StudentManagementSystem.this, jTextField, "学号查找",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);if (result == JOptionPane.OK_OPTION){String number = Text();List<Student> students = studentDao.numberSelect(number);if(students.size() == 0){JOptionPane.showMessageDialog(StudentManagementSystem.this, "学号输入有误", "无学号", JOptionPane.ERROR_MESSAGE);return;}updateTable(students);}}});//语文优秀率和不及格率chineseScoreButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = All();ArrayList<Student> yiBan = new ArrayList<>();ArrayList<Student> erBan = new ArrayList<>();ArrayList<Student> sanBan = new ArrayList<>();ArrayList<Student> siBan = new ArrayList<>();ArrayList<Student> wuBan = new ArrayList<>();for (int i = 0; i < students.size(); i++) {Student student = (i);String stuGrade = Grade();if(stuGrade.equals("一班")){yiBan.add(student);}if(stuGrade.equals("二班")){erBan.add(student);}if(stuGrade.equals("三班")){sanBan.add(student);}if(stuGrade.equals("四班")){siBan.add(student);}if(stuGrade.equals("五班")){wuBan.add(student);}}String yiBanYouXiuLv = chineseLv(yiBan);String erBanYouXiuLv = chineseLv(erBan);String sanBanYouXiuLv = chineseLv(sanBan);String siBanYouXiuLv = chineseLv(siBan);String wuBanYouXiuLv = chineseLv(wuBan);JOptionPane.showMessageDialog(StudentManagementSystem.this, yiBanYouXiuLv + erBanYouXiuLv +sanBanYouXiuLv +"n"+ siBanYouXiuLv +wuBanYouXiuLv, "语文优秀率和不及格率", JOptionPane.PLAIN_MESSAGE);}});//数学优秀率和不及格率mathScoreButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = All();ArrayList<Student> yiBan = new ArrayList<>();ArrayList<Student> erBan = new ArrayList<>();ArrayList<Student> sanBan = new ArrayList<>();ArrayList<Student> siBan = new ArrayList<>();ArrayList<Student> wuBan = new ArrayList<>();for (int i = 0; i < students.size(); i++) {Student student = (i);String stuGrade = Grade();if(stuGrade.equals("一班")){yiBan.add(student);}if(stuGrade.equals("二班")){erBan.add(student);}if(stuGrade.equals("三班")){sanBan.add(student);}if(stuGrade.equals("四班")){siBan.add(student);}if(stuGrade.equals("五班")){wuBan.add(student);}}String yiBanYouXiuLv = mathLv(yiBan);String erBanYouXiuLv = mathLv(erBan);String sanBanYouXiuLv = mathLv(sanBan);String siBanYouXiuLv = mathLv(siBan);String wuBanYouXiuLv = mathLv(wuBan);JOptionPane.showMessageDialog(StudentManagementSystem.this, yiBanYouXiuLv + erBanYouXiuLv +sanBanYouXiuLv +"n"+ siBanYouXiuLv +wuBanYouXiuLv, "数学优秀率和不及格率", JOptionPane.PLAIN_MESSAGE);}});//英语优秀率和不及格率englishScoreButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {List<Student> students = All();ArrayList<Student> yiBan = new ArrayList<>();ArrayList<Student> erBan = new ArrayList<>();ArrayList<Student> sanBan = new ArrayList<>();ArrayList<Student> siBan = new ArrayList<>();ArrayList<Student> wuBan = new ArrayList<>();for (int i = 0; i < students.size(); i++) {Student student = (i);String stuGrade = Grade();if(stuGrade.equals("一班")){yiBan.add(student);}if(stuGrade.equals("二班")){erBan.add(student);}if(stuGrade.equals("三班")){sanBan.add(student);}if(stuGrade.equals("四班")){siBan.add(student);}if(stuGrade.equals("五班")){wuBan.add(student);}}String yiBanYouXiuLv = englishLv(yiBan);String erBanYouXiuLv = englishLv(erBan);String sanBanYouXiuLv = englishLv(sanBan);String siBanYouXiuLv = englishLv(siBan);String wuBanYouXiuLv = englishLv(wuBan);JOptionPane.showMessageDialog(StudentManagementSystem.this, yiBanYouXiuLv + erBanYouXiuLv +sanBanYouXiuLv + "n" +siBanYouXiuLv +wuBanYouXiuLv, "英语优秀率和不及格率", JOptionPane.PLAIN_MESSAGE);}});}private String chineseLv(List<Student> students){if(students.size()==0){return "0";}double countNice = 0.0;double countNo = 0.0;String grade = "";grade = (0).getGrade();for (Student student : students) {double chinese = Chinese();if (chinese >= 90) {countNice++;}if(chinese < 60){countNo++;}}double nice = countNice/(students.size());String  str = String.format("%.2f",nice);double youXiu = Double.parseDouble(str);double no = countNo/(students.size());String  str1 = String.format("%.2f",no);double buJiGe = Double.parseDouble(str1);return grade + "语文成绩优秀率的百分比为:" + youXiu*100 +"%        " + ",不及格率的百分比为 " + buJiGe*100 +"%       ";}private String mathLv(List<Student> students){if(students.size()==0){return "0";}double countNice = 0.0;double countNo = 0.0;String grade = "";grade = (0).getGrade();for (Student student : students) {double chinese = Math();if (chinese >= 90) {countNice++;}if(chinese < 60){countNo++;}}double nice = countNice/(students.size());String  str = String.format("%.2f",nice);double youXiu = Double.parseDouble(str);double no = countNo/(students.size());String  str1 = String.format("%.2f",no);double buJiGe = Double.parseDouble(str1);return grade + "数学成绩优秀率的百分比为:" + youXiu*100 +"%        " + ",不及格率的百分比为 " + buJiGe*100 +"%       ";}private String englishLv(List<Student> students){if(students.size()==0){return "0";}double countNice = 0.0;double countNo = 0.0;String grade = "";grade = (0).getGrade();for (Student student : students) {double chinese = English();if (chinese >= 90) {countNice++;}if(chinese < 60){countNo++;}}double nice = countNice/(students.size());String  str = String.format("%.2f",nice);double youXiu = Double.parseDouble(str);double no = countNo/(students.size());String  str1 = String.format("%.2f",no);double buJiGe = Double.parseDouble(str1);return grade + "英语成绩优秀率的百分比为:" + youXiu*100 +"%        " + ",不及格率的百分比为 " + buJiGe*100 +"%       ";}private void updateTable(List<Student> students) {tableModel.setRowCount(0);for (Student student : students) {Object[] row = new Object[]{Id(),Name(),Number(),Grade(),Chinese(),Math(),English(),Total(),Average()};tableModel.addRow(row);}}public static void main(String[] args) {StudentManagementSystem frame = new StudentManagementSystem();frame.setVisible(true);}
}

用于操作数据库的类StudentDao

ust.studentSystem.service;ust.studentSystem.DatabaseUtil;
ust.studentSystem.Student;import java.util.List;public class StudentDao {public List<Student> getAll() {String sql = "select * from student";List<Student> students = ForList(Student.class, sql);return students;}public void insert(Student student) {String sql = "insert into student (name,number,grade,chinese,math,english) values(?,?,?,?,?,?)";DatabaseUtil.update(Name(),Number(),Grade(),Chinese(),Math(),English());}public void delete(Integer id) {String sql = "delete from student where id = ?";DatabaseUtil.update(sql,id);}public void update(Student student) {String sql = "update student set name = ?,number = ?, grade = ?,chinese = ?,math = ?,english = ? where id = ?";DatabaseUtil.update(Name(),Number(),Grade(),Chinese(),Math(),English(),Id());}public List<Student> totalSort(){String sql = "select * from student order by total desc";List<Student> forList = ForList(Student.class, sql);return forList;}public List<Student> averageSort(){String sql = "select * from student order by average desc";List<Student> forList = ForList(Student.class, sql);return forList;}public List<Student> chineseSort(){String sql = "select * from student order by chinese desc";List<Student> forList = ForList(Student.class, sql);return forList;}public List<Student> mathSort(){String sql = "select * from student order by math desc";List<Student> forList = ForList(Student.class, sql);return forList;}public List<Student> englishSort(){String sql = "select * from student order by english desc";List<Student> forList = ForList(Student.class, sql);return forList;}public List<Student> numberSort(){String sql = "select * from student order by number asc";List<Student> forList = ForList(Student.class, sql);return forList;}public List<Student> nameSelect(String name){String sql = "select * from student where name = ?";List<Student> forList = ForList(Student.class, sql,name);return forList;}public List<Student> gradeSelect(String grade){String sql = "select * from student where grade = ?";List<Student> forList = ForList(Student.class, sql,grade);return forList;}public List<Student> numberSelect(String number){String sql = "select * from student where number = ?";List<Student> forList = ForList(Student.class, sql,number);return forList;}public List<Student> chineseSelect(String chinese){String sql = "select * from student where chinese = ?";List<Student> forList = ForList(Student.class, sql,chinese);return forList;}public List<Student> mathSelect(String math){String sql = "select * from student where math = ?";List<Student> forList = ForList(Student.class, sql,math);return forList;}public List<Student> englishSelect(String english){String sql = "select * from student where english = ?";List<Student> forList = ForList(Student.class, sql,english);return forList;}
}

运行结果

本文发布于:2024-01-29 10:57:28,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170649705014795.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:信息管理系统   学生   Java   MySQL   Swing
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23