用Java学生信息管理系统
时间: 2024-01-03 10:05:10 浏览: 37
学生信息管理系统是一个典型的 CRUD 应用,即增加(Create)、查询(Retrieve)、修改(Update)和删除(Delete)。下面是一个使用Java语言实现学生信息管理系统的简单示例。
1. 数据库设计
首先需要设计数据库表结构,这里我们设计两张表:学生表和课程表。
学生表:
| 列名 | 数据类型 | 说明 |
| --------- | -------- | ---------- |
| id | int | 学生编号 |
| name | varchar | 学生姓名 |
| gender | varchar | 学生性别 |
| birthday | date | 学生生日 |
| address | varchar | 学生住址 |
| telephone | varchar | 学生电话号码 |
课程表:
| 列名 | 数据类型 | 说明 |
| -------- | -------- | -------- |
| id | int | 课程编号 |
| name | varchar | 课程名称 |
| credit | int | 课程学分 |
| teacher | varchar | 授课教师 |
| location | varchar | 授课地点 |
2. Java代码实现
接下来,我们使用Java语言编写学生信息管理系统的代码。我们使用MySQL作为数据库,使用JDBC连接数据库。
2.1. 建立数据库连接
在Java中,可以使用JDBC连接MySQL数据库。需要导入MySQL的JDBC驱动包,然后使用以下代码建立数据库连接:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/student";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
```
2.2. 学生信息管理
定义一个 Student 类,用于保存学生信息。然后定义一个 StudentDao 类,实现对学生信息的增删改查操作。
```
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
// 查询所有学生信息
public List<Student> findAll() {
List<Student> students = new ArrayList<>();
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM student";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setBirthday(rs.getDate("birthday"));
student.setAddress(rs.getString("address"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
// 根据学生编号查询学生信息
public Student findById(int id) {
Student student = null;
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM student WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setBirthday(rs.getDate("birthday"));
student.setAddress(rs.getString("address"));
student.setTelephone(rs.getString("telephone"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
// 添加学生信息
public void add(Student student) {
Connection conn = DatabaseConnection.getConnection();
String sql = "INSERT INTO student(name, gender, birthday, address, telephone) VALUES (?, ?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setDate(3, student.getBirthday());
pstmt.setString(4, student.getAddress());
pstmt.setString(5, student.getTelephone());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 更新学生信息
public void update(Student student) {
Connection conn = DatabaseConnection.getConnection();
String sql = "UPDATE student SET name=?, gender=?, birthday=?, address=?, telephone=? WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setDate(3, student.getBirthday());
pstmt.setString(4, student.getAddress());
pstmt.setString(5, student.getTelephone());
pstmt.setInt(6, student.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除学生信息
public void delete(int id) {
Connection conn = DatabaseConnection.getConnection();
String sql = "DELETE FROM student WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
2.3. 课程信息管理
定义一个 Course 类,用于保存课程信息。然后定义一个 CourseDao 类,实现对课程信息的增删改查操作。
```
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CourseDao {
// 查询所有课程信息
public List<Course> findAll() {
List<Course> courses = new ArrayList<>();
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM course";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Course course = new Course();
course.setId(rs.getInt("id"));
course.setName(rs.getString("name"));
course.setCredit(rs.getInt("credit"));
course.setTeacher(rs.getString("teacher"));
course.setLocation(rs.getString("location"));
courses.add(course);
}
} catch (SQLException e) {
e.printStackTrace();
}
return courses;
}
// 根据课程编号查询课程信息
public Course findById(int id) {
Course course = null;
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM course WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
course = new Course();
course.setId(rs.getInt("id"));
course.setName(rs.getString("name"));
course.setCredit(rs.getInt("credit"));
course.setTeacher(rs.getString("teacher"));
course.setLocation(rs.getString("location"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return course;
}
// 添加课程信息
public void add(Course course) {
Connection conn = DatabaseConnection.getConnection();
String sql = "INSERT INTO course(name, credit, teacher, location) VALUES (?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getName());
pstmt.setInt(2, course.getCredit());
pstmt.setString(3, course.getTeacher());
pstmt.setString(4, course.getLocation());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 更新课程信息
public void update(Course course) {
Connection conn = DatabaseConnection.getConnection();
String sql = "UPDATE course SET name=?, credit=?, teacher=?, location=? WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getName());
pstmt.setInt(2, course.getCredit());
pstmt.setString(3, course.getTeacher());
pstmt.setString(4, course.getLocation());
pstmt.setInt(5, course.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除课程信息
public void delete(int id) {
Connection conn = DatabaseConnection.getConnection();
String sql = "DELETE FROM course WHERE id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. 测试
在测试代码中,我们可以使用 StudentDao 和 CourseDao 类的方法操作数据库。
```
import java.util.List;
public class Test {
public static void main(String[] args) {
// 查询所有学生信息
StudentDao studentDao = new StudentDao();
List<Student> students = studentDao.findAll();
for (Student student : students) {
System.out.println(student);
}
// 查询所有课程信息
CourseDao courseDao = new CourseDao();
List<Course> courses = courseDao.findAll();
for (Course course : courses) {
System.out.println(course);
}
// 添加学生信息
Student student = new Student();
student.setName("张三");
student.setGender("男");
student.setBirthday(Date.valueOf("2000-01-01"));
student.setAddress("北京市");
student.setTelephone("123456789");
studentDao.add(student);
// 更新学生信息
student = studentDao.findById(1);
student.setName("李四");
studentDao.update(student);
// 删除学生信息
studentDao.delete(1);
}
}
```
以上就是一个简单的使用Java语言实现学生信息管理系统的示例。在实际开发中,可以根据需求进行扩展和优化。