oracle.net.ns.NetException: Listener refused the connection with the following error: ORA-12505, TNS

oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

 

问题描述:jdbc连接Oracle12 可插拔数据路PDB链接出错

解决方案:

  首先查看自己的oracle安装目录下的 D:\app\Administrator\virtual\product\12.2.0\dbhome_1\network\admin(自己的oracle安装目录)

这个文件夹下的  tnsnames.ora 两个配置文件

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = Xiong-Chun)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )
上面这个是系统本来就生成的orcl
下面这个就是自己创建的那个  可插拔数据库Pdb
把上面的复制一份改一改,就完事了

ORCLPDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = Xiong-Chun)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLPDB)#需要改的地方
    )
  )

然后就是java代码中的问题

首先用的Jar包是ojdbc7或者ojdbc8总之6是不能用的了

public class OracleTest {
    @Test
    public  void test001() throws ClassNotFoundException, SQLException {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //下面这个千万千万千万要注意,1521/ORCLPDB是斜杠!!!不是:冒号!!!
        String url = "jdbc:oracle:thin:@localhost:1521/ORCLPDB";
        String username = "数据库登陆用户名";
        String psw = "数据库登陆密码";
        Connection conn = DriverManager.getConnection(url,username,psw);

        String sql = "select * from emp";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getObject(1));
            System.out.println(resultSet.getObject(2));
            System.out.println(resultSet.getObject(3));
        }
        resultSet.close();
        ps.close();
        conn.close();

    }

 

你可能感兴趣的:(Oracle12_2,jdk)