通过Java(JDBC)连接到启用SSL的Oracle DB
我正在做一个概念验证(PoC)Java报告项目,在该项目中,我正在使用JDBC Thin驱动程序从我的工作站连接到启用SSL的Oracle数据库.由于数据库已启用SSL,因此我将所有必需的证书添加到Oracle Wallet中,并在Java代码中提供了其位置.证书还添加了JRE的 cacert . Java代码摘录-
I'm doing a Proof-of-Concept (PoC) Java reporting project in which I'm connecting to a SSL-enabled Oracle database from my workstation, using JDBC Thin driver. As the database is SSL-enabled, I added all the required certificates into a Oracle Wallet and provided its location in the Java code. The certs were also added cacert of the JRE. Java code excerpt -
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("ERROR: Oracle JDBC Driver not found");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
String oracleURL = "jdbc:oracle:thin:@(DESCRIPTION(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCPS)(HOST=<hostname>)(PORT=2484)))(CONNECT_DATA=(SERVICE_NAME=<service>)))";
// Provide user ID, password for schema
Properties props = new Properties();
props.setProperty("user", "<user id>");
props.setProperty("password", "<password>");
// Setting properties for SSL
props.setProperty("oracle.net.ssl_cipher_suites", "(ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha, SSL_DH_anon_WITH_3DES_EDE_CBC_SHA, SSL_DH_anon_WITH_RC4_128_MD5,SSL_DH_anon_WITH_DES_CBC_SHA)");
props.setProperty("oracle.net.ssl_client_authentication", "false");
props.setProperty("oracle.net.ssl_version", "3.0");
props.setProperty("oracle.net.encryption_client", "REJECTED");
props.setProperty("oracle.net.crypto_checksum_client", "REJECTED");
props.setProperty("javax.net.ssl.keyStore", "C:\\APP\\ORACLE\\product\\11.2.0\\client_1\\ewallet.p12");
props.setProperty("javax.net.ssl.keyStoreType","PKCS12");
props.setProperty("javax.net.ssl.keyStorePassword","Password1");
try {
connection = DriverManager.getConnection(oracleURL, props);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
System.out.println("Error code: " + e.getErrorCode());
System.out.println("SQL State: " + e.getSQLState());
e.printStackTrace();
return;
}
我能够编译程序并使用以下命令运行它-
I'm able to compile the program and also run it using the following -
java -cp z:\jdk1.7.0_13\bin\ojdbc14.jar;z:\jdk1.7.0_13\bin OracleConnCheck
其中:
- z:\ jdk1.7.0_13 \ bin \ ojdbc14.jar-ojdbc14.jar的位置
- z:\ jdk1.7.0_13 \ bin-Java类路径
- OracleConnCheck-Java类
但是我总是遇到伴随NL异常或SO异常的IO错误.我在这里签出了Oracle以及与同一异常相关的几篇文章,但没有一篇解决我的确切问题.有人可以帮忙吗?谢谢!
But I always encounter IO error accompanied with NL exception or SO exception. I checked out the Oracle and few articles related to the same exception in here but none addressed my exact problem. Could someone help? Thanks!
添加堆栈跟踪-
Oracle JDBC Driver Registered!
Connection Failed! Check output console
Error code: 17002
SQL State: null
java.sql.SQLException: Io exception: NL Exception was generated
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:257)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:389)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:190)
at OracleConnCheck.establishConnection(OracleConnCheck.java:51)
at OracleConnCheck.main(OracleConnCheck.java:72)
------- The End -------
已生成NL异常",表明连接字符串中存在格式错误.在您的情况下,您在DESCRIPTION
之后缺少=
.
"NL Exception was generated" indicates that there is a format error in the connection string. In your case you are missing =
after DESCRIPTION
.