webServletjavabean采用三层架构的开发模式,实现学生信息的增删除改查等功能。具体要求如下: 1.选择某一数据库管理系统,创建一个学生信息表user,保存学生的相关信息,往数据表中添加几条记录。 2.查询并显示数据表中所有学生的信息,运行界面如图1所示。 图1 3.点击图1中的新增按钮,显示如图2所示的界面。 图2 点击新增按钮,将表单中的数据添加到数据表示中。 4.点击图1中每个学生信息的学号字段,显示如图3所示的界面。 图3 单击图3的中修改按钮实现记录的修改。 5.单击图1中每个学生信息后面的删除按钮,可将该学生信息进行删除。代码
时间: 2024-03-08 16:50:03 浏览: 65
数学建模学习资料 姜启源数学模型课件 M04 数学规划模型 共85页.pptx
由于这是一个比较复杂的开发任务,代码实现可能会比较长,这里只给出一个简单的示例代码,供您参考。
User.java (JavaBean 类)
```java
public class User {
private String id;
private String name;
private String gender;
private int age;
private String className;
// getter 和 setter 方法
}
```
UserDao.java (数据访问层)
```java
public class UserDao {
// 数据库连接信息
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/test";
private String username = "root";
private String password = "123456";
// 获取数据库连接
private Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 查询所有学生信息
public List<User> findAll() {
List<User> userList = new ArrayList<User>();
Connection conn = getConnection();
String sql = "SELECT id, name, gender, age, class_name FROM user";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setGender(rs.getString("gender"));
user.setAge(rs.getInt("age"));
user.setClassName(rs.getString("class_name"));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
// 新增学生信息
public boolean add(User user) {
Connection conn = getConnection();
String sql = "INSERT INTO user(id, name, gender, age, class_name) VALUES(?, ?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getId());
pstmt.setString(2, user.getName());
pstmt.setString(3, user.getGender());
pstmt.setInt(4, user.getAge());
pstmt.setString(5, user.getClassName());
int result = pstmt.executeUpdate();
if (result > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 根据学号查询学生信息
public User findById(String id) {
Connection conn = getConnection();
String sql = "SELECT id, name, gender, age, class_name FROM user WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setGender(rs.getString("gender"));
user.setAge(rs.getInt("age"));
user.setClassName(rs.getString("class_name"));
return user;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// 修改学生信息
public boolean update(User user) {
Connection conn = getConnection();
String sql = "UPDATE user SET name = ?, gender = ?, age = ?, class_name = ? WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getGender());
pstmt.setInt(3, user.getAge());
pstmt.setString(4, user.getClassName());
pstmt.setString(5, user.getId());
int result = pstmt.executeUpdate();
if (result > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 删除学生信息
public boolean delete(String id) {
Connection conn = getConnection();
String sql = "DELETE FROM user WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
int result = pstmt.executeUpdate();
if (result > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
```
UserServlet.java (控制层)
```java
public class UserServlet extends HttpServlet {
private UserDao userDao = new UserDao();
// 查询所有学生信息
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> userList = userDao.findAll();
request.setAttribute("userList", userList);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}
// 新增学生信息
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
String className = request.getParameter("className");
User user = new User();
user.setId(id);
user.setName(name);
user.setGender(gender);
user.setAge(age);
user.setClassName(className);
if (userDao.add(user)) {
response.sendRedirect("user");
} else {
response.getWriter().write("新增失败!");
}
}
}
```
userList.jsp (前端页面)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title>
</head>
<body>
<h1>学生信息管理</h1>
<table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>班级</th>
<th>操作</th>
</tr>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.gender}</td>
<td>${user.age}</td>
<td>${user.className}</td>
<td>
<a href="user?id=${user.id}">修改</a>
<a href="deleteUser?id=${user.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="addUser.jsp">新增</a>
</body>
</html>
```
addUser.jsp (前端页面)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新增学生信息</title>
</head>
<body>
<h1>新增学生信息</h1>
<form action="user" method="post">
<label>学号:</label><input type="text" name="id"><br>
<label>姓名:</label><input type="text" name="name"><br>
<label>性别:</label><input type="text" name="gender"><br>
<label>年龄:</label><input type="text" name="age"><br>
<label>班级:</label><input type="text" name="className"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
以上代码仅供参考,具体实现还需要根据具体需求进行调整和优化。需要注意的是,为了实现三层架构,需要将前端、后端和数据库分别独立出来,实现彼此之间的解耦。同时,为了防止 SQL 注入攻击,需要使用 PreparedStatement 来执行 SQL 语句。
阅读全文