I have a really simple piece of JAVA code, where I try to connect from JAVA to my Oracle DB.
Everything works under Windows, but when I try to run it on Ubuntu, I have got an error.
I read a lot and I have tried lot of solutions. Here's my code:
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleJDBC {
private static String driver = "oracle.jdbc.driver.OracleDriver";
private static String password = "*****";
private static String dbname = "XE";
private static String username = "userir";
public Connection getConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:" + dbname, username,
password);
} catch (SQLException e) {
System.out.println("Connection Failed");
e.printStackTrace();
return null;
}
return connection;
}
public void closeConnection(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
when I run it, I receive an error:
Connection Failed java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458) at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:546) at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:236) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521) at java.sql.DriverManager.getConnection(DriverManager.java:571) at java.sql.DriverManager.getConnection(DriverManager.java:215) at utils.OracleJDBC.getConnection(OracleJDBC.java:26) at utils.TestMain.main(TestMain.java:6) Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392) at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434) at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687) at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247) at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320) ... 8 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:150) at oracle.net.nt.ConnOption.connect(ConnOption.java:133) at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370) ... 13 more
I logged in: sqlplus sys as sysdba
I started db: startup
I logged into Oracle as userir. XE exists.
I have no idea, what I did wrong...
Thank you in advance for your hints!