The most popular object to get from JNDI is a object of type javax.sql.DataSource, allowing the developer to obtain JDBC connections to databases. Simple-JNDI supports this out of the box.
There are four mandatory parameters for a DataSource in Simple-JNDI, and four optional parameters (see next section). The mandatory parameters are url, driver, user, password. The following shows an example of a DataSource that will be available under the lookup key application1/ds/TestDS.
application1/ds.properties
TestDS.type=javax.sql.DataSource
TestDS.driver=org.gjt.mm.mysql.Driver
TestDS.url=jdbc:mysql://localhost/testdb
TestDS.user=testuser
TestDS.password=testing
The code to obtain it would be:
InitialContext ctxt = new InitialContext();
DataSource ds = (DataSource) ctxt.lookup("application1/ds/TestDS");
This example uses a delimiter of '/', which must be set with the org.osjava.sj.delimiter property.
Partager