java注册登录代码

Java注册登录代码主要包括以下几个部分:

  1. 用户实体类(User):用于存储用户的基本信息,如用户名、密码等。
  2. 数据库操作类(DatabaseUtil):用于连接数据库、执行SQL语句等。
  3. 注册服务类(RegisterService):用于处理用户注册的逻辑,如检查用户名是否已存在、生成随机密码等。
  4. 登录服务类(LoginService):用于处理用户登录的逻辑,如验证用户名和密码是否正确。
  5. 控制器类(Controller):用于处理HTTP请求,调用相应的服务类方法。

以下是一个简单的Java注册登录代码示例:

// 用户实体类
public class User {
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    // getter和setter方法
}

// 数据库操作类
import java.sql.*;

public class DatabaseUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    public static boolean checkUsernameExist(String username) {
        String sql = "SELECT * FROM users WHERE username = ?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void register(User user) {
        String sql = "INSERT INTO users (username, password) VALUES (?, ?)";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

// 注册服务类
public class RegisterService {
    public boolean register(User user) {
        if (DatabaseUtil.checkUsernameExist(user.getUsername())) {
            return false;
        } else {
            DatabaseUtil.register(user);
            return true;
        }
    }
}

// 登录服务类
public class LoginService {
    public boolean login(String username, String password) {
        String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}

// 控制器类
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Controller {
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String action = request.getParameter("action");
        if ("register".equals(action)) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            User user = new User(username, password);
            RegisterService registerService = new RegisterService();
            boolean isSuccess = registerService.register(user);
            if (isSuccess) {
                response.getWriter().write("注册成功");
            } else {
                response.getWriter().write("用户名已存在");
            }
        } else if ("login".equals(action)) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            LoginService loginService = new LoginService();
            boolean isSuccess = loginService.login(username, password);
            if (isSuccess) {
                response.getWriter().write("登录成功");
            } else {
                response.getWriter().write("用户名或密码错误");
            }
        } else {
            response.getWriter().write("无效的操作");
        }
    }
}

这个示例仅供参考,实际应用中还需要考虑安全性、异常处理等问题。

你可能感兴趣的:(java,开发语言)