# EA-Exception Network Adapter macht probleme



## drldoom (10. Aug 2006)

Hallo,

ich habe folgende fehlermeldung, wenn sich meine anwendung mit der datenbank von oracle verbinden will:

"java.sql.SQLException: The Network Adapter cannot establish the connection"

ich verstehe das nicht ganz.

das mein code für die verbindung:


```
/*
 * dbSelect.java
 *
 * 
 */

package NameEgal;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * Sends a query to a SQL database management system and gets the result as a 
 * result set.
 * The connection parameters are stored in a poperties file
 */
public class DbSelect {
    
    private Connection conn = null;
    private String sqlQuery;
    private String[] args;
    private PreparedStatement prepSql;
    private Statement stmt;
    private ResultSet rSet;
    private Properties properties;
    private String dbClass;
    private String dbUrl;
    private String dbUser;
    private String dbPasswort;
    
    
    /**
     * Sets the SQL statement from the given parameter.
     * @param sqlQuery SQL SELECT statement
     */
    public DbSelect(String sqlQuery, String[] args) {
        this.sqlQuery = sqlQuery;
        this.args = args;
    }
    public DbSelect(String sqlQuery) {
        this.sqlQuery = sqlQuery;
    }
    
    /**
     * This method initializes the database connection.
     * @return null if no exception is thrown,
     * otherwise the exception as a string
     */
    public String init() {
        properties = new Properties();
        try {
            properties.load(new FileInputStream(System.getProperty("user.dir") +
                    System.getProperty("file.separator") +
                    "database.properties"));
            dbClass = properties.getProperty("dbClass");
            dbUrl = properties.getProperty("dbUrl");
            dbUser = properties.getProperty("dbUser");
            dbPasswort = properties.getProperty("dbPassword");
        } catch (FileNotFoundException ex) {
            return ex.toString();
        } catch (IOException ex) {
            ex.printStackTrace();
            return ex.toString();
        }
        try {
            Class.forName(dbClass);
        } catch (ClassNotFoundException ex) {
            return ex.toString();
        }
        try {
            conn = DriverManager.getConnection(dbUrl, dbUser, dbPasswort);
            stmt = conn.createStatement();
            prepSql = conn.prepareStatement(sqlQuery);
            if (args != null) {
                for (int i=0; i < args.length; i++)
                {
                    if (args[i] != null) {
                        prepSql.setString(i+1, args[i]);
                    }
                }
            }
            rSet = prepSql.executeQuery();
            return null;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return ex.toString();
        }
    }
    
    /**
     * To get the result set from the SELECT query,
     * @return the ResultSet
     */
    public ResultSet getResultSet() {
        return rSet;
    }
    
    /**
     * Close all database elements: the statement, the result set 
     * and the connection.
     */
    public void closeAll() {
        try {
            stmt.close();
            rSet.close();
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    /**
     * Closes the result set.
     */
    public void closeRseultSet() {
        try {
            rSet.close();
            
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * Closes the database connection
     */
    public void closeConnection() {
        try {
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * Closes the statement.
     */
    public void closeStatement() {
        try {
            stmt.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
```

kan mir einer helfen? ich verstehe das nicht.
muss mich mit oracle verbinden, habe auch die entsprechende lib dazu


----------



## Murray (10. Aug 2006)

Die Details für die Verbindung werden ja aus der Datei database.properties gelesen; steht da vielleicht eine falsche Adresse drin?

Die Fehlerbehandlung ist hier allerdings auch etwas dürftig; wenn man die Exceptions schon in Strings verpackt, könnte man wenigstens noch ein paar Daten anfügen, z.B. so:


```
/**
     * This method initializes the database connection.
     * @return null if no exception is thrown,
     * otherwise the exception as a string
     */
    public String init() {
        properties = new Properties();
        String fn = System.getProperty("user.dir") +
                    System.getProperty("file.separator") +
                    "database.properties");
        try {

            properties.load(new FileInputStream(fn));
            dbClass = properties.getProperty("dbClass");
            dbUrl = properties.getProperty("dbUrl");
            dbUser = properties.getProperty("dbUser");
            dbPasswort = properties.getProperty("dbPassword");
        } catch (FileNotFoundException ex) {
            return ex.toString();
        } catch (IOException ex) {
            ex.printStackTrace();
            return ex.toString() + " (properties: " + fn + ")";
        }
        try {
            Class.forName(dbClass);
        } catch (ClassNotFoundException ex) {
            return ex.toString() + " (properties: " + fn + ")";
        }
        try {
            conn = DriverManager.getConnection(dbUrl, dbUser, dbPasswort);
            stmt = conn.createStatement();
            prepSql = conn.prepareStatement(sqlQuery);
            if (args != null) {
                for (int i=0; i < args.length; i++)
                {
                    if (args[i] != null) {
                        prepSql.setString(i+1, args[i]);
                    }
                }
            }
            rSet = prepSql.executeQuery();
            return null;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return ex.toString() + " (properties: " + fn + ", dbUrl:" + dbUrl + ")";
        }
    }
```


----------



## drldoom (10. Aug 2006)

danke für deine hilfe.
es lag am port..


----------

