我正试图在我的春季应用程序中配置甲骨文钱包。以下是错误:
原因: org.springframework.jdbc.CannotGetJdbcConnectionException:无法获得JDBC连接;嵌套异常为java.sql.SQLRecoverableException: IO错误:指定的未知主机]
我将所有配置文件(cwallet.sso、ewallet.p12)和jars() & setup tnsnames.ora放在下面是代码。
private DriverManagerDataSource getDriverManagerDataSource(final String driver,
final String url,
final String username,
final String password) {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
Properties props = new Properties();
props.put("oracle.net.wallet_location", pathToWallet);
dataSource.setConnectionProperties(props);
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
//dataSource.setUsername(username);
//dataSource.setPassword(password);
return dataSource;
}发布于 2018-09-19 05:41:47
马利克
示例程序没有完整的信息。
我认为在您的程序中,如果您替换下面的代码
Properties props = new Properties();
props.put("oracle.net.wallet_location", pathToWallet);使用
System.setProperty("oracle.net.tns_admin", "**tnsnames.ora_file_location**");
System.setProperty("oracle.net.wallet_location", "**Oracle_Wallet_Location**");它应该能解决你的问题。
我已经设置了我的甲骨文钱包通过以下步骤。
cd %ORACLE_HOME%\BIN
mkstore -wrl D:\oraclewallet\wallet -create (Provide wallet password)
mkstore -wrl D:\oraclewallet\wallet -createCredential **Tns_Entry_Name** **Schema_Name** **Password**%ORACLE_HOME%\NETWORK\admin\sqlnet.ora中的条目
WALLET_LOCATION =(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=**Oracle_Wallet_Location**)))
SQLNET.WALLET_OVERRIDE = TRUE在这里,您应该能够使用sqlplus连接到db。
sqlplus /@**Tns_Entry_Name**在运行java程序时,应该设置下面的java运行时参数。
-Doracle.net.tns_admin=**%ORACLE_HOME%\NETWORK\ADMIN**
-Doracle.net.wallet_location=**Oracle_Wallet_Location**否则,我们可以在运行时将其设置为
System.setProperty("oracle.net.tns_admin", "%ORACLE_HOME%\\NETWORK\\ADMIN");
System.setProperty("oracle.net.wallet_location", "Oracle_Wallet_Location");现在要获得db连接,您应该使用数据库url作为
jdbc:oracle:thin:/@**tns_entry_name**确保在类路径中有下面的罐子
要测试的示例代码
package datasourcetest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class DataSourceConnectionExample {
private static final String DRIVER_CLASS_NAME = "oracle.jdbc.driver.OracleDriver";
private static final String URL = "jdbc:oracle:thin:/@**tns_entry_name**";
public static void main(String[] args) throws SQLException {
System.setProperty("oracle.net.tns_admin", "**%ORACLE_HOME%\\NETWORK\\ADMIN**");
System.setProperty("oracle.net.wallet_location", "**Oracle_Wallet_Location**");
Connection conn = getDataSource().getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 'test' name FROM dual");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
public static DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DRIVER_CLASS_NAME);
dataSource.setUrl(URL);
return dataSource;
}
}https://stackoverflow.com/questions/47025432
复制相似问题