2
votes

I am trying to connect to oracle database using JDBC.

following is the code::

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: 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)

Please help me to resolve the issue.

2
When you connect using sqlplus using special characters, does it work?Jacob
Just a side note: why are you connecting as SYSDBA from within your application code. That sounds like a terrible idea.a_horse_with_no_name
no..with sqlplus also it doent work..Anjali
If it does not work on dos command with command sqlplus user/password@SID then the user/password combination it self is wrong... try with a valid user/password.Jay
If it doesn't work in SQL*Plus then apparently your password is wrong. @Jay: there is no "DOS" anymore...a_horse_with_no_name

2 Answers

5
votes

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

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

String pwd = "s@novi123";

to

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

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

2
votes

A simple code would looks like this... As you are using thin driver you don't need to complicate with all those values from 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);

ie. jdbc:oracle:thin:@192.168.20.145:1521:oradg