我正在尝试使用 JDBC 连接到我的 RDS 实例。当我尝试通过代码时,我得到:
Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
错误信息。我可以使用相同的凭据、SID 等毫无问题地从 SQL Developer 连接。
对于那些没有使用过 Amazon 的 RDS 的人来说,它是在他们的 AWS 服务上提供的托管数据库。因为它是托管的,所以我无法访问与本地安装相同的设施,因此到目前为止我还没有弄清楚是否或如何访问该tnsnames.ora
文件以获取侦听器信息。
我的代码如下:
public Connection connectToDatabase() {
String url = "jdbc:oracle:thin:@//blahblahyadayada-eastnorthest-1.rds.amazonaws.com:1521/ORCL,username,password";
try {
return DriverManager.getConnection(url); // <-- error is here
} catch (SQLException e) {
e.printStackTrace();
try {
return DriverManager.getConnection(url);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return null;
}
public static void main(String[] args) {
Connection conn = db.connectToDatabase();
try {
ResultSet results = db.executeQuery("select 1;", conn);
if (results.next()) {
LOGGER.info("Results of query: {}",
String.valueOf(results.getInt(1)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
....
编辑
通过以下语句找到以下监听器信息:
show parameter db_name;
show parameter remote_listener;
show parameter listener_networks
show parameter local_listener;
NAME TYPE VALUE
-------------------------------------------------- ----------- ----------------------------------------------------------------------------------------------------
db_name string ORCL
NAME TYPE VALUE
-------------------------------------------------- ----------- ----------------------------------------------------------------------------------------------------
remote_listener string
NAME TYPE VALUE
-------------------------------------------------- ----------- ----------------------------------------------------------------------------------------------------
listener_networks string
NAME TYPE VALUE
-------------------------------------------------- ----------- ----------------------------------------------------------------------------------------------------
local_listener string (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))
侦听器的 RDS 参数不可修改并显示为动态。
select * from DBA_SERVICES;
显示:
SERVICE_ID NAME NAME_HASH NETWORK_NAME CREATION_DATE CREATION_DATE_HASH FAILOVER_METHOD FAILOVER_TYPE FAILOVER_RETRIES FAILOVER_DELAY MIN_CARDINALITY MAX_CARDINALITY GOAL DTP ENABLED AQ_HA_NOTIFICATIONS CLB_GOAL EDITION
---------- ---------------------------------------------------------------- ---------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------- ------------------ ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------- -------------- --------------- --------------- ------------ --- ------- ------------------- -------- ------------------------------
1 SYS$BACKGROUND 165959219 13-05-01 0 NONE N NO NO SHORT
2 SYS$USERS 3427055676 13-05-01 0 NONE N NO NO SHORT
3 ORCL_A 3453147048 ORCL_A
我修改了我的连接字符串以尝试ORCL_A
同样的错误。