如果密码具有特殊字符,则无法使用JDBC连接到oracle数据库

时间:2014-02-20 13:02:04

标签: java oracle jdbc

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

以下是代码::

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();
    }
}

}

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

我收到以下错误消息::

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

    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)

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:4)

当用户名,密码或连接字符串(如@/等)中有特殊字符时,我们必须将其包含在双引号中,例如,如果密码为{{1}我们在sqlplus中连接为p@ssword

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

username/"p@ssword"@database

String pwd = "s@novi123";

我不是Java专家,只是猜测角色应该是String pwd = "\"s@novi123\""; ; - )

答案 1 :(得分:1)

一个简单的代码看起来像这样......当您使用瘦驱动程序时,您不需要复制来自tnsnames.ora的所有值。

        Class.forName("oracle.jdbc.OracleDriver");
        Properties properties = new Properties();
        properties.setProperty("user", username);
        properties.setProperty("password", password);
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@<DB_HOST>:<PORT>:<SID>, properties);

即。 JDBC:预言:瘦:@ 192.168.20.145:1521:oradg