I found that example to be wrong as well. This worked for me.
ic.createSubcontext("java:comp");
ic.createSubcontext("java:comp/env");
ic.createSubcontext("java:comp/env/jdbc");
final PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setUrl("jdbc:postgresql://localhost:5432/mydb");
ds.setUser("postgres");
ds.setPassword("pg");
ic.bind("java:comp/env/jdbc/mydb", ds);
The difference you'll note is that the '/' after 'java:' in each of the contexts is wrong and shouldn't be there.