navicat
时间: 2023-07-31 12:05:09 浏览: 135
好的,下面是一个使用MySQL数据库和Servlet的基本登录注册代码示例:
1. 创建一个名为`User`的JavaBean类,包含以下属性和方法:
```java
public class User {
private String username;
private String password;
// 构造方法
public User() {}
public User(String username, String password) {
this.username = username;
this.password = password;
}
// getters 和 setters
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;
}
}
```
2. 创建一个名为`DBUtil`的数据库工具类,用于连接数据库和执行SQL语句:
```java
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "123456";
// 获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
// 执行查询操作
public static ResultSet executeQuery(String sql, Object... params) {
Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
// 执行更新操作
public static int executeUpdate(String sql, Object... params) {
Connection conn = getConnection();
PreparedStatement pstmt = null;
int result = 0;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, null);
}
return result;
}
// 关闭资源
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. 创建一个名为`UserDao`的数据访问对象类,用于对用户表进行CRUD操作:
```java
public class UserDao {
// 添加用户
public boolean addUser(User user) {
String sql = "INSERT INTO user(username, password) VALUES(?, ?)";
int result = DBUtil.executeUpdate(sql, user.getUsername(), user.getPassword());
return result > 0;
}
// 根据用户名查询用户
public User getUserByUsername(String username) {
User user = null;
String sql = "SELECT * FROM user WHERE username=?";
ResultSet rs = DBUtil.executeQuery(sql, username);
try {
if (rs.next()) {
user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(null, null, rs);
}
return user;
}
}
```
4. 创建一个名为`LoginServlet`的Servlet类,用于处理用户登录请求:
```java
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private UserDao userDao;
@Override
public void init() throws ServletException {
userDao = new UserDao();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取表单参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 根据用户名查询用户
User user = userDao.getUserByUsername(username);
// 验证用户信息
if (user != null && user.getPassword().equals(password)) {
// 登录成功,将用户信息保存到Session中
HttpSession session = request.getSession();
session.setAttribute("user", user);
// 跳转到首页
response.sendRedirect(request.getContextPath() + "/index.jsp");
} else {
// 登录失败,返回错误信息
request.setAttribute("error", "用户名或密码错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
```
5. 创建一个名为`RegisterServlet`的Servlet类,用于处理用户注册请求:
```java
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private UserDao userDao;
@Override
public void init() throws ServletException {
userDao = new UserDao();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取表单参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 封装用户对象
User user = new User(username, password);
// 添加用户
boolean result = userDao.addUser(user);
// 返回结果
if (result) {
request.setAttribute("message", "注册成功,请登录!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
} else {
request.setAttribute("error", "注册失败,请重试!");
request.getRequestDispatcher("/register.jsp").forward(request, response);
}
}
}
```
以上就是一个基本的登录注册代码示例,您可以根据自己的需要进行修改和完善。
阅读全文