Hi there,

I have created a Web project using Eclipse 3.4. However, it can't resolve 
the class reference to the current directory.
Here is the project layout and description of the problem:

>DBTest
 >src
   > command
     CommandExecutor.java
        ...
        ...
     DatabaseCommand.java
        ...


Double-clicks on the file CommandExecutor.java, it display the following 
content:

package command;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

/**
 * Gets and caches data sources, gets connections
 * Implements Singleton pattern
 */

public class CommandExecutor {

    private static CommandExecutor myOnlyInstance = null;
    private DataSource ds = null;
    
    // accessor method for the singleton
    public static CommandExecutor getInstance() throws NamingException {
        if (myOnlyInstance == null) {
            myOnlyInstance = new CommandExecutor();
        }
        return myOnlyInstance;
    }
    
    // Constructor for the singleton
    private CommandExecutor() throws NamingException {
        getDataSource();
    }
    
    // get the data source from initial context
    public DataSource getDataSource() throws NamingException {
        
        if (ds == null) {
            
            InitialContext ctx = new InitialContext();
            Context envCtx = (Context) 
ctx.lookup("java:comp/env");
            ds = (DataSource) envCtx.lookup("jdbc/TestDB");
            
        }
        
        return ds;
    }
    
    // get the SQL connection from the database
    public Connection getConnection() throws NamingException, 
SQLException {
        return getDataSource().getConnection();
        
    }
    
    // execute particular database command
    public Object executeDatabaseCommand(DatabaseCommand c) throws 
Exception {
        
        Connection conn = null;
        try {
            conn = getConnection();
            Object o = c.executeDatabaseOperation(conn);
            return o;
        } catch (SQLException e) {
            throw e;
        } catch (NamingException ne) {
            throw ne;
        } finally {
            if (conn != null) conn.close();
        }
    }
}

The DatabaseCommand has error. Move the cursor onto the name of 
DatabaseCommand, an error message popup:

DatabaseCommand cannot be resolved as a type
3 quick fixes available:
  . Add type parameter "DatabaseCommand" to "CommandExecutor"
  . Add type parameter "DatabaseCommand" to 
"executeDatabaseCommand(DatabaseCommand)"
  . Fix Project Setup...

I think I need to choose the third option "Fix Project Setup".

Now, from Java Build Path, under the Source menu tab, it has the the 
following layout:
DBTest/src
   Included: (All)
   Excluded: (None)
   Native librarylocation (None)

Look back the very beginning of this post, you can see the class file 
DatabaseCommand.java file is already defined in the same directory as as 
CommandExecutor.java defined.

How to tell Eclipse compiler that the DatabaseCommand class is in the same 
directory as the CommandExecutor.java?

Thanks
Sam

Reply via email to