((1)创建t_users表,字段为id、name、password.email、birthday; (2)创建User类,属性与数据库的七users表中字段一一对应,用于封装数据; 3)使用JDBC相关接口和类实现数据库中数据的查询; (4)创建Servlet程
时间: 2024-01-21 12:16:35 浏览: 176
使用JDBC从数据库中查询数据的方法
5星 · 资源好评率100%
序实现以下功能:
- 注册:将用户提交的信息插入到t_users表中;
- 登录:根据用户输入的用户名和密码查询t_users表中是否存在对应的记录;
- 修改密码:更新t_users表中对应用户的密码字段;
- 查询用户信息:根据用户输入的用户名查询t_users表中对应的记录,并返回给前端页面。
1. 创建t_users表,字段为id、name、password、email、birthday
```sql
CREATE TABLE t_users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
birthday DATE NOT NULL
);
```
2. 创建User类,属性与数据库的t_users表中字段一一对应,用于封装数据
```java
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
// 构造方法、getters、setters
}
```
3. 使用JDBC相关接口和类实现数据库中数据的查询
```java
public class UserDao {
private Connection conn;
public UserDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
// 根据用户名和密码查询用户信息
public User findUser(String name, String password) {
User user = null;
try {
String sql = "SELECT * FROM t_users WHERE name=? AND password=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
// 将用户信息插入到t_users表中
public void addUser(User user) {
try {
String sql = "INSERT INTO t_users(name, password, email, birthday) VALUES(?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
pstmt.setDate(4, user.getBirthday());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 根据用户名查询用户信息
public User findUserByName(String name) {
User user = null;
try {
String sql = "SELECT * FROM t_users WHERE name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
// 更新用户密码
public void updatePassword(User user) {
try {
String sql = "UPDATE t_users SET password=? WHERE name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getName());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
4. 创建Servlet程序实现以下功能:
- 注册:将用户提交的信息插入到t_users表中;
- 登录:根据用户输入的用户名和密码查询t_users表中是否存在对应的记录;
- 修改密码:更新t_users表中对应用户的密码字段;
- 查询用户信息:根据用户输入的用户名查询t_users表中对应的记录,并返回给前端页面。
```java
public class UserServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("register")) {
register(request, response);
} else if (action.equals("login")) {
login(request, response);
} else if (action.equals("changepassword")) {
changePassword(request, response);
} else if (action.equals("finduser")) {
findUser(request, response);
}
}
// 注册
private void register(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
String email = request.getParameter("email");
String birthday = request.getParameter("birthday");
User user = new User();
user.setName(name);
user.setPassword(password);
user.setEmail(email);
user.setBirthday(Date.valueOf(birthday));
userDao.addUser(user);
response.sendRedirect("success.jsp");
}
// 登录
private void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
User user = userDao.findUser(name, password);
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("main.jsp");
} else {
response.sendRedirect("fail.jsp");
}
}
// 修改密码
private void changePassword(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
User user = new User();
user.setName(name);
user.setPassword(password);
userDao.updatePassword(user);
response.sendRedirect("success.jsp");
}
// 查询用户信息
private void findUser(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
User user = userDao.findUserByName(name);
request.setAttribute("user", user);
request.getRequestDispatcher("userinfo.jsp").forward(request, response);
}
public void destroy() {
userDao.close();
}
}
```
阅读全文