Java+Swing: 连接数据库并完成登录验证 整理10

2023-12-14 19:25:18

1. 封装连接数据库的工具类

package com.utils;

import java.sql.*;

/**
 * @Author:xiexu
 * @Date:2023/12/11 10:13
 */
// 连接数据库的工具类
public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/student_score?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    static {  // 加载一下驱动
        try {
            Class.forName(DRIVER);  // 执行com.mysql.cj.jdbc.Driver 里面的静态代码块
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 获取数据库连接
    public static Connection getConnection() throws SQLException {
        try {
            Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 关闭连接
    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closePS(PreparedStatement preparedStatement) {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeRS(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

详细解释:?

(1)

    static {  // 加载一下驱动
        try {
            Class.forName(DRIVER);  // 执行com.mysql.cj.jdbc.Driver 里面的静态代码块
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

这是一个静态的代码块,当类加载的时候,在静态代码块中,通过Class.forName() 来动态加载JDBC的驱动类,便于后续的数据库连接操作

(2)

    public static Connection getConnection() throws SQLException {
        try {
            Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

建立数据库连接, 通过DriverManager.getConnection()方法获取参数,来建立连接

(3)

// 关闭连接
    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

// 关闭相应的资源
    public static void closePS(PreparedStatement preparedStatement) {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeRS(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

通过 closeConnection() 方法关闭数据库的连接, 通过closePS()和closeRS() 关闭相关的资源

?

2. 实体类

因为是从数据库中查找数据进行校验,所以需要定义一个实体类,对应数据库中的字段

@Data
public class Admin {
    private Integer id;
    private String userName;
    private String pwd;
}

这里面get和set方法使用@Data注解就搞定了?

?

3. service层

从数据库中查找数据进行校验,并返回boolean类型

3.1 定义一个接口

package com.service;

import com.entity.Admin;

import java.sql.SQLException;

/**
 * @Author:xiexu
 * @Date:2023/12/11 10:33
 */
public interface AdminService {
    boolean validateAdmin(Admin admin) throws SQLException;
}

3.2 实现类(实现接口的方法)

package com.service.Impl;

import com.entity.Admin;
import com.service.AdminService;
import com.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author:xiexu
 * @Date:2023/12/11 10:40
 */
public class AdminServiceImpl implements AdminService {
    @Override
    public boolean validateAdmin(Admin admin) throws SQLException {
        String userName = admin.getUserName();
        String pwdParam = admin.getPwd();
        String sql = "select pwd from admin where user_name = ?";
        Connection connection = DBUtil.getConnection();

        // 空指针,返回false
        if (connection ==null) {
            return false;
        }
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, userName); // 将userName参数设置到查询语句中的第一个占位符处。
        ResultSet resultSet = preparedStatement.executeQuery(); // 执行查询, 返回resultSet对象

        while (resultSet.next()) {
            String pwd = resultSet.getString(1);
            if (pwdParam.equals(pwd)) {  // 比较
                return true;
            }
        }
        DBUtil.closeRS(resultSet);
        DBUtil.closePS(preparedStatement);
        DBUtil.closeConnection(connection);
        return false;
    }
}

?

4. 登录校验

?

登录界面的详细代码请参考:http://t.csdnimg.cn/4Ia1L? 和??http://t.csdnimg.cn/9Erk3

?

文章来源:https://blog.csdn.net/m0_61495539/article/details/134923049
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。