Oracle中的数据库连接 [英] DB connection in oracle

查看:126
本文介绍了Oracle中的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Exception in thread "main" java.sql.SQLException: Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor  
The Connection descriptor used by the client was:localhost:1521:orcl

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:261)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)  
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)  
at oraenter code herecle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)  
at java.sql.DriverManager.getConnection(DriverManager.java:582)  
at java.sql.DriverManager.getConnection(DriverManager.java:185)  
at com.test.Date.main(Date.java:13)

这是我的代码

Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");  

我也查看了tnsnames.ora文件

ORCL =
(DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = orcl)
    )
)

我在类路径中添加了ojdbc14.jar,并且我正在使用oracle10g

I added ojdbc14.jar in my classpath and i am using oracle10g

请您帮我一下.

推荐答案

就像提到的,请检查您是否能够执行tnsping之类的tnsping,如果工作正常,请尝试从sqlplus或任何工具连接到数据库如TOADSQLDeveloper.如果发现任何错误,请发布错误,否则请使用以下内容连接到数据库并查看其工作方式.

Like mentioned check whether you are able to do a tnsping like tnsping orcl and if that works fine try to connect to your database from sqlplus or any tool like TOAD or SQLDeveloper. Post the errors if you are getting any, else use the following to connect to your database and see how it works.

    try {
                Class.forName("oracle.jdbc.OracleDriver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return;
            }
            Connection con = null;
            try {
                con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");
                   } catch (SQLException ex) {            
            ex.printStackTrace();            
        } finally {
            close(connection);
        }
return 

Update 1

Update 1

您可以尝试使用以下代码

Can you try with the following code

jdbc:oracle:thin:@//localhost:1521:orcl","system","tiger

如果这样不起作用,也请尝试

If that doesn't work try this as well

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=<host>)(PORT=<port>))(CONNECT_DATA=(SERVICE_NAME=<service>)))

更多信息此处

致谢

这篇关于Oracle中的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆