Javaee登录注册数据库代码
时间: 2023-09-04 14:15:24 浏览: 141
以下是使用 JavaEE 技术实现登录注册功能的示例代码,使用 MySQL 数据库进行存储和查询。
### 数据库配置
首先需要创建一个名为 `user` 的数据库,并创建一个名为 `users` 的表,表结构如下:
```sql
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
### 注册功能
```java
package com.example;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username == null || username.trim().length() == 0) {
request.setAttribute("message", "请输入用户名!");
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}
if (password == null || password.trim().length() == 0) {
request.setAttribute("message", "请输入密码!");
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
// 查询是否已存在该用户
String sql = "SELECT * FROM users WHERE username=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
request.setAttribute("message", "该用户名已被注册!");
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}
// 插入新用户
sql = "INSERT INTO users (username, password) VALUES (?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ps.executeUpdate();
request.setAttribute("message", "注册成功!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
request.setAttribute("message", "注册失败!");
request.getRequestDispatcher("/register.jsp").forward(request, response);
}
}
}
```
### 登录功能
```java
package com.example;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
// 查询用户是否存在
String sql = "SELECT * FROM users WHERE username=? AND password=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
// 登录成功,将用户信息存入 Session
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect(request.getContextPath() + "/main.jsp");
} else {
request.setAttribute("message", "用户名或密码错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
request.setAttribute("message", "登录失败!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
```
以上示例代码仅供参考,实际开发中还需考虑数据加密、安全性等问题。
阅读全文