EA-Exception Network Adapter macht probleme

Status
Nicht offen für weitere Antworten.

drldoom

Mitglied
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:

Code:
/*
 * 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

Top Contributor
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:

Code:
    /**
     * 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 + ")";
        }
    }
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
T Webserviceaufruf verursacht eine Exception Netzwerkprogrammierung 3
R Socket FATAL EXCEPTION MAIN bei Socket based client/server app Netzwerkprogrammierung 2
D Exception Handling bei In/Outputsockets in eigenen Threads Netzwerkprogrammierung 1
A Cast Exception bei einfachem RMI Beispiel Netzwerkprogrammierung 3
M Socket Exception tritt auf - weiß nicht weiter Netzwerkprogrammierung 3
K Socket Exception Connection reset Netzwerkprogrammierung 9
C ObjectInputReader wirft beim zweiten Aufruf eine Exception Netzwerkprogrammierung 3
M Socket TCP keep alive Exception wird nicht ausgelöst Netzwerkprogrammierung 11
G Exception: Connection reset by peer: socket write error Netzwerkprogrammierung 2
A Socket Socket Verbindung unterbrochen --> keine Exception Netzwerkprogrammierung 7
H Socket Closed Exception verhindern Netzwerkprogrammierung 3
M RMI unmarshaling exception ??? Netzwerkprogrammierung 2
D Socket Streams schliessen .. Exception gewollt? Netzwerkprogrammierung 4
K Socket Socket Exception Netzwerkprogrammierung 3
eQuest RMI Unserializable Exception Netzwerkprogrammierung 4
F Bekomme NoSuchElement Exception Netzwerkprogrammierung 5
S RMI Exception Netzwerkprogrammierung 2
T rmi ssl zu große Objekte übergeben -> Exception Netzwerkprogrammierung 10
clupus Exception beim Schließen eines Sockets Netzwerkprogrammierung 6
G Exception javax.naming.CommunicationException Netzwerkprogrammierung 16
G Nullpointer Exception - Multithreading Netzwerkprogrammierung 25
G XML-RPC -> Exception $Proxy0-Unknown Source-No such handl Netzwerkprogrammierung 8
T Exception serialisieren? Netzwerkprogrammierung 5
K öffnen des socket schlägt fehl -> ABER: keine exception . Netzwerkprogrammierung 2
M ois nicht null, aber ois.getObject liefer exception Netzwerkprogrammierung 3
R ObjectOutput- / ObjectInputStream Exception? Netzwerkprogrammierung 2
F Java Mail . Exception java.lang.NoClassDefFoundError Netzwerkprogrammierung 2
M Exception in thread "main" java.lang.NoClassDefFou Netzwerkprogrammierung 2
J JavaMail Exception bei senden an anderen Server. Netzwerkprogrammierung 8
M schreiben auf geschlossenen Socket ohne Exception Netzwerkprogrammierung 6
R LINUX: getHostAddress() und getHostName() werfen Exception Netzwerkprogrammierung 6
8 PrintWriter Exception Netzwerkprogrammierung 3
D socket exception + timing probleme Netzwerkprogrammierung 2
A Exception bei Cookie lesen Netzwerkprogrammierung 2
M Network Framework Netzwerkprogrammierung 2
A ? Home-Network, Server/Client-Einrichtung Netzwerkprogrammierung 4
M Network Broadcasting Netzwerkprogrammierung 9
Kr0e Java Game Network Engine Netzwerkprogrammierung 22
P IP-Adresse von Drahtlos-LAN-Adapter WLAN Netzwerkprogrammierung 1
R viele HttpRequests - Adapter überlastet? Netzwerkprogrammierung 11

Ähnliche Java Themen

Neue Themen


Oben