我正在尝试在eclipse中使用java连接到oracle数据库12c。这是一个不断被抓住的例外
Exception in thread "main" java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:743)
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBHelper.makeConnection(DBHelper.java:31)
at Main.main(Main.java:37)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:470)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:506)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:595)
at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:246)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:264)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
... 7 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:161)
at oracle.net.nt.ConnOption.connect(ConnOption.java:159)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:428)
... 13 more
以下是我所做的
main.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main {
public static void main(String[] arg) throws SQLException{
DBHelper dbconn = new DBHelper("system", "reborn78");
Connection conn = dbconn.makeConnection();
ResultSet result = dbconn.runQuery(conn, "SELECT A,B FROM NEWS");
System.out.println(result);
}
}
DBHelper.java
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBHelper {
private String userName;
private String passWord;
private final String url = "jdbc:oracle:thin:@localhost:1521/ORCL";
public DBHelper(String userName, String passWord) throws SQLException{
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver( myDriver );
this.userName = userName;
this.passWord = passWord;
}
public Connection makeConnection() throws SQLException{
Properties props = new Properties();
props.setProperty("user", this.userName);
props.setProperty("password", this.passWord);
return DriverManager.getConnection(this.url,props);
}
public ResultSet runQuery(Connection conn, String sqlQuery) throws SQLException{
PreparedStatement preStatement;
ResultSet result;
Statement statement = conn.createStatement();
preStatement = conn.prepareStatement(sqlQuery);
statement.execute(sqlQuery);
result = preStatement.executeQuery();
return result;
}
}
下面是我的listener.ora文件
# listener.ora Network Configuration File: E:appUsamaproduct12.1.0dbhome_1NETWORKADMINlistener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = E:appUsamaproduct12.1.0dbhome_1)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:E:appUsamaproduct12.1.0dbhome_1binoraclr12.dll")
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
下面是我的tnsnames.ora文件
# tnsnames.ora Network Configuration File: E:appUsamaproduct12.1.0dbhome_1NETWORKADMINtnsnames.ora
# Generated by Oracle configuration tools.
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
(CONNECT_DATA =
(SID = CLRExtProc)
(PRESENTATION = RO)
)
)
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
SQL使用sqlplus连接,侦听器运行并连接到SQL开发人员!下面是图片链接https://www.dropbox.com/sh/vbv0gkfcz2g59t5/AACtIVR1-bIen99DNN4HDsWaa?dl=0
我浪费了三天的时间来纠正这个问题,昨天我可以让它正常工作,但今天当我重新启动系统时,同样的错误又回来了。我已经尝试了所有可用的东西,但今天似乎都不起作用。提前感谢
已解决。
我可以通过使用数据库配置助手重新配置数据库,然后通过网络配置助手配置网络服务来运行它。
事实证明,据我所知,这是启动通过侦听器连接所需服务的唯一方法。每次重新启动系统时,我都必须手动完成并在网络配置助手中测试网络连接。
感谢您的支持。