基于java和mysql做一个学生选课系统,要实现增删改查,并且要有学生登录功能,给出代码
时间: 2024-03-05 18:51:19 浏览: 21
由于这是一个比较复杂的系统,无法在这里完整地给出所有代码。以下是我给您的建议:
1. 数据库设计:首先需要设计数据库表,包括学生表、课程表和选课表。学生表和课程表分别记录学生和课程的基本信息,选课表则记录学生选课的情况,其中包括学生ID和课程ID。
2. Java后端实现:可以使用Spring Boot框架实现后端服务,通过JDBC连接MySQL数据库,实现增删改查功能。需要实现的接口包括学生登录、学生信息管理、课程信息管理和选课管理等。
以下是一个简单的示例代码:
```java
// 数据库连接配置
String url = "jdbc:mysql://localhost:3306/mydatabase?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
String username = "root";
String password = "123456";
// 学生登录
@PostMapping("/login")
public String login(@RequestParam String username, @RequestParam String password) {
// 查询数据库检查用户名和密码是否正确
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM student WHERE username = ? AND password = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
if (rs.next()) {
// 登录成功
return "success";
} else {
// 登录失败
return "fail";
}
} catch (SQLException e) {
e.printStackTrace();
return "error";
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 学生信息管理
@GetMapping("/students")
public List<Student> getStudents() {
// 查询数据库获取所有学生信息
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
String sql = "SELECT * FROM student";
rs = stmt.executeQuery(sql);
List<Student> students = new ArrayList<>();
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"));
students.add(student);
}
return students;
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 课程信息管理
@GetMapping("/courses")
public List<Course> getCourses() {
// 查询数据库获取所有课程信息
// ...
}
// 选课管理
@PostMapping("/select_course")
public String selectCourse(@RequestParam int studentId, @RequestParam int courseId) {
// 将选课信息插入数据库选课表中
// ...
}
```
3. 前端页面实现:可以使用HTML、CSS和JavaScript等技术实现前端页面,通过Ajax调用后端接口实现数据的展示和操作。需要实现的页面包括学生登录页面、学生信息管理页面、课程信息管理页面和选课管理页面等。
以下是一个简单的示例代码:
```html
<!-- 学生登录页面 -->
<form action="/login" method="post">
<label for="username">用户名:</label>
<input type="text" name="username" id="username">
<label for="password">密码:</label>
<input type="password" name="password" id="password">
<button type="submit">登录</button>
</form>
<!-- 学生信息管理页面 -->
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr v-for="student in students">
<td>{{ student.id }}</td>
<td>{{ student.name }}</td>
<td>{{ student.gender }}</td>
<td>{{ student.age }}</td>
</tr>
</tbody>
</table>
<script>
// 使用Vue.js实现数据绑定和Ajax请求
new Vue({
el: 'table',
data: {
students: []
},
mounted() {
axios.get('/students').then((response) => {
this.students = response.data;
});
}
});
</script>
<!-- 课程信息管理页面 -->
<!-- ... -->
<!-- 选课管理页面 -->
<form action="/select_course" method="post">
<label for="student_id">学生ID:</label>
<input type="text" name="student_id" id="student_id">
<label for="course_id">课程ID:</label>
<input type="text" name="course_id" id="course_id">
<button type="submit">选课</button>
</form>
```
总之,以上是一个学生选课系统的基本实现思路和示例代码,具体实现细节还需要根据您的实际需求来进行调整。