Skip to main content
Participating Frequently
August 30, 2012
Question

Accessing Connection object without ServiceFactory

  • August 30, 2012
  • 1 reply
  • 1593 views

you can access a Connection object with :

theServiceFactory = createObject('java','coldfusion.server.ServiceFactory');

//Creating the connection object simply by passing the DSN name

con = theServiceFactory.getDataSourceService().getDataSource('datasourcename').getConnection();

My server admin doesnt want me to use the ServiceFactory. What are alternative methods of getting the Connection object?

PS I need to access the Connection object to perform an Oracle Batch Insert.

This topic has been closed for replies.

1 reply

Miguel-F
Inspiring
August 30, 2012

Why aren't you just using the datasource directly?

<cfquery name="queryname" dataSource="datasourcename">

     <!--- Oracle Batch insert code --->

</cfquery>

If you are stuck inside cfscript for some reason, try using the query function:  http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSe9cbe5cf462523a0693d5dae123bcd28f6d-7ffb.html

Message was edited by: Miguel-F

JamesC01Author
Participating Frequently
August 30, 2012

Because that's not how Batch Insert works, it's a series of function calls on the connector:

local.connect.setAutoCommit(false);

local.sql = "INSERT INTO...VALUES(?,?,?,?)

local.sqlStatement = local.connect.prepareStatement(local.sql);

for (...)

{

local.sqlStatement.setString(1,"#some#");

local.sqlStatement.setString(2,"#thing#");

...

local.sqlStatement.addBatch();

}

local.sqlStatement.executeBatch();

local.connect.commit();   

local.sqlStatement.close();

local.connect.close();          

Miguel-F
Inspiring
August 30, 2012

Which is just a series of insert statements, right?  I think the same thing could be accomplished by doing something like:

<cfquery name="queryname" datasource="datasourcename">

INSERT INTO tablename (column1, column2, column3, column4)

VALUES

<cfloop from="1" to="#whatever#" index="i">

( <cfqueryparam cfsqltype="cf_sql_varchar" value="#value1#" />

,<cfqueryparam cfsqltype="cf_sql_varchar" value="#value2#" />

,<cfqueryparam cfsqltype="cf_sql_varchar" value="#value3#" />

,<cfqueryparam cfsqltype="cf_sql_varchar" value="#value4#" /> )

<cfif i LT whatever>, </cfif>

</cfloop>

</cfquery>