OCTDN 2021-07-11 16:42 采纳率: 36.4%
浏览 76
已结题

junit测试时,说无法初始化BaseDao类,为啥啊

java.lang.NoClassDefFoundError: Could not initialize class com.OCTDN.dao.BaseDao

at com.OCTDN.service.users.UsersServiceImpl.login(UsersServiceImpl.java:31)
at com.OCTDN.service.users.UsersServiceImpl.test(UsersServiceImpl.java:39)

UsersServiceImpl.login(UsersServiceImpl.java:31):

public Users login(String userCode, String userPassword) {
        Connection connection = null;
        Users users = null;

        try {
            connection = BaseDao.getConnection();
            //通过业务层调用对应的数据库进行操作
            users = usersDao.getLoginUser(connection,userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResources(connection,null,null);
        }
        return users;
    }

UsersServiceImpl.test(UsersServiceImpl.java:39):

@Test
    public void test(){
        UsersServiceImpl usersService = new UsersServiceImpl();
        Users admin = usersService.login("admin", "964878");
        System.out.println(admin.getUserPassword());
    }

BaseDao:

package com.OCTDN.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

//操作数据库的公共类
public class BaseDao {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //静态代码块,类加载时自动初始化
    static {
        Properties properties = new Properties();
        //通过类加载器(getClassLoader)来读取对应的资源
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");

        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    //获取数据库链接
    public static Connection getConnection(){
        Connection connection =null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    //编写【查询】公共方法
    public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
        //prepareStatement对象预编译SQL语句
        preparedStatement = connection.prepareStatement(sql);

        //Object的占位符是从1开始,但是数组是从0开始,所以需要加1
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        //由于prepareStatement对象预编译了SQL语句,所以此处可以执行
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    //编写【增/删/改】公共方法
    public static int execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws SQLException {
        //prepareStatement对象预编译SQL语句
        preparedStatement = connection.prepareStatement(sql);

        //Object的占位符是从1开始,但是数组是从0开始,所以需要加1
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        //由于prepareStatement对象预编译了SQL语句,所以此处可以执行
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }

    //释放资源
    public static boolean closeResources(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) {
        boolean flag = true;
        if (resultSet!= null){
            try {
                resultSet.close();
                //GC垃圾回收
                resultSet = null;
            } catch (SQLException e) {
                e.printStackTrace();
                //如果GC回收失败
                flag = false;
            }

        }

        if (preparedStatement!= null){
            try {
                preparedStatement.close();
                //GC垃圾回收
                preparedStatement = null;
            } catch (SQLException e) {
                e.printStackTrace();
                //如果GC回收失败
                flag = false;
            }

        }

        if (connection!= null){
            try {
                connection.close();
                //GC垃圾回收
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                //如果GC回收失败
                flag = false;
            }

        }

        return flag;
    }

}

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-07-11 16:54
    关注

    登录方法中,你创建BaseDao对象,通过对象去获取连接试试。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月19日
  • 已采纳回答 9月11日
  • 创建了问题 7月11日