要使用Java连接MySQL实现登录注册,首先需要导入JDBC驱动,然后创建数据库连接,执行SQL语句进行操作。
环境准备
1、安装MySQL数据库,并创建用户表和用户信息表。
CREATE DATABASE test; USE test; CREATE TABLE user ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL );
2、下载MySQL Connector/J(JDBC驱动),并将其添加到Java项目的类路径中。
Java代码实现登录注册功能
1、导入相关包:
import java.sql.*; import java.util.Scanner;
2、编写数据库连接工具类:
public class DBUtil { private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; private static final String USER = "root"; private static final String PASSWORD = "your_password"; static { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() { return null; } }
3、编写用户类:
public class User { private int id; private String username; private String password; // 构造方法、getter和setter方法省略... }
4、编写用户管理类:
public class UserManager { public boolean register(User user) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = DBUtil.getConnection(); String sql = "INSERT INTO user (username, password) VALUES (?, ?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); preparedStatement.setString(2, user.getPassword()); int result = preparedStatement.executeUpdate(); return result > 0; } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源... } return false; } public User login(String username, String password) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = DBUtil.getConnection(); String sql = "SELECT * FROM user WHERE username = ? AND password = ?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, username); preparedStatement.setString(2, password); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { User user = new User(); user.setId(resultSet.getInt("id")); user.setUsername(resultSet.getString("username")); user.setPassword(resultSet.getString("password")); return user; } else { return null; } } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源... } return null; } }
5、编写主程序:
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请选择操作:1.注册 2.登录"); int operation = scanner.nextInt(); scanner.nextLine(); // 清除缓冲区换行符 UserManager userManager = new UserManager(); switch (operation) { case 1: // 注册操作 System.out.println("请输入用户名:"); String username = scanner.nextLine(); System.out.println("请输入密码:"); String password = scanner.nextLine(); if (userManager.register(new User(username, password))) { System.out.println("注册成功!"); } else { System.out.println("注册失败,请检查用户名是否已存在!"); } break; case 2: // 登录操作 System.out.println("请输入用户名:"); String inputUsername = scanner.nextLine(); System.out.println("请输入密码:"); String inputPassword = scanner.nextLine(); User user = userManager.login(inputUsername, inputPassword); if (user != null) { System.out.println("登录成功,欢迎 " + user.getUsername() + "!"); } else { System.out.println("登录失败,请检查用户名和密码是否正确!"); } break; default: // 无效操作,退出程序 System.out.println("无效操作,程序退出!"); break; } } }
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/641135.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复