java实现登录注册功能
时间: 2023-10-11 12:13:22 浏览: 624
实现登录注册功能需要以下几个步骤:
1. 创建数据库,并创建用户表,包含字段:id、username、password。
2. 编写Java代码,连接数据库,实现用户的注册和登录功能。
3. 注册功能:用户输入用户名和密码,Java代码将其插入用户表中。
4. 登录功能:用户输入用户名和密码,Java代码查询用户表中是否存在该用户,如果存在,判断密码是否正确,如果正确,则登录成功;否则登录失败。
以下是实现登录和注册功能的Java代码示例:
1. 连接数据库
```
public class DBUtil {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
private static final String USER = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
return conn;
}
}
```
2. 注册功能
```
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
try {
Connection conn = DBUtil.getConnection();
String sql = "insert into user(username, password) values(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ps.executeUpdate();
out.print("<script>alert('注册成功!');window.location.href='login.jsp'</script>");
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
out.print("<script>alert('注册失败!');window.location.href='register.jsp'</script>");
e.printStackTrace();
}
}
}
```
3. 登录功能
```
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
try {
Connection conn = DBUtil.getConnection();
String sql = "select * from user where username=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String psw = rs.getString("password");
if (password.equals(psw)) {
out.print("<script>alert('登录成功!');window.location.href='index.jsp'</script>");
} else {
out.print("<script>alert('密码错误!');window.location.href='login.jsp'</script>");
}
} else {
out.print("<script>alert('用户名不存在!');window.location.href='login.jsp'</script>");
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上代码示例仅供参考,实际应用中需要根据具体需求进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)