我试图通过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包含"。"和" _"和我的host_name包含"。"
请在这里帮忙!
答案 0 :(得分:0)
//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);
答案 1 :(得分:0)
以下是从oracle文档创建数据源对象的正确方法:
//指定数据源对象
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@//myhost:1521/orcl");
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
我希望你正确使用它