JDBC编程连接数据库以及异常原因以及解决方法Can‘t find bundle for base name com/ni/jdbc/config, locale zh_CN

一、JDBC编程连接数据库

(一)示例代码

public class JDBCUtils0 {
    
    
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    static{
    
    
        ResourceBundle config = ResourceBundle.getBundle("config", new Locale("zh", "CN"));
        driver = config.getString("driver");
        url = config.getString("url");
        username = config.getString("username");
        password = config.getString("password");
        try {
    
    
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
    
    
            throw new RuntimeException(e);
        }
    }

    public static Connection lianJie() {
    
    
        //第五步:获得数据库连接
        try {
    
    
            Connection connection =  DriverManager.getConnection(url,username,password);
            return connection;
        } catch (SQLException e) {
    
    
            e.printStackTrace();
            //这里会发生SQL异常,因为我们提供的的账户和密码不一定能连接成功
            return null;
        }


    }

    public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){
    
    
        if (resultSet != null) {
    
    
            try {
    
    
                resultSet.close();
            } catch (SQLException e) {
    
    
                throw new RuntimeException("关闭resultSet失败");
            }
        }

        if (preparedStatement != null) {
    
    
            try {
    
    
                preparedStatement.close();
            } catch (SQLException e) {
    
    
                throw new RuntimeException("关闭preparedStatement失败");
            }
        }

        if (connection != null) {
    
    
            try {
    
    
                connection.close();
            } catch (SQLException e) {
    
    
                throw new RuntimeException("关闭connection失败");
            }
        }
    }
}

(二)配置文件

在这里插入图片描述

driver=com.mysql.cj.jdbc.Driver

url=jdbc:mysql://localhost:3306/emp

username=root

password=nigx=0128

二、异常原因以及解决方法Can’t find bundle for base name com/ni/jdbc/config, locale zh_CN

Exception in thread "main" java.lang.ExceptionInInitializerError at com.ni.eg.jdbc.Test.main(Test.java:19) Caused by: java.util.MissingResourceException: Can't find bundle for base name com/ni/jdbc/config, locale zh_CN

(一)从错误信息来看,这是一个在Java程序中发生的异常。异常的具体信息是“java.lang.ExceptionInInitializerError”,这通常表示在静态初始化块或静态变量初始化过程中发生了异常。

从错误信息的下一部分“Caused by: java.util.MissingResourceException: Can’t find bundle for base name com/ni/jdbc/config, locale zh_CN”,可以看出,问题出现在寻找资源上,具体的资源是“com/ni/jdbc/config”的bundle,而且需要的是中文(zh_CN)的版本。

(二)为了解决这个问题,你需要检查以下几点:

  1. 确保“com/ni/jdbc/config”的资源文件存在,并且文件名正确。
    Java中,资源文件通常以.properties结尾,所以你可能需要检查文件名是否为“com.ni.jdbc.config.properties”。
  2. 确保资源文件位于正确的位置。根据Java的类路径规则,资源文件应该放在与类文件相同的包(package)目录下,或者放在src/main/resources目录下。

重点:注意Maven项目

  1. 确认资源文件的命名和位置是针对你的项目的结构和构建方式的。例如,如果你正在使用Maven,那么资源文件应该放在src/main/resources目录下。在这里插入图片描述

  2. 确认文件的内容是正确的。你需要打开文件并检查文件内容是否符合预期。

  3. 如果你的项目是多模块的,那么需要检查其他模块是否也有相同的资源文件,并且它们的位置和命名都是正确的。

一旦你解决了这个问题,你的程序应该就可以正常运行了。

猜你喜欢

转载自blog.csdn.net/weixin_48053866/article/details/132735034
今日推荐