SQL Developer: “network adapter could not establish the connection”


In order to become re-acquainted with Oracle database development I downloaded and installed Oracle Database 11g and Oracle Developer.  During the install of Oracle Database 11g on Windows 8, I opted not to create a database.  After Oracle Database 11g was installed, I created my database using the Database Configuration Assistant (DBCC).  But when I tried to connect to my database using Oracle Developer, testing the connection failed each time; the reason being that the “network adapter could not establish the connection.”

I managed to solve this issue by using the Net Configuration Assistant to set up a listener for TCP, TCPS, and NPS.  Then via the control panel, I had to go to Device Manager, right-click on the root node of the tree and select Add legacy hardware followed by Network Adapters -> Microsoft -> Microsoft KM-Test Loopback Adapter (older versions of Windows: Microsoft Loopback Adapter).

After performing the above configuration, I was able to connect to my Oracle database without a problem.

Advertisements

OracleXE JDBC Example


I have Oracle XE 11g installed on my computer with the associated JDeveloper IDE. To develop this example, I used my preferred IDE which is NetBeans 7.0.1.

To begin with, start a new Java Application project.  I have called mine HumanResources as I am using the sample HR schema of OracleXE. My package is com.training. Add the following JAR files to your libraries folder: ojdl.jar, ojdbc6dms.jar, and dms.jar. These files are located at C:\Oracle\Middleware\oracle_common\modules on my computer.

The code below is for the file HumanResources.java.

package oraclexe;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class HumanResources 
{
    public static void main(String[] args) 
    {
        // 1. Load the database driver.
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }
        catch (ClassNotFoundException e)
        {
            System.err.println(e);
            System.exit(-1);
        }
        try
        {
            // 2. Open a connection to the database.
            Connection connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE"
                    , "HR"
                    , "YourPassword"
            );
            // 3. Build the query.
            String query = "SELECT * FROM HR.COUNTRIES";
            // 4. Execute the query.
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);
            // 5. Show the query results.
            while(rs.next())
            {
                System.out.println("Country: " + rs.getString(1)
                        + " - " + rs.getString(2));
            }
            // 6. Close the connection.
            connection.close();
        }
        catch(java.sql.SQLException e)
        {
            System.err.println(e);
            System.exit(-1);
        }
        catch(Exception e)
        {
            System.err.println(e);
            System.exit(-1);   
        }
    }
}

Compile and run the project. In the Output -> Debugger Console window, you should see the following output:

run:
Country: AR - Argentina
Country: AU - Australia
Country: BE - Belgium
Country: BR - Brazil
Country: CA - Canada
Country: CH - Switzerland
Country: CN - China
Country: DE - Germany
Country: DK - Denmark
Country: EG - Egypt
Country: FR - France
Country: HK - HongKong
Country: IL - Israel
Country: IN - India
Country: IT - Italy
Country: JP - Japan
Country: KW - Kuwait
Country: MX - Mexico
Country: NG - Nigeria
Country: NL - Netherlands
Country: SG - Singapore
Country: UK - United Kingdom
Country: US - United States of America
Country: ZM - Zambia
Country: ZW - Zimbabwe
BUILD SUCCESSFUL (total time: 4 seconds)

If you encounter problems, make sure you have included the necessary JAR files in your project. Secondly make sure that the database services are running. Thirdly check your connection string. And finally check your database driver class name is spelt correctly with the correct capitalisation.

Oracle XE Logon


If you forget your Oracle XE password and cannot login, open up a command prompt or shell and type the following:

Linux Users

sudo su – oracle
sqlplus “/ as sysdba”
alter user <username> identified by <password>

Windows Users

sqlplus “/ as sysdba”
alter user <username> identified by <password>

Original information taken from: https://forums.oracle.com/forums/thread.jspa?threadID=2153336.