javaee课设学生管理系统
时间: 2023-12-17 21:28:09 浏览: 100
java课设-学生管理系统
以下是一个简单的JavaEE课设学生管理系统的实现,包括登录验证和学生信息的增删改查功能。该系统使用了Struts框架和MySQL数据库。
1. 登录验证功能:
```java
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String execute() throws Exception {
if (username.equals("admin") && password.equals("123456")) {
return SUCCESS;
} else {
addActionError("用户名或密码错误!");
return ERROR;
}
}
// getters and setters
}
```
2. 学生信息的增删改查功能:
```java
public class StudentAction extends ActionSupport {
private List<Student> students;
private Student student;
private int id;
public String list() throws Exception {
students = StudentDAO.getAllStudents();
return SUCCESS;
}
public String add() throws Exception {
StudentDAO.addStudent(student);
return SUCCESS;
}
public String edit() throws Exception {
student = StudentDAO.getStudentById(id);
return SUCCESS;
}
public String update() throws Exception {
StudentDAO.updateStudent(student);
return SUCCESS;
}
public String delete() throws Exception {
StudentDAO.deleteStudent(id);
return SUCCESS;
}
// getters and setters
}
```
3. 数据库连接和操作:
```java
public class StudentDAO {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/student";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() throws Exception {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return conn;
}
public static List<Student> getAllStudents() throws Exception {
List<Student> students = new ArrayList<Student>();
Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
students.add(student);
}
rs.close();
stmt.close();
conn.close();
return students;
}
public static void addStudent(Student student) throws Exception {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO student(name, age, gender) VALUES (?, ?, ?)");
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
public static Student getStudentById(int id) throws Exception {
Student student = null;
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM student WHERE id = ?");
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.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
}
rs.close();
pstmt.close();
conn.close();
return student;
}
public static void updateStudent(Student student) throws Exception {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("UPDATE student SET name = ?, age = ?, gender = ? WHERE id = ?");
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getId());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
public static void deleteStudent(int id) throws Exception {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM student WHERE id = ?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
```
阅读全文