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  |  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
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »