以下是连接数据库时的连接字符串:
public static void createConnection() throws Exception {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:brdqa", "STATICDATA_APP",
"password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
运行此代码时,我得到以下异常:
java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
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 oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.markit.scripts.ConnectMRD.createConnection(ConnectMRD.java:32)
at com.markit.scripts.ConnectMRD.main(ConnectMRD.java:54)
我谷歌很多,发现只有这个连接字符串存在问题。 字符串“brdqa”是我的数据库名称,“STATICDATA_APP”是我的用户名。我可以手动访问数据库。 请建议。
答案 0 :(得分:0)
此错误表示驱动程序无法将套接字连接到主机&#34; localhost&#34;在1521港口。
您可以仔细检查相同的连接字符串是否适用于sqlplus:
sqlplus "STATICDATA_APP/password@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=brdqa)))"
听众不支持&#34; localhost&#34;作为主机名。您是否尝试使用本地IP地址&#34; 127.0.0.1&#34;?
"jdbc:oracle:thin:@127.0.0.1:1521:brdqa"