JavaWeb实现学生信息管理系统需要以下步骤:
1. 环境搭建
首先需要搭建好JavaWeb开发环境,包括JDK和IDE等工具,以及Tomcat服务器。可以参考相关教程进行安装配置。
2. 创建项目
在IDE中创建一个新的JavaWeb项目,并创建所需的数据库表。可以使用MySQL等关系型数据库进行创建,具体表结构可以根据实际需求进行设计。
3. 编写JavaBean
创建一个JavaBean类作为模型,用于存储学生信息,包括学号、姓名、性别、年龄等属性,以及对应的getter和setter方法。
示例:
public class Student {
private String id;
private String name;
private String sex;
private int age;
//getter和setter方法
}
4. 编写DAO层
在DAO层中,通过JDBC连接数据库,实现对学生信息的增删改查操作。可以使用PreparedStatement等工具类进行操作。同时,建议使用连接池进行连接管理。
示例:
public class StudentDAO {
//连接池
private DataSource ds;
//查询所有学生信息
public List<Student> findAll() throws SQLException {
List<Student> students = new ArrayList<>();
Connection conn = ds.getConnection();
String sql = "SELECT * FROM student";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
students.add(student);
}
rs.close();
pstmt.close();
conn.close();
return students;
}
//根据学号查询学生信息
public Student findById(String id) throws SQLException {
Student student = null;
Connection conn = ds.getConnection();
String sql = "SELECT * FROM student WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
}
rs.close();
pstmt.close();
conn.close();
return student;
}
//添加学生信息
public void add(Student student) throws SQLException {
Connection conn = ds.getConnection();
String sql = "INSERT INTO student VALUES (?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getSex());
pstmt.setInt(4, student.getAge());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
//根据学号删除学生信息
public void delete(String id) throws SQLException {
Connection conn = ds.getConnection();
String sql = "DELETE FROM student WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
//更新学生信息
public void update(Student student) throws SQLException {
Connection conn = ds.getConnection();
String sql = "UPDATE student SET name=?,sex=?,age=? WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getId());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
5. 编写Servlet
在Servlet中,实现与客户端交互,接收用户请求并调用DAO层进行数据操作,最终将结果返回给客户端。建议使用JSP进行视图展示。
示例:
public class StudentServlet extends HttpServlet {
private StudentDAO dao;
public void init() throws ServletException {
dao = new StudentDAO();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if ("list".equals(action)) {
try {
List<Student> students = dao.findAll();
request.setAttribute("students", students);
request.getRequestDispatcher("/student_list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("edit".equals(action)) {
String id = request.getParameter("id");
try {
Student student = dao.findById(id);
request.setAttribute("student", student);
request.getRequestDispatcher("/student_edit.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("delete".equals(action)) {
String id = request.getParameter("id");
try {
dao.delete(id);
response.sendRedirect(request.getContextPath() + "/StudentServlet?action=list");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
Student student = new Student();
student.setId(request.getParameter("id"));
student.setName(request.getParameter("name"));
student.setSex(request.getParameter("sex"));
student.setAge(Integer.parseInt(request.getParameter("age")));
try {
dao.add(student);
response.sendRedirect(request.getContextPath() + "/StudentServlet?action=list");
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("update".equals(action)) {
Student student = new Student();
student.setId(request.getParameter("id"));
student.setName(request.getParameter("name"));
student.setSex(request.getParameter("sex"));
student.setAge(Integer.parseInt(request.getParameter("age")));
try {
dao.update(student);
response.sendRedirect(request.getContextPath() + "/StudentServlet?action=list");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
以上就是JavaWeb实现学生信息管理系统的完整攻略,其中涉及到了JavaBean、DAO层、Servlet等技术,同时需要注意相关的数据库配置和视图展示。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JavaWeb实现学生信息管理系统(1) - Python技术站