项目四:学生信息管理系统
1、 ?項目概述
1.1 ?項目主要內容
1.簡述本項目實現的主要內容和目標
(1)實現一個簡單的學生信息管理的程序StudentInfo。
(2)教學管理人員能夠使用StudentInfo程序對學生基本信息、課程信息進行管理,包括數據的添加、修改、刪除和瀏覽;
(3)能夠對學生選課進行管理,包括添加學生選課信息、錄入成績;
(4)能夠使用查詢功能,快速查看到指定學生或指定課程的基本信息以及所指定學生的選課信息;
(5)要注意添加學生基本信息、課程信息相關數據時,學號和課程號不能重復;還有在添加學生選課信息時,要求該學生和課程必須是存在的,而且不能添加重復的選課信息。
(6)提供友好的交互界面,可以方便用戶進行功能選擇,實現信息的管理和查詢,并可清晰地顯示相關信息。
2.使用的開發平臺:IDEA.Java Swing
1.2 項目需求分析
管理員能夠使用該程序對學生基本信息、課程信息、成績信息進行管理,包括數據的增加、修改、刪除和查詢。
增加功能:增加學生基本信息、課程信息、成績信息。增加時學生學號和課程號不可重復,且在選課信息部分增加的學生和課程必須存在;
修改功能:修改學生和課程基本信息以及選課信息。注意點和增加部分一樣;
刪除功能:可以刪除不需要的信息。若被刪除的信息在其他表中被使用,則應先停止此信息的使用,再允許刪除。
查詢功能:快速查看到指定學生的基本和選課信息或指定課程的基本信息;考慮到便捷性,最好實現輸入不完全的信息即可查詢的功能。
提供友好的交互界面,可以方便用戶進行功能選擇,實現信息的管理和查詢,并可清晰地顯示相關信息。
2、 ?項目設計
2.1 ???項目目標
根據上面的需求分析,學生成績管理系統要達到的目標:
(1)教學管理人員能夠使用StudentInfo程序對學生基本信息、課程信息進行管理,包括數據的添加、修改、刪除和瀏覽;
(2)能夠對學生選課進行管理,包括添加學生選課信息、錄入成績;
(3)能夠使用查詢功能,快速查看到指定學生或指定課程的基本信息以及所指定學生的選課信息;
(4)要注意添加學生基本信息、課程信息相關數據時,學號和課程號不能重復;還有在添加學生選課信息時,要求該學生和課程必須是存在的,而且不能添加重復的選課信息。
(5)提供友好的交互界面,可以方便用戶進行功能選擇,實現信息的管理和查詢,并可清晰地顯示相關信息。
2.2 ???構建開發環境
【1】系統開發平臺:Eclipse,Java,Mysql
【2】系統開發語言:Java、數據庫語言
【3】運行平臺:IDEA.JAVA.Swing
2.3 ???系統功能結構
學生信息管理系統在數據庫建立三個表,分別為:教師表、學生表、課程表,對于每個表格,都可以對上面的信息進行增刪改查等操作。
3、功能分析
學生管理系統所需要達成如下功能:?
?
?
?
?
?
4、詳細設計
4.1 Package dao
【1】 ?模塊概述
CourseDao操作實體類、ScoreDao操作實體類、StudentDao操作實體類、UserDao操作實體類。
【2】 ?代碼實現
CourseDao操作實體類:
package com.score.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.score.model.Course;
import com.score.utils.DBUtils;
/**
?* CourseDao操作實體類
?*/
public class CourseDao {
???/**
????* 添加Course
????*/
???public void insert(Course course){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "insert into course(cname,tname) values(?,?)";
?????????psmt = conn.prepareStatement(sql); ??????
?????????psmt.setString(1, course.getCname());
?????????psmt.setString(2, course.getTname());
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????*修改Course
????*/
???public void update(Course course){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "update course set cname=?,tname=? where id = ?";
?????????psmt = conn.prepareStatement(sql);
?????????psmt.setString(1, course.getCname());
?????????psmt.setString(2, course.getTname());
?????????psmt.setInt(3, course.getId());
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????* 刪除Course
????*/
???public void delete(int id){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "delete from ?course where id = ?";
?????????psmt = conn.prepareStatement(sql);
?????????psmt.setInt(1, id);
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????* 查詢Course
????*/
???public List<Course> find(Course course){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????ResultSet rs = null;
??????List<Course> list = new ArrayList<Course>();
??????try {
?????????conn = DBUtils.getConnection();
?????????StringBuffer buffer = new StringBuffer("select * from course where 1=1");
?????????if(StringUtils.isNotEmpty(course.getCname())){
????????????buffer.append(" and cname like '%"+course.getCname()+"%'");
?????????}
?????????if(StringUtils.isNotEmpty(course.getTname())){
????????????buffer.append(" and tname like '%"+course.getTname()+"%'");
?????????}
?????????if(0 != course.getId()){
????????????buffer.append(" and id = "+course.getId()+"");
?????????}
?????????psmt = conn.prepareStatement(buffer.toString());
?????????rs = psmt.executeQuery();
?????????Course courseParam = null;
?????????while(rs.next()){
????????????courseParam = new Course(); ??????
????????????courseParam.setId(rs.getInt("id"));
????????????courseParam.setCname(rs.getString("cname"));
????????????courseParam.setTname(rs.getString("tname"));
????????????list.add(courseParam);
?????????}
?????????DBUtils.close(conn, psmt, rs);
??????} catch (Exception e) {
?????????e.printStackTrace();
??????}
??????return list;
???}
???
}
ScoreDao操作實體類:
package com.score.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.score.model.Score;
import com.score.model.ScoreCustom;
import com.score.utils.DBUtils;
/**
?* ScoreDao操作實體類
?*/
public class ScoreDao {
???/**
????* 添加Score
????*/
???public void insert(Score score){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "insert into score(stu_id,cou_id,score) values(?,?,?)";
?????????psmt = conn.prepareStatement(sql); ??????
?????????psmt.setInt(1, score.getStuId());
?????????psmt.setInt(2, score.getCouId());
?????????psmt.setFloat(3, score.getScore());
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????*修改Score
????*/
???public void update(Score score){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "update score set stu_id=?,cou_id=?,score=? where id = ?";
?????????psmt = conn.prepareStatement(sql);
?????????psmt.setInt(1, score.getStuId());
?????????psmt.setInt(2, score.getCouId());
?????????psmt.setFloat(3, score.getScore());
?????????psmt.setInt(4, score.getId());
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????* 刪除Score
????*/
???public void delete(int id){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "delete from ?score where id = ?";
?????????psmt = conn.prepareStatement(sql);
?????????psmt.setInt(1, id);
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????* 查詢成績關聯查詢學生和課程
????*/
???public List<ScoreCustom> find(Score score){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????ResultSet rs = null;
??????List<ScoreCustom> list = new ArrayList<ScoreCustom>();
??????try {
?????????conn = DBUtils.getConnection();
?????????StringBuffer buffer = new StringBuffer("select t1.`name`,t1.classname,t2.cname,t3.* from student t1,course t2,score t3 ");
?????????buffer.append(" where t1.id = t3.stu_id and t2.id = t3.cou_id");
?????????if(0 != score.getStuId()){
????????????buffer.append(" and t1.id = "+score.getStuId()+"");
?????????}
?????????if(0 != score.getCouId()){
????????????buffer.append(" and t2.id = "+score.getCouId()+"");
?????????}
?????????psmt = conn.prepareStatement(buffer.toString());
?????????rs = psmt.executeQuery();
?????????ScoreCustom scoreParam = null;
?????????while(rs.next()){
????????????scoreParam = new ScoreCustom(); ??????
????????????scoreParam.setId(rs.getInt("id"));
????????????scoreParam.setStuId(rs.getInt("stu_id"));
????????????scoreParam.setCouId(rs.getInt("cou_id"));
????????????scoreParam.setScore(rs.getFloat("score"));
????????????scoreParam.setName(rs.getString("name"));
????????????scoreParam.setClassname(rs.getString("classname"));
????????????scoreParam.setCname(rs.getString("cname"));
????????????list.add(scoreParam);
?????????}
?????????DBUtils.close(conn, psmt, rs);
??????} catch (Exception e) {
?????????e.printStackTrace();
??????}
??????return list;
???}
???
???/**
????* 根據課程id和學號查詢數量
????* @param score
????* @return
????*/
???public int countByStuIdAndCouId(Score score){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????ResultSet rs = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????StringBuffer buffer = new StringBuffer("select count(*) size from score where stu_id=? and cou_id=?");
?????????psmt = conn.prepareStatement(buffer.toString());
?????????psmt.setInt(1, score.getStuId());
?????????psmt.setInt(2, score.getCouId());
?????????rs = psmt.executeQuery();
?????????if(rs.next()){
????????????return rs.getInt("size");
?????????}
??????}catch (Exception e) {
?????????e.printStackTrace();
??????}
??????return 0;
???}
???
}
StudentDao操作實體類:
package com.score.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.score.model.Student;
import com.score.utils.DBUtils;
/**
?* StudentDao操作實體類
?*/
public class StudentDao {
???/**
????* 添加Student
????*/
???public void insert(Student student){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "insert into student(classname,name) values(?,?)";
?????????psmt = conn.prepareStatement(sql); ??????
?????????psmt.setString(1, student.getClassname());
?????????psmt.setString(2, student.getName());
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????*修改Student
????*/
???public void update(Student student){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "update student set classname=?,name=? where id = ?";
?????????psmt = conn.prepareStatement(sql);
?????????psmt.setString(1, student.getClassname());
?????????psmt.setString(2, student.getName());
?????????psmt.setInt(3, student.getId());
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????* 刪除Student
????*/
???public void delete(int id){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????String sql = "delete from ?student where id = ?";
?????????psmt = conn.prepareStatement(sql);
?????????psmt.setInt(1, id);
?????????psmt.executeUpdate();
?????????DBUtils.close(conn, psmt);
??????} catch (SQLException e) {
?????????e.printStackTrace();
??????}
???}
???
???/**
????* 查詢Student
????*/
???public List<Student> find(Student student){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????ResultSet rs = null;
??????List<Student> list = new ArrayList<Student>();
??????try {
?????????conn = DBUtils.getConnection();
?????????StringBuffer buffer = new StringBuffer("select * from student where 1=1");
?????????if(StringUtils.isNotEmpty(student.getClassname())){
????????????buffer.append(" and classname like '%"+student.getClassname()+"%'");
?????????}
?????????if(StringUtils.isNotEmpty(student.getName())){
????????????buffer.append(" and name like '%"+student.getName()+"%'");
?????????}
?????????if(0 != student.getId()){
????????????buffer.append(" and id = "+student.getId()+"");
?????????}
?????????psmt = conn.prepareStatement(buffer.toString());
?????????rs = psmt.executeQuery();
?????????Student studentParam = null;
?????????while(rs.next()){
????????????studentParam = new Student(); ????
????????????studentParam.setId(rs.getInt("id"));
????????????studentParam.setClassname(rs.getString("classname"));
????????????studentParam.setName(rs.getString("name"));
????????????list.add(studentParam);
?????????}
?????????DBUtils.close(conn, psmt, rs);
??????} catch (Exception e) {
?????????e.printStackTrace();
??????}
??????return list;
???}
???
}
UserDao操作實體類:
package com.score.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.score.model.User;
import com.score.utils.DBUtils;
/**
?* UserDao操作實體類
?*/
public class UserDao {
???/**
????* 根據賬號密碼查詢
????* @param user
????* @return
????*/
???public User findByUnameAndPwd(User user){
??????Connection conn = null;
??????PreparedStatement psmt = null;
??????ResultSet rs = null;
??????User userParam = null;
??????try {
?????????conn = DBUtils.getConnection();
?????????StringBuffer buffer = new StringBuffer("select * from `user` where uname=123 and pwd=?");
?????????psmt = conn.prepareStatement(buffer.toString());
?????????psmt.setString(1, user.getUname());
?????????psmt.setString(2, user.getPwd());
?????????rs = psmt.executeQuery();
?????????if(rs.next()){
????????????userParam = new User(); ??????
????????????userParam.setId(rs.getInt("id"));
????????????userParam.setUname(rs.getString("uname"));
????????????userParam.setPwd(rs.getString("pwd"));
?????????}
?????????DBUtils.close(conn, psmt, rs);
??????} catch (Exception e) {
?????????e.printStackTrace();
??????}
??????return userParam;
???}
???
}
4.2 Package model
【1】 ?模塊概述
Course實體類、Score實體類、Student實體類、User實體類。
【2】 ?代碼實現
以Course實體類為例,進行創建:
package com.score.model;
/**
?* Course實體類
?*/
public class Course {
???private int id;// 課程號
???private String cname;// 課程名稱
???private String tname;// 教師
???
???
???public Course(String cname, String tname) {
??????super();
??????this.cname = cname;
??????this.tname = tname;
???}
???public Course(int id, String cname, String tname) {
??????super();
??????this.id = id;
??????this.cname = cname;
??????this.tname = tname;
???}
???public Course() {
??????super();
???}
???
???public ?int getId() {
??????return id;
???}
???public void setId(int id) {
??????this.id = id;
???}
???public ?String getCname() {
??????return cname;
???}
???public void setCname(String cname) {
??????this.cname = cname;
???}
???public ?String getTname() {
??????return tname;
???}
???public void setTname(String tname) {
??????this.tname = tname;
???}
}
4.3 Package utils
【1】 ?模塊概述
CommontUtils和DBUtils。
【2】 ?代碼實現
CommonUtils:
??package com.score.utils;
import java.util.Iterator;
import java.util.List;
import com.score.dao.CourseDao;
import com.score.model.Course;
public class CommonUtils {
???private static CourseDao courseDao = new CourseDao();
???
???//獲取所有的課程名稱
???public static String[] getAllCourseName(){
??????List<Course> list = courseDao.find(new Course());
??????String[] arr = new String[list.size()];
??????for (int i = 0; i < arr.length; i++) {
?????????arr[i] = list.get(i).getCname();
??????}
??????return arr;
???}
???
???//獲取課程id
???public static int getCourseIdByCourseName(String courseName){
??????List<Course> list = courseDao.find(new Course());
??????for (Course course : list) {
?????????if(course.getCname().equals(courseName)){
????????????return course.getId();
?????????}
??????}
??????return 0;
???}
}
DBUtils:
package com.score.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
???
???private static String url="jdbc:mysql://localhost:3306/db_score";
???private static String username="root";
???private static String password="root";
???private static String driver="com.mysql.jdbc.Driver";
???static{
??????try {
?????????Class.forName(driver);
??????} catch (ClassNotFoundException e) {
?????????e.printStackTrace();
??????}
???}
???public static Connection getConnection() throws SQLException{
??????return DriverManager.getConnection(url, username, password);
???}
???
???public static void close(Connection conn,PreparedStatement psmt, ResultSet rs){
??????if(rs != null){try{rs.close();}catch(SQLException e){e.printStackTrace();}}
??????if(psmt != null){try{psmt.close();}catch(SQLException e){e.printStackTrace();}}
??????if(conn != null){try{conn.close();}catch(SQLException e){e.printStackTrace();}}
???}
???public static void close(Connection conn,PreparedStatement psmt){
??????close(conn,psmt,null);
???}
// public static void main(String[] args) {
// ???try {
// ??????System.out.println("數據庫連接成功");
// ??????DBUtils.getConnection();
// ???} catch (SQLException e) {
// ??????e.printStackTrace();
// ???}
// }
}
4.4 Package views
【1】 ?模塊概述
AddScoreFrm、CourseManageFrm、LoginFrm、MenuFrm、ScoreManageFrm和StudentManageFrm。
【2】 ?代碼實現
以AddScoreFrm為例:
package com.score.views;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.score.dao.ScoreDao;
import com.score.model.Score;
import com.score.utils.CommonUtils;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddScoreFrm extends JFrame {
???private JPanel contentPane;
???private JTextField stu_idTxt;
???private JTextField scoreTxt;
???private JComboBox comboBox;
???
???private ScoreDao scoreDao = new ScoreDao();
???/*public static void main(String[] args) {
??????AddScoreFrm frame = new AddScoreFrm();
??????frame.setVisible(true);
???}*/
???public AddScoreFrm() {
??????setTitle("學生成績管理系統-添加成績");
??????setBounds(100, 100, 450, 300);
??????setResizable(false);
??????setLocationRelativeTo(null);
??????setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
??????contentPane = new JPanel();
??????contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
??????setContentPane(contentPane);
??????contentPane.setLayout(null);
??????
??????JLabel label = new JLabel("學號");
??????label.setBounds(105, 32, 54, 15);
??????contentPane.add(label);
??????
??????stu_idTxt = new JTextField();
??????stu_idTxt.setBounds(137, 29, 162, 21);
??????contentPane.add(stu_idTxt);
??????stu_idTxt.setColumns(10);
??????
??????JLabel label_1 = new JLabel("課程");
??????label_1.setBounds(105, 75, 29, 15);
??????contentPane.add(label_1);
??????
??????comboBox = new JComboBox();
??????comboBox.setModel(new DefaultComboBoxModel(CommonUtils.getAllCourseName()));//從數據庫中查詢所有課程名字,填充到下拉條
??????comboBox.setBounds(137, 72, 162, 21);
??????contentPane.add(comboBox);
??????
??????JLabel label_2 = new JLabel("分數");
??????label_2.setBounds(105, 120, 54, 15);
??????contentPane.add(label_2);
??????
??????scoreTxt = new JTextField();
??????scoreTxt.setBounds(137, 117, 162, 21);
??????contentPane.add(scoreTxt);
??????scoreTxt.setColumns(10);
??????
??????JButton button = new JButton("添加成績");
??????button.addActionListener(new ActionListener() {
?????????public void actionPerformed(ActionEvent e) {
????????????int id = Integer.parseInt(stu_idTxt.getText());
????????????int courseId = CommonUtils.getCourseIdByCourseName(comboBox.getSelectedItem().toString());
????????????float score = Float.parseFloat(scoreTxt.getText());
????????????if(0 != scoreDao.countByStuIdAndCouId(new Score(id, courseId))){
???????????????JOptionPane.showMessageDialog(null, "失敗,這個學生和課程已經添加過成績了!");
???????????????return;
????????????}
????????????scoreDao.insert(new Score(id, courseId, score));
????????????JOptionPane.showMessageDialog(null, "添加成功");
????????????stu_idTxt.setText("");
????????????scoreTxt.setText("");
?????????}
??????});
??????button.setBounds(137, 169, 93, 23);
??????contentPane.add(button);
???}
}
5、項目小結
對于學生信息管理系統項目,由于時間較短,不熟悉數據庫和idea的建立過程,只完成了部分功能。該數據庫只有一個學生基本信息表,實現了添加、刪除、修改和查詢功能,并實現了登錄界面的設計。缺點是對學生基本信息表的數據格式沒有嚴格要求。
通過研究和實踐,我基本上可以了解創建一個用戶友好的項目管理系統和應用界面。如果我有足夠的時間,我會設計登錄界面,指定三個登錄人員:管理員、教師和學生,然后管理員可以對教師和學生進行授權和授權,學生可以添加、刪除、修改和檢查他們的選課信息。他們只能查看課程分數和其他人的信息。教師可以添加、刪除、修改和檢查學生的成績和基本信息等。
總結
以上是生活随笔為你收集整理的项目四:学生信息管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于菜鸟的圆通电子面单打印
- 下一篇: PCD 文件格式