用idea写学生信息管理系统
时间: 2023-08-26 15:16:08 浏览: 139
学生信息管理系统_javaWeb.zip
5星 · 资源好评率100%
首先,需要在MySQL中创建相应的数据库和表。在MySQL中执行以下SQL语句:
```
CREATE DATABASE student_info;
USE student_info;
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT NOT NULL,
class VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL
);
CREATE TABLE score (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT NOT NULL,
subject VARCHAR(50) NOT NULL,
score INT NOT NULL,
exam_time DATE NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(id)
);
```
然后,可以使用Java语言编写前端界面和后台逻辑。下面是一个简单的示例:
1. 创建Maven项目,并在pom.xml中添加以下依赖:
```
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
```
2. 创建GUI界面,使用Swing实现。
3. 创建数据库连接类,实现对MySQL数据库的连接和操作。
```
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/student_info?serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
private static Connection conn = null;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
4. 创建学生信息操作类,实现对学生信息的增删改查。
```
import org.apache.commons.lang3.StringUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
public boolean add(Student student) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO student (name, gender, age, class, phone) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, student.getName());
stmt.setString(2, student.getGender());
stmt.setInt(3, student.getAge());
stmt.setString(4, student.getClazz());
stmt.setString(5, student.getPhone());
int count = stmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
return false;
}
public boolean update(Student student) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE student SET name = ?, gender = ?, age = ?, class = ?, phone = ? WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, student.getName());
stmt.setString(2, student.getGender());
stmt.setInt(3, student.getAge());
stmt.setString(4, student.getClazz());
stmt.setString(5, student.getPhone());
stmt.setInt(6, student.getId());
int count = stmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
return false;
}
public boolean delete(int id) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM student WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
int count = stmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
return false;
}
public Student getById(int id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM student WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setClazz(rs.getString("class"));
student.setPhone(rs.getString("phone"));
return student;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return null;
}
public List<Student> getAll() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Student> students = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM student";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setClazz(rs.getString("class"));
student.setPhone(rs.getString("phone"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return students;
}
public List<Student> search(String keyword) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Student> students = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM student WHERE id LIKE ? OR name LIKE ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, "%" + keyword + "%");
stmt.setString(2, "%" + keyword + "%");
rs = stmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setClazz(rs.getString("class"));
student.setPhone(rs.getString("phone"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return students;
}
}
```
5. 创建考试成绩操作类,实现对考试成绩的增删改查。
```
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ScoreDao {
public boolean add(Score score) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO score (student_id, subject, score, exam_time) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, score.getStudentId());
stmt.setString(2, score.getSubject());
stmt.setInt(3, score.getScore());
stmt.setDate(4, new Date(score.getExamTime().getTime()));
int count = stmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
return false;
}
public boolean update(Score score) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE score SET student_id = ?, subject = ?, score = ?, exam_time = ? WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, score.getStudentId());
stmt.setString(2, score.getSubject());
stmt.setInt(3, score.getScore());
stmt.setDate(4, new Date(score.getExamTime().getTime()));
stmt.setInt(5, score.getId());
int count = stmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
return false;
}
public boolean delete(int id) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM score WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
int count = stmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, null);
}
return false;
}
public Score getById(int id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM score WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
Score score = new Score();
score.setId(rs.getInt("id"));
score.setStudentId(rs.getInt("student_id"));
score.setSubject(rs.getString("subject"));
score.setScore(rs.getInt("score"));
score.setExamTime(rs.getDate("exam_time"));
return score;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return null;
}
public List<Score> getByStudentId(int studentId) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Score> scores = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM score WHERE student_id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, studentId);
rs = stmt.executeQuery();
while (rs.next()) {
Score score = new Score();
score.setId(rs.getInt("id"));
score.setStudentId(rs.getInt("student_id"));
score.setSubject(rs.getString("subject"));
score.setScore(rs.getInt("score"));
score.setExamTime(rs.getDate("exam_time"));
scores.add(score);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return scores;
}
public List<Score> getAll() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Score> scores = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM score";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Score score = new Score();
score.setId(rs.getInt("id"));
score.setStudentId(rs.getInt("student_id"));
score.setSubject(rs.getString("subject"));
score.setScore(rs.getInt("score"));
score.setExamTime(rs.getDate("exam_time"));
scores.add(score);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return scores;
}
}
```
6. 创建主程序类,在GUI界面中实现对学生信息和考试成绩的增删改查操作。
```
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class MainFrame extends JFrame {
private JPanel contentPane;
private JTextField txtId;
private JTextField txtName;
private JTextField txtGender;
private JTextField txtAge;
private JTextField txtClass;
private JTextField txtPhone;
private JTextField txtScoreId;
private JTextField txtStudentId;
private JTextField txtSubject;
private JTextField txtScore;
private JTextField txtExamTime;
private JTable tableStudent;
private JTable tableScore;
private StudentDao studentDao = new StudentDao();
private ScoreDao scoreDao = new ScoreDao();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
setTitle("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// 学生信息管理面板
JPanel panelStudent = new JPanel();
panelStudent.setBounds(10, 10, 764, 275);
contentPane.add(panelStudent);
panelStudent.setLayout(null);
JLabel lblId = new JLabel("学号:");
lblId.setBounds(10, 10, 60, 25);
panelStudent.add(lblId);
txtId = new JTextField();
txtId.setBounds(80, 10, 120, 25);
panelStudent.add(txtId);
txtId.setColumns(10);
JLabel lblName = new JLabel("姓名:");
lblName.setBounds(10, 45, 60, 25);
panelStudent.add(lblName);
txtName = new JTextField();
txtName.setBounds(80, 45, 120, 25);
panelStudent.add(txtName);
txtName.setColumns(10);
JLabel lblGender = new JLabel("性别:");
lblGender.setBounds(10, 80, 60, 25);
panelStudent.add(lblGender);
txtGender = new JTextField();
txtGender.setBounds(80, 80, 120, 25);
panelStudent.add(txtGender);
txtGender.setColumns(10);
JLabel lblAge = new JLabel("年龄:");
lblAge.setBounds(10, 115, 60, 25);
panelStudent.add(lblAge);
txtAge = new JTextField();
txtAge.setBounds(80, 115, 120, 25);
panelStudent.add(txtAge);
txtAge.setColumns(10);
JLabel lblClass = new JLabel("班级:");
lblClass.setBounds(10, 150, 60, 25);
panelStudent.add(lblClass);
txtClass = new JTextField();
txtClass.setBounds(80, 150, 120, 25);
panelStudent.add(txtClass);
txtClass.setColumns(10);
JLabel lblPhone = new JLabel("联系方式:");
lblPhone.setBounds(10, 185, 60, 25);
panelStudent.add(lblPhone);
txtPhone = new JTextField();
txtPhone.setBounds(80, 185, 120, 25);
panelStudent.add(txtPhone);
txtPhone.setColumns(10);
JButton btnStudentAdd = new JButton("添加");
btnStudentAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
btnStudentAdd.setBounds(10, 220, 80, 25);
panelStudent.add(btnStudentAdd);
JButton btnStudentUpdate = new JButton("修改");
btnStudentUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateStudent();
}
});
btnStudentUpdate.setBounds(100,
阅读全文