本文介绍了如果密码具有特殊字符,则无法使用JDBC连接到oracle数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JDBC连接到oracle数据库。

I am trying to connect to oracle database using JDBC.

以下是代码::

public class OraclePwdTest {
static{
    try {
        Class.forName("oracle.jdbc.OracleDriver");

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String ip ="192.168.20.145";
    String sid = "oradg";
    int port = 1521;
    String user = "sys";
    String pwd = "s@novi123";
    Connection conn = null;
    String url = "jdbc:oracle:thin:"+"(DESCRIPTION =" +
            "(ADDRESS_LIST =" +
            "(ADDRESS = (PROTOCOL = TCP)(HOST = "+ ip +")" +
            "(PORT = " + port + "))" +
                ")" +
            "(CONNECT_DATA = (SRVR=DEDICATED) " +
            "(SID = " + sid +
            "))" +
            ")";
    java.util.Properties prop = new java.util.Properties ();

    prop.put ("user", user);
    prop.put ("password", pwd);
    prop.put ("internal_logon", "sysdba");
    try {
        conn = DriverManager.getConnection(url,prop);

        System.out.println("Connected");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

如果密码具有#@等特殊字符,则上述代码不起作用。它使用普通密码。

If password is having special character like #@...then above code does not work. It works with plain password.

我收到以下错误消息::

I get following error message ::

java.sql.SQLException:ORA -01017:用户名/密码无效;登录被拒绝

java.sql.SQLException: ORA-01017: invalid username/password; logon denied

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:389)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:382)
    at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:573)
    at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:431)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:366)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:752)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:366)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:536)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:228)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at TestOracleConnection.main(TestOracleConnection.java:54)

请帮我解决问题。

推荐答案

如果用户名,密码或连接字符串中有特殊字符,如 @ / 等,我们必须如果密码是 p @ ssword 我们在sqlplus中连接为 username /p @ ssword@database

When special characters are there in username, password or connection string like @, / etc., we have to include it within double quoted, for example, if the password is p@ssword we connect in sqlplus as username/"p@ssword"@database

您可以在java中尝试使用转义字符将密码用双引号括起来,尝试更改

You can try the same in java by enclosing your password in double quotes using escape characters, try changing

String pwd = "s@novi123";

String pwd = "\"s@novi123\"";

我不是java专家,只是猜测scape字符应该是 \\ \\ ; - )

I am not a java expert, just guessed the scape character should be \ ;-)

这篇关于如果密码具有特殊字符,则无法使用JDBC连接到oracle数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:43