【问题标题】:JDBC connection from eclipse not working, but working in Oracle SQL developer来自 Eclipse 的 JDBC 连接不起作用,但在 Oracle SQL 开发人员中工作
【发布时间】:2017-03-20 06:11:06
【问题描述】:

我正在尝试通过 JDBC 连接到数据库

Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@host_name:port:service_name"; //Changed the host_name, port and service_name for confidentiality
String username = "user_name"; // Changed the user_name
String password = "my_password"; //Changed the password  
Connection conn = DriverManager.getConnection(url, username, password);

得到以下异常

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:

但同时,我可以通过 Oracle SQL 开发人员进行连接。

注意-我的 service_name 由“.”组成和“_”,我的主机名包含“。”

请在这里帮忙!

【问题讨论】:

    标签: sql eclipse jdbc oracle-sqldeveloper


    【解决方案1】:
    //Step: 1
    Load Driver Class
    ex- Class.forName("oracle.jdbc.driver.OracleDriver");
    
    //Step: 2
    String url="jdbc:oracle:thin:@ipaddress:portNo of oracle :database name";
    ex- String url="jdbc:oracle:thin:@localhost:1521:XE";
    
    //Step: 3
    User Name
    String username = "user_name";
    ex- String username = "system";
    
    //Step: 3
    Password
    String password = "oracle_db_Password";
    ex- String password = "sys123";
    
    //Step:4 
    Connection con=DriverManager.getConnection(url,username,password);
    

    【讨论】:

      【解决方案2】:

      以下是从 oracle 文档创建数据源对象的正确方法:

      //指定数据源对象

          OracleDataSource ods = new OracleDataSource();
          ods.setURL("jdbc:oracle:thin:@//myhost:1521/orcl");
          ods.setUser("scott");
          ods.setPassword("tiger");
          Connection conn = ods.getConnection();
      

      希望你用对了

      【讨论】:

      • 这里的“信息”对象是什么?
      • 如果这个答案有指向该文档的链接会更好。
      猜你喜欢
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多