pf » Turning a JDBC Result set into a ColdFusion query
Turning a JDBC Result set into a ColdFusion query

Recent discussion on the CFCDev mailing list (at cfczone.org) shows how to return a ColdFusion query object from a Java class using a JDBC result set (java.sql.ResultSet). The solution posed by both Brandon Purcell, and I was to pass your JDBC result set in to the constructor of the coldfusion.sql.QueryTable class. Joe Eugene tested the hypothisis, and asserts that it does work, but is slower then returning the result set, and navigating through using the methods of the ResultSet class.
The coldfusion.sql.QueryTable class comes with ColdFusion MX (this technique will only work on CFMX), and is located in the cfusion.jar file. You will need to add the cfusion.jar file to your class path before you can compile any java code that uses the QueryTable class (you don't need to worry about the classpath if you are accessing QueryTable via CFOBJECT, or CreateObject within CFML). More information can be found about the coldfusion.sql.QueryTable class here.
Some ColdFusion code to convert a result set to a Query object:
<cfset newQuery = CreateObject("java", "coldfusion.sql.QueryTable")>
<cfset newQuery.init( someObject.getResultSet() ) >
Some Java code to return a query from a java class (requres cfusion.jar in classpath)
import coldfusion.sql.QueryTable;
public class QueryUtil
{
public static coldfusion.sql.QueryTable getColdFusionQuery(java.sql.ResultSet rs)
{
return new coldfusion.sql.QueryTable(rs);
}
}
Related Entries
- Checking your JDBC Driver Version - March 31, 2004
1. I've been able to successfully instantiate a QueryTable in Java by passing it a ResultSet. However, in my case this only works for a RS containing no more than 9 records. If the RS is 10 records or larger, the QT throws the following error:
java.lang.ExceptionInInitializerError: coldfusion.server.ServiceFactory$ServiceNotAvailableException: The Runtime service is not available.
Anyone know how to get around this?
2. I can successfully create a QueryTable in CF by passing it a ResultSet. However, what I really want is a Query. How do I get from QueryTable to Query?
Get your QueryTable as above:
<cfset newQuery.init( someObject.getResultSet() )>
Then get the first Table (coldfusion.sql.Table) from the QueryTable. A coldfusion.sql.Table is a CF Query:
<cfset myTrueQuery = newQuery.firstTable() >
You can test this:
<cfoutput>#IsQuery(myTrueQuery)#</cfoutput>
IsQuery(newQuery) should return true
<cfset context = createObject("java","javax.naming.Context")> <cfset hashtable = createObject("java","java.util.Hashtable")> <cfset hashtable.put(Context.INITIAL_CONTEXT_FACTORY,"jrun.naming.JRunContextFactory")> <cfset hashtable.put(Context.PROVIDER_URL,"localhost:2902")> <cfset initialContext = createObject("java","javax.naming.InitialContext")> <cfset initialContext.init(hashtable)> <cfset datasource = initialContext.lookup("myDataSource")> <cftry> <cfset connection = datasource.getConnection()> <cfset preparedStatement = connection.prepareStatement("select firstName, lastName from users where userId = ?")> <cfset preparedStatement.setString(1,"XX4567")> <cfset query = createObject("java","coldfusion.sql.QueryTable").init(preparedStatement.executeQuery())> <cfset connection.close()> <cfcatch type="any"> <cftry> <cfset connection.close()> <cfcatch type="any"> </cfcatch> </cftry> <cfrethrow> </cfcatch> </cftry>
- Dear SQL Server Enterprise Manager Developer
- PostalMethods - Web Service for Snail Mail
- Mastering CFQUERYPARAM
- Google Code Search for ColdFusion
- Speaking at CFUNITED 2008
- Getting ColdFusion SQL Statements from SQL Server Trace
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
RSS
add to del.icio.us
Pete Freitag is a software engineer, and web developer located in










