java - 到 Oracle Cloud ATP 19c 的 JDBC 连接
问题描述
我正在使用 Java 8 并尝试从 NetBeans 8.2 测试与新 Oracle 云数据库的 JDBC 连接
package project;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;
public class UCPSample {
//final static String DB_URL="jdbc:oracle:thin:@myhost:1521/orclservicename";
// Use the TNS Alias name along with the TNS_ADMIN - For ATP and ADW
final static String DB_URL="jdbc:oracle:thin:@almubarak_medium?TNS_ADMIN=/src/Wallet_almubarak/";
final static String DB_USER = "Admin";
final static String DB_PASSWORD = "mypassword";
final static String CONN_FACTORY_CLASS_NAME="oracle.jdbc.pool.OracleDataSource";
/*
* The sample demonstrates UCP as client side connection pool.
*/
public static void main(String args[]) throws Exception {
// Get the PoolDataSource for UCP
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
// Set the connection factory first before all other properties
pds.setConnectionFactoryClassName(CONN_FACTORY_CLASS_NAME);
pds.setURL(DB_URL);
pds.setUser(DB_USER);
pds.setPassword(DB_PASSWORD);
pds.setConnectionPoolName("JDBC_UCP_POOL");
// Default is 0. Set the initial number of connections to be created
// when UCP is started.
pds.setInitialPoolSize(5);
// Default is 0. Set the minimum number of connections
// that is maintained by UCP at runtime.
pds.setMinPoolSize(5);
// Default is Integer.MAX_VALUE (2147483647). Set the maximum number of
// connections allowed on the connection pool.
pds.setMaxPoolSize(20);
// Default is 30secs. Set the frequency in seconds to enforce the timeout
// properties. Applies to inactiveConnectionTimeout(int secs),
// AbandonedConnectionTimeout(secs)& TimeToLiveConnectionTimeout(int secs).
// Range of valid values is 0 to Integer.MAX_VALUE. .
pds.setTimeoutCheckInterval(5);
// Default is 0. Set the maximum time, in seconds, that a
// connection remains available in the connection pool.
pds.setInactiveConnectionTimeout(10);
// Get the database connection from UCP.
try (Connection conn = pds.getConnection()) {
System.out.println("Available connections after checkout: "
+ pds.getAvailableConnectionsCount());
System.out.println("Borrowed connections after checkout: "
+ pds.getBorrowedConnectionsCount());
// Perform a database operation
doSQLWork(conn);
}
catch (SQLException e) {
System.out.println("UCPSample - " + "SQLException occurred : "
+ e.getMessage());
}
System.out.println("Available connections after checkin: "
+ pds.getAvailableConnectionsCount());
System.out.println("Borrowed connections after checkin: "
+ pds.getBorrowedConnectionsCount());
}
/*
* Creates an EMP table and does an insert, update and select operations on
* the new table created.
*/
public static void doSQLWork(Connection conn) {
try {
conn.setAutoCommit(false);
// Prepare a statement to execute the SQL Queries.
Statement statement = conn.createStatement();
// Create table EMP
statement.executeUpdate("create table EMP(EMPLOYEEID NUMBER,"
+ "EMPLOYEENAME VARCHAR2 (20))");
System.out.println("New table EMP is created");
// Insert some records into the table EMP
statement.executeUpdate("insert into EMP values(1, 'Jennifer Jones')");
statement.executeUpdate("insert into EMP values(2, 'Alex Debouir')");
System.out.println("Two records are inserted.");
// Update a record on EMP table.
statement.executeUpdate("update EMP set EMPLOYEENAME='Alex Deborie'"
+ " where EMPLOYEEID=2");
System.out.println("One record is updated.");
// Verify the table EMP
ResultSet resultSet = statement.executeQuery("select * from EMP");
System.out.println("\nNew table EMP contains:");
System.out.println("EMPLOYEEID" + " " + "EMPLOYEENAME");
System.out.println("--------------------------");
while (resultSet.next()) {
System.out.println(resultSet.getInt(1) + " " + resultSet.getString(2));
}
System.out.println("\nSuccessfully tested a connection from UCP");
}
catch (SQLException e) {
System.out.println("UCPSample - "
+ "doSQLWork()- SQLException occurred : " + e.getMessage());
}
finally {
// Clean-up after everything
try (Statement statement = conn.createStatement()) {
statement.execute("drop table EMP");
}
catch (SQLException e) {
System.out.println("UCPSample - "
+ "doSQLWork()- SQLException occurred : " + e.getMessage());
}
}
}
}
我在这里使用文档
- 将我的钱包文件夹下载到 src 文件夹
- 下载并安装 JCE Unlimited Strength Jurisdiction Policy Files。
- 下载所有 ojdbc8-full jars 文件并将其添加到我的库中
我总是收到错误消息
UCPSample - SQLException occurred : Unable to start the Universal Connection Pool: oracle.ucp.UniversalConnectionPoolException: Cannot get Connection from Datasource: java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
解决方案
当我将 TNS_ADMIN 分隔为 CONNECTION_PROPERTY 不在连接字符串中时,它终于可以工作了
Properties info = new Properties();
info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);
info.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "20");
info.put(OracleConnection.CONNECTION_PROPERTY_TNS_ADMIN,"c:\\Wallet_almubarak");
推荐阅读
- reactjs - 尝试过滤我在 ReactJs 中使用 fetch API 获取的数据
- rust - 错误[E0412]:在此范围内找不到类型“字符串”
- wso2 - 尝试发布 API WSO2 2.6.0 时出错
- angular - Angular 2:从一个函数将值设置为变量,然后在同一类的另一个函数中获取它
- electron - 在 Windows 10 上滚动分页页面时,electron.js 中出现“GPU 进程意外退出:exit_code=34”错误
- javascript - ajax请求后如何在动态html中注册onclick函数?
- perl - Perl OOP 看不到新方法
- pytorch - 张量元组
- elixir - Escript 文件和 .gitignore
- python - 用于验证 json 文件内容的函数的 Pytest