Ask Ben: Processing A Query In Java

Posted July 19, 2006 at 2:31 PM

Tags: ColdFusion, Ask Ben

How do you send and process a coldfusion query to a java object created by cfobject (not through cfx)?

I have to admit, as my Car Talk friends would say, this one "Stumped the Chump". I am not a Java expert. From what I have read though, underlying the ColdFusion query object is the Java class:

coldfusion.sql.QueryTable

This implements the Java interfaces for:

javax.sql.RowSet and coldfusion.wddx.RecordSet

When you send ColdFusion objects to Java, ColdFusion generally does a type-cast automatically to the appropriate Java type. I am not sure how to test this here, but you might be able to pass the Query object directly to Java. If not, you can explicitly convert the Query object to the Java QueryTable object:

 Launch code in new window » Download code as text file »

  • <cfset jQueryTable = CreateObject(
  • "java",
  • "coldfusion.sql.QueryTable"
  • ).Init( qColdFusionQuery )
  • />

... or as CFObject ...

 Launch code in new window » Download code as text file »

  • <cfobject
  • type="java"
  • action="create"
  • name="jQueryTable"
  • class="coldfusion.sql.QueryTable"
  • />
  •  
  • <!--- Initialize the object. --->
  • <cfset jQueryTable.Init( qColdFusionQuery ) />

This creates an instance of the Java QueryTable and passes in your ColdFusion query (qColdFusionQuery) as a the constructor argument. Then, once you get this object into your Java arena, you can use the built in methods to manipulate it. I got this list off of the page http://www.activsoftware.com/mx/undocumentation/query.cfm originally posted on Pete Freitag's site:

queryName.first()
queryName.last()
queryName.isFirst()
queryName.isLast()
queryName.next()
queryName.previous()
queryName.findColumn(String name)
sort(int columnID, boolean ascending)
getColumnTypeName(int columnID)
... many more functions here ...

As far as passing this into a function or returning it from a function, you just have to make sure that object type is defined as "coldfusion.sql.QueryTable".

As someone commented on Pete's site (linked above), to get back to a regular ColdFusion query object, you can do:

 Launch code in new window » Download code as text file »

  • <cfset qActualColdFusionQuery = jQueryTable.FirstTable() />

When I first started looking into this question, I went about it with the idea of building up a ResultSet from scratch. I couldn't find any information on that at all. Then I thought maybe casting XML to ResultSet somehow? No go on that either. But since the QueryTable class implements the interfaces for RowSet, you should be able to do just about everything you want to do.

NOTE: I learned about this FOR the answer and it may contain misinformation.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »