使用MVC模式链接数据库实现用户注册登录功能,要求有User类,UserDao类,UserController类
时间: 2024-01-07 11:02:43 浏览: 101
好的,我来回答你的问题。
首先,MVC是一种常用的软件架构模式,它将应用程序分为三个核心部分:模型(Model)、视图(View)和控制器(Controller)。
在这个应用程序中,我们需要实现用户注册登录功能,因此需要三个类:User类、UserDao类和UserController类。
User类是一个简单的Java类,它包含了用户的基本信息,如用户名、密码、邮箱等。它的代码如下:
```
public class User {
private int id;
private String username;
private String password;
private String email;
// 构造方法
public User(int id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
```
UserDao类是一个数据访问对象类,它用于与数据库进行交互,实现对用户信息的增删改查等操作。它的代码如下:
```
public class UserDao {
// 连接数据库
private Connection conn;
// 构造方法
public UserDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "password";
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加用户
public boolean addUser(User user) {
boolean flag = false;
PreparedStatement pstmt = null;
try {
String sql = "insert into user(username,password,email) values(?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
// 根据用户名查询用户
public User getUserByUsername(String username) {
User user = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String sql = "select * from user where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String password = rs.getString("password");
String email = rs.getString("email");
user = new User(id, username, password, email);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
}
```
UserController类是一个控制器类,它用于处理用户的请求和响应,实现用户注册登录等功能。它的代码如下:
```
public class UserController {
// 用户注册
public String register(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
User user = new User(0, username, password, email);
UserDao userDao = new UserDao();
boolean flag = userDao.addUser(user);
if (flag) {
return "register_success.jsp";
} else {
return "register_failure.jsp";
}
}
// 用户登录
public String login(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
UserDao userDao = new UserDao();
User user = userDao.getUserByUsername(username);
if (user != null && user.getPassword().equals(password)) {
return "login_success.jsp";
} else {
return "login_failure.jsp";
}
}
}
```
以上就是使用MVC模式链接数据库实现用户注册登录功能的完整示例代码。
阅读全文