Context context = new InitialContext();
dataSource = (DataSource) context.lookup(\"java:comp/env/jdbc/multiDS\");
connection = dataSource.getConnection();
This is easily done with Simple-JNDI. Create a property file "jdbc/multiDS.properties" in your working directory to configure your datasource with these properties:
type=javax.sql.DataSource
driver=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost/testdb
user=testuser
password=testing
Then instantiate the context with
final Hashtable env = new Hashtable();
env.put("org.osjava.sj.root", "working_dir");
env.put("org.osjava.sj.jndi.shared", "true");
env.put("java.naming.factory.initial", "org.osjava.sj.SimpleContextFactory");
env.put("org.osjava.sj.delimiter", "/");
env.put("org.osjava.sj.space", "java:comp/env")
Context ctx = new InitialContext(env);
After that you can call
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/multiDS");
Find more info about Simple-JNDI here https://github.com/h-thurow/Simple-JNDI