0

I am using Apache Derby and have the following code:

DBConnectionFactory.java

package edu.unsw.comp9321.jdbc;

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;

import edu.unsw.comp9321.common.DataSourceException;
import edu.unsw.comp9321.common.ServiceLocatorException;

 // This class looks up the database via JNDI and returns a connection to the DAO Implementation class


public class DBConnectionFactory {

static Logger logger = Logger.getLogger(DBConnectionFactory.class.getName());
private static DBConnectionFactory factory = null;
private DataSource ds = null;
private InitialContext ctx;
private Context subctx;

private DBConnectionFactory() throws ServiceLocatorException{
    try{
        ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("java:comp/env/jdbc/cs9321");
        logger.info("Database found:"+ds.toString());
    }catch(NamingException e){
        logger.severe("Cannot find context, throwing exception"+e.getMessage());
        e.printStackTrace();
        throw new ServiceLocatorException();
    }
}

public DataSource getDataSource(){
    return ds;
}

public static Connection getConnection() throws ServiceLocatorException, SQLException{

    if(factory==null)
        factory = new DBConnectionFactory();
    Connection conn = factory.getDataSource().getConnection();

    return conn;
}

}

The error seems to occur here:

ds = (DataSource) ctx.lookup("java:comp/env/jdbc/cs9321");

I read that this may occur if derbyclient.jar is not in the build path. However, I have already added this jar to the build path.

Would anyone have any suggestions as to what I can do?

Thanks.

  • 1
    Please check if the solution in this link helps http://stackoverflow.com/questions/8602390/unable-to-access-datasource-remotely-through-jboss – Hirak Apr 29 '14 at 11:14

1 Answers1

2

You're missing a couple of jars, add this two and everything should be ok:

commons-dbcp-1.4.jar commons-pool-1.6.jar

Hope that helps!

Cheers