Yoni Keren Yoni Keren - 1 year ago 74
MySQL Question

How do I run a web app code LOCALLY?

I've written a RESTful web api for my client (android app) to use.
I need to implement another application, a local desktop application, which accesses

classes which are already implemented for the server side.

I don't know how to.

Specifically, this class for a server side:

public Database(String TableName, String KeyName) throws SQLException, NamingException {
mTableName = TableName;
mKeyName = KeyName;

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

Right now there is an exception thrown (NoInitialContextException) which makes perfect sense because I am missing context.xml or specifically within it this resource is missing

<Resource name="jdbc/nasdaq_companies" auth="Container"
type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
username="*****" password="*****" maxActive="20" maxIdle="10" maxWait="-1"/>

Assuming that the server == localhost, How do I add a context resource to a local java application? (Or otherwise, how do i fix this problem?)

Answer Source

If you are talking about running something like a console app I solved it by setting up context manually by calling method in Main that looked something like this:

    public static void setupInitialContext() {
            try {
                NamingManager.setInitialContextFactoryBuilder(new InitialContextFactoryBuilder() {

                    public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException {
                        return new InitialContextFactory() {

                            public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
                                return new InitialContext() {

                                    private Hashtable<String, DataSource> dataSources = new Hashtable<>();

                                    public Object lookup(String name) throws NamingException {

                                        if (dataSources.isEmpty()) { //init datasources
                                            MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
                                            dataSources.put("jdbc/SomeDataSourceName", ds); 
    //lets add another datasource (optional)
                                            MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
                                            dataSources.put("jdbc/OtherDataSourceName", ds);    
//you can keep adding these as you need                                         

                                        if (dataSources.containsKey(name)) {
                                            return dataSources.get(name);

                                        return null; //or throw exception Java style :)


            catch (NamingException ne) {



You use this method by calling it first in your Main and after that getting those datasources should work

public static void main( String[] args ) {


        Context initContext = new InitialContext();        
        DataSource dataSource = (DataSource) initContext.lookup("jdbc/SomeDB");


PS: i think i found this way on StackOverflow as well but honestly can't remember where :(