使用数据库技术和DAO模式设计,完成以下两个页面, 1. 要求注册页 数据 插入到表中。设计一个表User,包含三个字段(性别,年龄,ID) 2. 显示页,显示从数据库中查询到的数据 在ec
时间: 2023-09-19 18:08:05 浏览: 81
通过反射注解批量插入数据到DB的实现方法
抱歉,我是一个语言模型,无法直接在ec上进行开发和部署操作。但我可以为您提供以下代码示例,帮助您完成数据库和DAO的设计。
首先,创建一个User类来表示用户信息:
```java
public class User {
private String id;
private String gender;
private int age;
public User(String id, String gender, int age) {
this.id = id;
this.gender = gender;
this.age = age;
}
// getters and setters
}
```
接下来,创建一个UserDao接口来定义对User表的操作:
```java
public interface UserDao {
void addUser(User user);
List<User> getAllUsers();
}
```
然后,创建一个UserDaoImpl类来实现UserDao接口,使用JDBC连接数据库进行操作:
```java
public class UserDaoImpl implements UserDao {
private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
private Connection conn;
public UserDaoImpl() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
@Override
public void addUser(User user) {
String sql = "INSERT INTO User (id, gender, age) VALUES (?, ?, ?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, user.getId());
ps.setString(2, user.getGender());
ps.setInt(3, user.getAge());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<User> getAllUsers() {
List<User> users = new ArrayList<>();
String sql = "SELECT * FROM User";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String id = rs.getString("id");
String gender = rs.getString("gender");
int age = rs.getInt("age");
User user = new User(id, gender, age);
users.add(user);
}
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
}
```
最后,创建一个RegisterServlet来处理注册请求,将用户信息插入到User表中:
```java
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
User user = new User(id, gender, age);
UserDao userDao = new UserDaoImpl();
userDao.addUser(user);
response.sendRedirect("success.jsp");
}
}
```
创建一个ShowAllUsersServlet来显示所有用户的信息:
```java
public class ShowAllUsersServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao userDao = new UserDaoImpl();
List<User> users = userDao.getAllUsers();
request.setAttribute("users", users);
request.getRequestDispatcher("show.jsp").forward(request, response);
}
}
```
最后,创建两个JSP页面,一个用于在注册页面中输入用户信息,另一个用于显示所有用户的信息。
register.jsp:
```html
<form action="register" method="post">
ID: <input type="text" name="id" /><br />
Gender: <input type="text" name="gender" /><br />
Age: <input type="text" name="age" /><br />
<input type="submit" value="Register" />
</form>
```
show.jsp:
```html
<table>
<tr>
<th>ID</th>
<th>Gender</th>
<th>Age</th>
</tr>
<c:forEach var="user" items="${users}">
<tr>
<td>${user.id}</td>
<td>${user.gender}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</table>
```
以上就是一个简单的使用数据库和DAO模式设计的注册和显示页面的示例。
阅读全文