Not able to connect to oracle database using JDBC if password is having special characters
java, jdbc, oracle
Solution
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 `\` ;-)
Problem
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.