Getting database connection for JDBC calls
It is not always feasible to use OfBizDelegator to get all the details that we need. What if we need to execute a complex query in the database via JDBC? In this recipe, we will see how we can retrieve the database connection that is defined in entityengine.xml
.
How to do it...
The database connection lookup is pretty simple if you are familiar with JDBC. Follow these quick steps to retrieve a connection:
Create a
javax.naming.InitialContext
object:InitialContext cxt = new InitialContext();
Retrieve the database information from the entity configurations using
EntityConfigUtil
:DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo ("defaultDS");
Here,
defaultDS
is the name of the data source defined inentityengine.xml
.Retrieve the
jndi-name
string from theDataSourceInfo
object:String jndiName = datasourceInfo.jndiJdbcElement.getAttribute ( "jndi-name" );
Use
jndi-name
to look up thejavax.sql.DataSource
object:DataSource ds = ( DataSource...