JDBC抽取Base类时,出现的让人抓狂了一夜的NoClassDefFoundError: Could not initialize class XXX恶心人的BUG

在这里插入图片描述
bug模样:Exception in thread “main” java.lang.NoClassDefFoundError: Could not initialize class com.ibentu.jdbc.JdbcBase

能输入用户名,也能输入密码,就是不能够在数据库校验,为什么呢?在查了众多建议之后,有说因为jar包问题,官方文档那生涩的解释更是不能理解。

仔细检查了一遍代码,原来是抽取Base类的时候,一不小心把properties的路径弄扯了
在这里插入图片描述

将此处改为下面的样子即可,因为此路径默认在src下面寻找
在这里插入图片描述

之后运行,OK了
在这里插入图片描述

附上源码:此源码有SQL注入风险
JdbcBase.java

/**
 * JDBC工具类
 */
public  class JdbcBase {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    /**
     * 文件的读取只需要读取一次,即可拿到这些值,使用静态代码块
     */
    static {
        //读取资源文件、获取值
        try {
            //1.创建Properties集合类
            Properties properties = new Properties();
            //2.获取src路径下文件的方式--->ClassLoader 类加载器
            ClassLoader classLoader = JdbcBase.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            System.out.println(path);
            //3.加载文件
            properties.load(new FileReader(path));
            //4.获取数据,赋值
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");
            //5.注册驱动
            Class.forName(driver);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
        /**
         *  获取连接
         */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    /**
     * 释放资源
     * @param resultSet
     * @param statement
     * @param connection
     */

    public static void close(ResultSet resultSet, Statement statement, Connection connection){

        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

UserRegister.java

public class UserRegister {

    public static void main(String[] args) {
        //1.键盘录入,接收用户名和密码
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();

        //2.调用方法
        UserRegister userRegister = new UserRegister();
        boolean flag = userRegister.login(username,password);
        //3.判断结果,输出不同语句
        if (flag ){
            //登陆成功
            System.out.println("登陆成功");
        }else {
            System.out.println("用户名或密码错误");
        }
    }
    /**
     * 登陆方法
     */
    //连接数据库,判断是否登陆成功

    public boolean login(String username, String password){
        if (username == null || password == null){
            return false;
        }


        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet =null;
        try {
            //1.获取连接
            connection = JdbcBase.getConnection();
            //2.定义sql
            String sql = "select * from user where username = '" + username+"' and  password = '" + password + "' ";
            //3.获取执行sql的对象
            statement = connection.createStatement();
            //4.执行查询
            resultSet = statement.executeQuery(sql);
            //5.判断
            /*if (resultSet.next()){//如果有下一行,则返回true
                return true;
            }else {
                return false;
            }*/
            return resultSet.next();


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            System.out.println("finally未执行");
            JdbcBase.close(resultSet, statement, connection);
            System.out.println("finally已执行");
        }
        return false;
    }
}

数据库user表
在这里插入图片描述

----------------------------Happy End----------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值