Java 如果密码包含特殊字符,则无法使用 JDBC 连接到 oracle 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21908610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not able to connect to oracle database using JDBC if password is having special characters
提问by Anjali
I am trying to connect to oracle database using JDBC.
我正在尝试使用 JDBC 连接到 oracle 数据库。
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
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)
Please help me to resolve the issue.
请帮我解决这个问题。
采纳答案by San
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
当特殊字符出现在用户名,密码或类似的连接字符串@
,/
等等,我们有内双引号括起来,例如,如果密码是它包含p@ssword
我们在sqlplus连接为username/"p@ssword"@database
You can try the same in java by enclosing your password in double quotes using escape characters, try changing
您可以通过使用转义字符将密码括在双引号中,在 java 中尝试相同的方法,尝试更改
String pwd = "s@novi123";
to
到
String pwd = "\"s@novi123\"";
I am not a java expert, just guessed the scape character should be \
;-)
我不是 Java 专家,只是猜测 scape 字符应该是\
;-)
回答by Jay
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.
一个简单的代码看起来像这样...当您使用瘦驱动程序时,您不需要将 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
IE。jdbc:oracle:thin:@192.168.20.145:1521:oradg