【Oracle错误】ORA-12505,TNS:listener does not currently know of sid given in connect descriptor.
【错误描述】
ORA-12505,TNS:listener does not currently know of sid given in connect descriptor.
【出错原因】
老的url写法不适用于新的数据库。
之前程序url和dirver是这样写的:
public static Connection getConnection() { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; String user = "luna"; String pass = "1234"; conn = DriverManager.getConnection(url, user, pass); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }
这种写法在11g上没问题,但到了特定的12c数据库就出错,爆如题异常。
【处置方式】
改写driver和url为:
public class DbUtil { //-- 以下为连接Oracle数据库的四大参数 private static final String DRIVER = "oracle.jdbc.OracleDriver"; private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521/orcl"; private static final String USER = "luna"; private static final String PSWD = "1234"; public static Connection getConn() throws Exception{ Class.forName(DRIVER); Connection conn = DriverManager.getConnection(URL, USER, PSWD); return conn; } }
通过对比可以发现,driver少了一部分,url改:为/,对比表格如下:
适用数据库11g | 适用数据库12c | |
driver |
oracle.jdbc.driver.OracleDriver |
oracle.jdbc.OracleDriver |
url |
jdbc:oracle:thin:@127.0.0.1:1521:orcl |
jdbc:oracle:thin:@127.0.0.1:1521/orcl |
【注意】
不是所有的12c数据库都会不接受原有的连接方式。
END