Hi I have a database connection class. This class is part of a dynamic web project I am running the web project on tomcat server locally and I am also trying to connect to a derby server. I have imported all the external files for derby and tomcat, both servers are running. When I run my application at this line I keep failing
ds = (DataSource) ctx.lookup("java:/comp/env");
These are the packages I have imported
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
This is a snippet from the code
public DatabaseConnection() throws ServiceLocatorException{
try{
ctx = new InitialContext();
System.out.println("We are trying to connect to the Derby server: ");
ds = (DataSource) ctx.lookup("java:/comp/env");
logger.info("Database found:"+ds.toString());
}catch(NamingException e){
logger.severe("Cannot find context, throwing exception"+e.getMessage());
e.printStackTrace();
throw new ServiceLocatorException();
}
}
The exception I am getting is this
java.lang.ClassCastException: org.apache.naming.NamingContext cannot be cast to javax.sql.DataSource
I am not sure what to do.