Saturday, March 24, 2012

Creating a database conection object in Coldfusion

In my last project ,I came across a situation where I had to create a database conection object in coldfusion and passed it to the java class which further make a database query using that conection object.

Here is the code to implement it..

//Creating an object of servicefactory class
theServiceFactory = createObject('java','coldfusion.server.ServiceFactory');

//Creating the connection object simply by passing the DSN name
con = theServiceFactory.getDataSourceService().getDataSource('DataSource Name').getConnection();

//Now passing the connection object as argument to the test.java class
testConnection = createObject('java', 'namespace to code').init(con );

Hope this will help you..Keep coding :)