This might be a good start:
HashMap<String,String> dsNames = new HashMap<String,String>();
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
System.setProperty(Context.PROVIDER_URL, "file:/home/user/tmp");
InitialContext ic = new InitialContext();
dsNames.put("yourDataSourceName", "jdbc/your_jndi_name");
// Construct BasicDataSource reference
Reference ref = new Reference("javax.sql.DataSource",
"org.apache.commons.dbcp.BasicDataSourceFactory", null);
ref.add(new StringRefAddr("driverClassName", "theDriver"));
ref.add(new StringRefAddr("url","theDBURL"));
ref.add(new StringRefAddr("username", "obvious"));
ref.add(new StringRefAddr("password", "obvious"));
ref.add(new StringRefAddr("maxActive", "10"));
ref.add(new StringRefAddr("maxIdle", "3"));
ref.add(new StringRefAddr("initialSize", "3"));
ref.add(new StringRefAddr("maxWait", "5"));
ic.rebind("jdbc/your_jndi_name", ref);
//And to get a reference to your data source anywhere else in your program:
InitialContext ic2 = new InitialContext();
DataSource myDS = (DataSource) ic2.lookup(dsNames.get(dsName));
Now as you can see, I'm using a pooled connection factory, but you can replace that to suit your needs. Also, the initial context factory is an FSContext which means the JNDI name will be looked up in a regular file system, an URL Context Factory must be available but I have never used it. Most classes used are in the javax.naming package.
Hope this helps.