如何使用Java连接到Amazon RDS实例时解析ORA-12514

时间:2013-09-11 14:57:23

标签: java oracle amazon-rds

我正在尝试使用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连接而没有问题。

对于那些没有使用过亚马逊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

2 个答案:

答案 0 :(得分:1)

JDBC连接字符串不正确。

字符串应为:jdbc:oracle:thin:[user/password]@//<host><:port>/<service>

更改连接字符串解决了问题。

答案 1 :(得分:0)

如果您使用的是AWS RDS 之类的第三方服务,请尝试此操作

oracledb.getConnection(
  {
    user          : "hr",
    password      : mypw,  // mypw contains the hr schema password
    connectString : "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=mymachine.example.com)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))"
  }