通过 jdbc 瘦驱动程序连接时出现 ORA-01017

ORA-01017 when connecting through jdbc thin driver

使用

我正在尝试使用 JDBC 驱动程序从 Java 应用程序连接到数据库。失败并显示 "ORA-01017: invalid username/password; logon denied" 消息。

我电脑上安装的东西(我无能为力):

我认为这些没有干扰,因为从 Eclipse 项目使用的库中删除 ojdbc6.jar 不会产生任何输出(没有错误,select 子句也没有输出), 所以我相当确定实际上正在使用瘦驱动程序。

我创建了一个小型测试应用程序来演示该问题:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class OracleTester {

    public static void main(String[] args) {
        String database = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx.xx.xxx.xxx)(PORT=13301)))(CONNECT_DATA=(SERVICE_NAME=something)))";

        String username = "myUser";
        String password = "myPass";

        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection conn = DriverManager.getConnection(database,username,password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select 'hello' from dual");
            while(rs.next()){
                System.out.println("output: " + rs.getString(0));
            }
            conn.close();
        }
        catch(Exception e){
            System.out.println(e.getLocalizedMessage());
        }

        System.out.println("Done!");
    }

}

输出:

ORA-01017: invalid username/password; logon denied

Done!

一位友好的数据库管理员前来救援,发现这实际上是一个 Oracle 错误:

Problem Description:
--------------------
When trying to connect by using the JDBC THIN 11g driver to a database 11g 
using Enterprise User Security (EUS) connections throw invalid username/

When usign the JDBC OCI driver the connection can be made.

现在 - 戴上你的帽子:

Available Workarounds:
----------------------
Use OCI.

请注意,我使用的是 11.2.0.4,而错误说

Tested Versions:
----------------
JDBC THIN Driver 11.1.0.6.0 and 11.1.0.7.0

显然它已经存在了一段时间。我不确定我是否理解 - 如果无法正确连接到数据库,他们为什么要推出该驱动程序的新版本?这似乎是每个人在使用瘦驱动程序时遇到的第一个问题?

但是,我们的本地数据库管理员英雄发现了这个:

Set the property oracle.jdbc.thinLogonCapability=o3 for the JDBC connection by passing the option oracle.jdbc.thinLogonCapability=o3 on the command line.  

For example:
java -Doracle.jdbc.thinLogonCapability=o3 <Java Class>

There is no loss of security when following this workaround. 

在 Eclipse 中,我已将此行添加到 VM 参数(运行 -> 运行 配置 -> 参数 -> VM 参数 -> 添加 -Doracle.jdbc.thinLogonCapability=o3 ) 而且,你瞧,我终于可以进入数据库了。

EUS(企业用户)在从 11.2.0.3 开始的 JDBC-thin 驱动程序中受支持,但您是否确认用户 "myUser" 在数据库中确实被定义为 EUS? EUS 用户在中央目录(例如 OID)中定义,而不是您通过 "create user".

在数据库中创建的普通用户

附加说明:SQLDeveloper 使用 JDBC 瘦驱动程序。因此,如果您可以连接 SQLDeveloper,那么您应该也可以从独立的 JDBC 程序进行连接。只需确保 SQLDeveloper 使用与您正在使用的相同的 jdbc jar。如果您想检查您正在使用的 JDBC 驱动程序的版本,只需执行 "java -jar ojdbc7.jar" 即可打印版本。

如果在 oracle thin wallet 连接配置中指定了用户名和密码(即使是空值),也会发生类似的异常。以下配置将起作用。

    System.setProperty("oracle.net.wallet_location", [wallet location]);
    System.setProperty("oracle.net.tns_admin", [Tns location]);

    "url" =  "jdbc:oracle:thin:/@tns_alias"
    "driver_class" =  "oracle.jdbc.driver.OracleDriver"