Not able to connect to oracle database using JDBC if password is having special characters

前端 未结 2 577
猫巷女王i
猫巷女王i 2020-12-21 15:53

I am trying to connect to oracle database using JDBC.

following is the code::

public class OraclePwdTest {
static{
    try {           
        Clas         


        
相关标签:
2条回答
  • 2020-12-21 16:31

    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 \ ;-)

    0 讨论(0)
  • 2020-12-21 16:57

    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

    0 讨论(0)
提交回复
热议问题