【发布时间】:2011-07-24 08:18:03
【问题描述】:
我目前正在尝试连接到我当前计算机上的数据库。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] argv) throws Exception {
Connection connection = null;
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String portNumber = "1521";
String sid = "xe";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "scott";
String password = "tiger";
connection = DriverManager.getConnection(url, username, password);
System.out.println("Success");
} catch (ClassNotFoundException e) {
System.out.println("Class Not Found Error");
}
}
}
我不断收到此错误,我不知道为什么...
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:xe
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:496)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:465)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:21)
在我的服务器中,我使用了命令(以 sys 身份登录) SQL> 从 v$thread 中选择实例; (它返回) 实例--> xe
我做错了什么?
谢谢!
附:我也试过 127.0.0.1 而不是 localhost
【问题讨论】:
-
lsnrctl status与拥有数据库的用户一起运行时会显示什么? -
要么是你的连接字符串,要么是数据库配置文件。
-
您使用的是 Oracle Express Edition (XE) 还是 Standard/Enterprise 版?
-
@Vineet:SID
xe表明他正在使用 Express Edition。 -
@Luke,没必要。他正在运行一个示例,按照他之前的问题,所以
xeSID 可能来自他复制的示例。
标签: java database oracle exception sqlexception