(二)java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK

不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK

1、启动报错

java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
详细报错内容:
在这里插入图片描述

2、背景

使用 JDBC 连接Oracle数据库时出现报错。示例代码如下:

DataSourceConnectionUtils.java

package com.example.jdbctemplateproject.utils;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源连接工具
 *
 * @author: shipleyleo
 * @create: 2023-04-07 17:32:16
 */
public class DataSourceConnectionUtils {

    public static void jdbcTest(String url, String username, String password) throws ClassNotFoundException, SQLException {
        //注册driver
        Class.forName("oracle.jdbc.driver.OracleDriver");
		//建立数据库连接对象
        Connection conn = DriverManager.getConnection(url, username, password);
		//建立操作对象
        Statement stmt = conn.createStatement();
		//结果集
        ResultSet rs = stmt.executeQuery("select * from student");
        while(rs.next()) { // 转换每行的返回值到 Map 中
            System.out.println("id:" + rs.getLong("id") + ",name:" + rs.getString("name"));
        }
		//依次关闭结果集,操作对象,数据库对象
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }


    public static void main(String[] args) {
        try {
            jdbcTest("jdbc:oracle:thin:@localhost:1521:orcl", "system", "*********");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

下面是已添加的Oracle驱动包、支持字符集的依赖包。
pom.xml

		<!-- Oracle 驱动包 -->
		<dependency>
			<groupId>com.oracle.database.jdbc</groupId>
			<artifactId>ojdbc8</artifactId>
			<version>21.5.0.0</version>
			<scope>runtime</scope>
		</dependency>
		<!-- Additional library required to support Internationalization -->
		<dependency>
			<groupId>com.oracle.database.nls</groupId>
			<artifactId>orai18n</artifactId>
			<version>21.5.0.0</version>
			<scope>provided</scope>
		</dependency>

3、原因分析

根据提示,报错是由于缺少字符集相关的 orai18n 依赖包所致。但是检查 pom.xml 文件,发现有配置 orai18n 依赖包。经排查测试,发现跟<scope>标签内的取值有关。当 scope 取值为 provided、test (不支持运行期)时,执行main方法会出现报错;当 scope 取值为 runtime、compile (支持运行期)时,执行main方法不会出现报错。

在这里插入图片描述

4、解决方案

将 scope 的值改为 runtime 或者 compile(当然,也可以直接将 scope标签去掉,系统会默认选择compile)。如下所示:

pom.xml

		<!-- Additional library required to support Internationalization -->
		<dependency>
			<groupId>com.oracle.database.nls</groupId>
			<artifactId>orai18n</artifactId>
			<version>21.5.0.0</version>
			<scope>compile</scope>
		</dependency>

参考资料

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在给定的引用,出现了多个java.sql.SQLException: 不支持字符集 (在路径添加 orai18n.jar): ZHS16GBK的错误。这个错误通常出现在使用Java连接数据库时,数据库无法识别或不支持指定的字符集ZHS16GBK。为了解决这个问题,需要在路径添加orai18n.jar文件。这个文件是Oracle数据库提供的一个包,其包含了支持各种字符集的工具和资源。通过添加这个文件,可以使Java程序正确地连接并与数据库进行通信。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【整合springboot和oracle报错】不支持字符集 (在路径添加 orai18n.jar): ZHS16GBK](https://blog.csdn.net/u011250186/article/details/128424496)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java.sql.SQLException: 不支持字符集 (在路径添加 orai18n.jar): ZHS16GBK ](https://download.csdn.net/download/weixin_37477226/86727797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值