Coldfusion jdbcURL Query
I was looking for a way to have coldfusion accept a jdbcURL and query the database. Well I found it: http://cfsilence.com/blog/client/index.cfm/2007/11/7/DSNLess-CFQuery did a great job explaining it, and even wrapped it in a customTag called "query".
Great Job.
Here is the guts of the query.cfm file
Great Job.
Here is the guts of the query.cfm file
<cfif thisTag.ExecutionMode eq "start">
<!--- custom tag attributes --->
<cfparam name="attributes.jdbcURL" default="" />
<cfparam name="attributes.username" default="" />
<cfparam name="attributes.password" default="" />
<cfparam name="attributes.qName" default="" />
<cfelse>
<cfset variables._queryString = thisTag.GeneratedContent>
<cfset thisTag.GeneratedContent = "" />
<cfset _jSQL = createObject("java", "java.sql.DriverManager") />
<cfset _connectURL = attributes.jdbcURL />
<cfset _jSQL.setLoginTimeout(2) />
<cfset _conn = _jSQL.getConnection(_connectURL,"#attributes.username#","#attributes.password#") />
<cfset _stmt = _conn.prepareStatement(javacast("string", preserveSingleQuotes(variables._queryString))) />
<cfset _result = _stmt.ExecuteQuery() />
<cfset caller[attributes.qName] = createObject( "java", "coldfusion.sql.QueryTable").init(_result) />
</cfif>
<!--- custom tag attributes --->
<cfparam name="attributes.jdbcURL" default="" />
<cfparam name="attributes.username" default="" />
<cfparam name="attributes.password" default="" />
<cfparam name="attributes.qName" default="" />
<cfelse>
<cfset variables._queryString = thisTag.GeneratedContent>
<cfset thisTag.GeneratedContent = "" />
<cfset _jSQL = createObject("java", "java.sql.DriverManager") />
<cfset _connectURL = attributes.jdbcURL />
<cfset _jSQL.setLoginTimeout(2) />
<cfset _conn = _jSQL.getConnection(_connectURL,"#attributes.username#","#attributes.password#") />
<cfset _stmt = _conn.prepareStatement(javacast("string", preserveSingleQuotes(variables._queryString))) />
<cfset _result = _stmt.ExecuteQuery() />
<cfset caller[attributes.qName] = createObject( "java", "coldfusion.sql.QueryTable").init(_result) />
</cfif>
Comments
Post a Comment