能帮我写出一个简单的javaweb学生考勤管理系统的所需代码嘛
时间: 2023-06-19 22:09:54 浏览: 130
当然可以,以下是一个简单的JavaWeb学生考勤管理系统的代码示例:
1. 创建数据库表
考勤表:
```sql
CREATE TABLE attendance (
id INT(11) NOT NULL AUTO_INCREMENT,
student_id INT(11) NOT NULL,
attendance_date DATE NOT NULL,
status INT(1) NOT NULL,
PRIMARY KEY (id)
);
```
学生表:
```sql
CREATE TABLE student (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
address VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
```
2. 创建JavaBean类
Attendance.java:
```java
public class Attendance {
private int id;
private int studentId;
private Date attendanceDate;
private int status;
// getter and setter
}
```
Student.java:
```java
public class Student {
private int id;
private String name;
private String address;
// getter and setter
}
```
3. 创建Dao类
AttendanceDao.java:
```java
public class AttendanceDao {
private Connection conn;
public AttendanceDao(Connection conn) {
this.conn = conn;
}
public void addAttendance(Attendance attendance) throws SQLException {
String sql = "INSERT INTO attendance (student_id, attendance_date, status) VALUES (?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, attendance.getStudentId());
pstmt.setDate(2, new java.sql.Date(attendance.getAttendanceDate().getTime()));
pstmt.setInt(3, attendance.getStatus());
pstmt.executeUpdate();
}
}
public List<Attendance> getAttendanceList() throws SQLException {
List<Attendance> attendanceList = new ArrayList<>();
String sql = "SELECT * FROM attendance";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Attendance attendance = new Attendance();
attendance.setId(rs.getInt("id"));
attendance.setStudentId(rs.getInt("student_id"));
attendance.setAttendanceDate(rs.getDate("attendance_date"));
attendance.setStatus(rs.getInt("status"));
attendanceList.add(attendance);
}
}
}
return attendanceList;
}
}
```
StudentDao.java:
```java
public class StudentDao {
private Connection conn;
public StudentDao(Connection conn) {
this.conn = conn;
}
public void addStudent(Student student) throws SQLException {
String sql = "INSERT INTO student (name, address) VALUES (?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getAddress());
pstmt.executeUpdate();
}
}
public List<Student> getStudentList() throws SQLException {
List<Student> studentList = new ArrayList<>();
String sql = "SELECT * FROM student";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAddress(rs.getString("address"));
studentList.add(student);
}
}
}
return studentList;
}
}
```
4. 创建Servlet类
添加学生Servlet:
```java
@WebServlet("/add_student")
public class AddStudentServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String address = req.getParameter("address");
Student student = new Student();
student.setName(name);
student.setAddress(address);
try {
Connection conn = DBUtil.getConnection();
StudentDao studentDao = new StudentDao(conn);
studentDao.addStudent(student);
DBUtil.closeConnection(conn);
resp.sendRedirect("index.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
考勤记录Servlet:
```java
@WebServlet("/attendance_list")
public class AttendanceListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Connection conn = DBUtil.getConnection();
AttendanceDao attendanceDao = new AttendanceDao(conn);
List<Attendance> attendanceList = attendanceDao.getAttendanceList();
req.setAttribute("attendanceList", attendanceList);
DBUtil.closeConnection(conn);
req.getRequestDispatcher("attendance_list.jsp").forward(req, resp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5. 创建JSP页面
index.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生考勤管理系统</title>
</head>
<body>
<form method="post" action="add_student">
<input type="text" name="name" placeholder="请输入学生姓名">
<input type="text" name="address" placeholder="请输入学生地址">
<button type="submit">添加</button>
</form>
<br>
<a href="attendance_list">查看考勤记录</a>
</body>
</html>
```
attendance_list.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>考勤记录</title>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>学生ID</th>
<th>考勤时间</th>
<th>状态</th>
</tr>
<c:forEach var="attendance" items="${attendanceList}">
<tr>
<td>${attendance.id}</td>
<td>${attendance.studentId}</td>
<td>${attendance.attendanceDate}</td>
<td>${attendance.status}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
以上是一个简单的JavaWeb学生考勤管理系统的示例代码,仅供参考。完整的代码需要根据实际情况进行修改和完善。
阅读全文