简体   繁体   English

来自eclipse的JDBC连接不起作用,但是在Oracle SQL Developer中起作用

[英]JDBC connection from eclipse not working, but working in Oracle SQL developer

I'm trying to connect into DB via JDBC 我正在尝试通过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);

Got the below exception 得到了以下异常

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:

But at the same time, I could connect via Oracle SQL developer. 但是同时,我可以通过Oracle SQL开发人员进行连接。

Note- My service_name consists of "." 注意:我的service_name由“。”组成。 and "_" and My host_name contains "." 和“ _”,而我的host_name包含“。”

Please help here! 请在这里帮助!

//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);

Below is the right way to create data source object from oracle documentation: 以下是从oracle文档创建数据源对象的正确方法:

//specify the datasource object //指定数据源对象

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

I hope you are using it right way 希望您使用正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM