1. (简答题) 使用数据库技术和DAO模式设计,完成以下两个页面, 1. 要求注册页 数据 插入到表中。设计一个表User,包含三个字段(性别,年龄,ID) 2. 显示页,显示从数据库中查询到的数据
时间: 2023-09-16 17:11:53 浏览: 83
设计模式以及用户登陆注册页面
1. 创建表User:
```sql
CREATE TABLE User (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
gender VARCHAR(10),
age INT,
user_id VARCHAR(50)
);
```
2. DAO模式设计:
```java
public class UserDao {
// 数据库连接
private Connection connection;
public UserDao(Connection connection) {
this.connection = connection;
}
// 插入用户数据
public void insert(User user) throws SQLException {
String sql = "INSERT INTO User (gender, age, user_id) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, user.getGender());
statement.setInt(2, user.getAge());
statement.setString(3, user.getUserId());
statement.executeUpdate();
statement.close();
}
// 查询所有用户数据
public List<User> findAll() throws SQLException {
List<User> userList = new ArrayList<>();
String sql = "SELECT * FROM User";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setGender(resultSet.getString("gender"));
user.setAge(resultSet.getInt("age"));
user.setUserId(resultSet.getString("user_id"));
userList.add(user);
}
resultSet.close();
statement.close();
return userList;
}
}
```
3. 注册页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="register" method="post">
<div>
<label>性别:</label>
<input type="text" name="gender">
</div>
<div>
<label>年龄:</label>
<input type="text" name="age">
</div>
<div>
<label>ID:</label>
<input type="text" name="user_id">
</div>
<button type="submit">注册</button>
</form>
</body>
</html>
```
4. 注册页面处理:
```java
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserDao userDao;
public RegisterServlet() {
super();
}
@Override
public void init() throws ServletException {
super.init();
// 获取数据库连接
Connection connection = (Connection) getServletContext().getAttribute("connection");
userDao = new UserDao(connection);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
String userId = request.getParameter("user_id");
User user = new User(gender, age, userId);
try {
// 插入用户数据到数据库
userDao.insert(user);
response.sendRedirect("display");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5. 显示页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>性别</th>
<th>年龄</th>
<th>ID</th>
</tr>
<% for (User user : userList) { %>
<tr>
<td><%= user.getId() %></td>
<td><%= user.getGender() %></td>
<td><%= user.getAge() %></td>
<td><%= user.getUserId() %></td>
</tr>
<% } %>
</table>
</body>
</html>
```
6. 显示页面处理:
```java
public class DisplayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserDao userDao;
public DisplayServlet() {
super();
}
@Override
public void init() throws ServletException {
super.init();
// 获取数据库连接
Connection connection = (Connection) getServletContext().getAttribute("connection");
userDao = new UserDao(connection);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 查询所有用户数据
List<User> userList = userDao.findAll();
request.setAttribute("userList", userList);
request.getRequestDispatcher("/WEB-INF/views/display.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
```
阅读全文