不支持的字符集 (在類路徑中添加 orai18n.jar): ZHS16GBK
- (一)SQL state [99999]; error code [17056]; 不支持的字符集 (在類路徑中添加 orai18n.jar): ZHS16GBK; nested exception
- (二)java.sql.SQLException: 不支持的字符集 (在類路徑中添加 orai18n.jar): ZHS16GBK
1、啟動報錯
java.sql.SQLException: 不支持的字符集 (在類路徑中添加 orai18n.jar): ZHS16GBK
詳細(xì)報錯內(nèi)容:
2、背景
使用 JDBC 連接Oracle數(shù)據(jù)庫時出現(xiàn)報錯。示例代碼如下:
DataSourceConnectionUtils.java
package com.example.jdbctemplateproject.utils;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
/**
* 數(shù)據(jù)源連接工具
*
* @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");
//建立數(shù)據(jù)庫連接對象
Connection conn = DriverManager.getConnection(url, username, password);
//建立操作對象
Statement stmt = conn.createStatement();
//結(jié)果集
ResultSet rs = stmt.executeQuery("select * from student");
while(rs.next()) { // 轉(zhuǎn)換每行的返回值到 Map 中
System.out.println("id:" + rs.getLong("id") + ",name:" + rs.getString("name"));
}
//依次關(guān)閉結(jié)果集,操作對象,數(shù)據(jù)庫對象
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驅(qū)動包、支持字符集的依賴包。
pom.xml
<!-- Oracle 驅(qū)動包 -->
<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、原因分析
根據(jù)提示,報錯是由于缺少字符集相關(guān)的 orai18n 依賴包所致。但是檢查 pom.xml 文件,發(fā)現(xiàn)有配置 orai18n 依賴包。經(jīng)排查測試,發(fā)現(xiàn)跟<scope>
標(biāo)簽內(nèi)的取值有關(guān)。當(dāng) scope 取值為 provided、test (不支持運(yùn)行期
)時,執(zhí)行main方法會出現(xiàn)報錯;當(dāng) scope 取值為 runtime、compile (支持運(yùn)行期
)時,執(zhí)行main方法不會出現(xiàn)報錯。
4、解決方案
將 scope 的值改為 runtime 或者 compile(當(dāng)然,也可以直接將 scope標(biāo)簽去掉,系統(tǒng)會默認(rèn)選擇compile)。如下所示:
pom.xml文章來源:http://www.zghlxwxcb.cn/news/detail-708830.html
<!-- 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>
參考資料文章來源地址http://www.zghlxwxcb.cn/news/detail-708830.html
- maven 中 scope標(biāo)簽的作用(runtime、provided、test、compile 的作用)
到了這里,關(guān)于(二)java.sql.SQLException: 不支持的字符集 (在類路徑中添加 orai18n.jar): ZHS16GBK的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!