• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Problem with return a ColdFusion query object from a Java class

Explorer ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

Hi!
I need to return a ColdFusion query object from a Java class using a JDBC result set ( java.sql.ResultSet);

I have tried to pass my JDBC result set in to the constructor of the coldfusion.sql.QueryTable class with this code:


ColdFusion code

<cfset pra = createObject("java","QueryUtil").init()>
<cfset newQuery = CreateObject("java", "coldfusion.sql.QueryTable")>
<cfset newQuery.init( pra.getColdFusionQuery () ) >

My java class execute a query to db and return QueryTable

Java code (QueryUtil.java)

import coldfusion.sql.QueryTable; // (CFusion.jar for class QueryTable)
import com.allaire.cfx //(cfx.jar for class Query used from QueryTable)
public class QueryUtil
{
public static coldfusion.sql.QueryTable getColdFusionQuery(java.sql.ResultSet rs)
{
return new coldfusion.sql.QueryTable(rs);
}
}

but when i run cfm page and coldfusion server tries to execute : "<cfset pra = createObject("java","QueryUtil").init()>" this error appears:

Object Instantiation Exception.
An exception occurred when instantiating a java object. The cause of this exception was that: coldfusion/sql/QueryTable.

If i try to execute QueryUtil.java with Eclipse all it works.

Also I have tried to return java.sql.ResultSet directly to coldfusion.sql.QueryTable.init () with failure.

Do you know some other solution?
TOPICS
Advanced techniques

Views

6.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Jun 25, 2007 Jun 25, 2007
thanks cf_dev2

This is the solution:

if you use other jar file in your java.class, you must write the jar's path in the jvm classpath:

-- open C:\CFusionMX7\runtime\bin\jvm.config with text editor;
-- add at the bottom the full path fo your jar files
-- save
-- restar coldfusion

i thing that you can also add classpath in coldfusion administrator, but i don't know how;

i found only CF classpath.

hi to all.

Votes

Translate

Translate
Advocate ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

You can go down the serializable route and convert your java recordset into something that you can load into CF (e.g. WDDX, XML, JSON). That way you get a plain text result from java that you can load into CF. Personally I'd go with WDDX. I'm sure there are some Java WDDX functions and you can use <cfwddx> to convert it to a CF query (pretty fast).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

Why are you trying to return a ColdFusion Query? Usually you would return a native Java datatype like a Vector of value objects. Otherwise your going to have to construct a query in ColdFusion which is extra looping.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 06, 2007 Jun 06, 2007

Copy link to clipboard

Copied

ik1hsr,

I think there are some other problems with the code, but to start with:

[ColdFusion code]

<cfset newQuery = CreateObject("java", "coldfusion.sql.QueryTable")>
<cfset newQuery.init( pra.getColdFusionQuery () ) >

[/ColdFusion code]

The getColdFusionQuery() method already returns a "coldfusion.sql.QueryTable" object. So technically you don't have to create another one.

[ColdFusion code]

<cfset newQuery.init( pra.getColdFusionQuery () ) >

[[/ColdFusion code]

The getColdFusionQuery() method expects a parameter of type java.sql.ResultSet. But you're calling the method without passing in any parameters.

You might want to review the cfx classes. Specifically the addQuery() method.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 11, 2007 Jun 11, 2007

Copy link to clipboard

Copied

I have modified my code:

[Coldfusion code]
...
rs = st.executeQuery("SELECT * FROM Table");
ourQuery = createObject("java","QueryUtil").getColdFusionQuery(rs);
...
[/ColdFusion code]

java class
--------------------------------------------------------
import coldfusion.sql.QueryTable;
public class QueryUtil
{
public static QueryTable getColdFusionQuery(java.sql.ResultSet rs) throws java.sql.SQLException
{
return new QueryTable(rs);
}
}
--------------------------------------------------

But when i run it occurs the same error:

--------------------------------------------------------------------
An exception occurred when instantiating a java object. The cause of this exception was that: coldfusion/sql/QueryTable.
-----------------------------------------------------------------------

Do you know why??

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 11, 2007 Jun 11, 2007

Copy link to clipboard

Copied

ik1hsr,

Can you explain your overall goal? I ask because typically you don't create a java resultset in ColdFusion code. You simply use cfquery.

<cfquery name="YourQuery" datasource="YourDatasource">
SELECT * FROM Table
</cfquery>

This creates a query object named "YourQuery" that you can use in your ColdFusion code. So there is no need to create a java resulset.

Are you trying to do something different?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 11, 2007 Jun 11, 2007

Copy link to clipboard

Copied

Yes, i need to use this java.class...
..now i'm trying to serialize the ResultSet and use it in CF with WDDX....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

this is the reason for which i can't use CFQUERY

-- I have cfc component that implements my Business logic (some sql query)
-- I need to create a java class to replace this cfc.



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

I don't know if it helps, but I found this page with the properties / methods for QueryTable:

http://www.zrinity.com/developers/mx/undocumentation/query.cfm

Do you still get the same error if you try an instantiate the object using the default constructor:

return new QueryTable();

If so, I would theorize that perhaps the CF java objects are protected somehow. Though it doesn't seem so from the link I just mentioned.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

thanks for the reply,but i search in google for a week.....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

Have you tried recreating the entirety of the java code in CF using createObject()?

rs = st.executeQuery("SELECT * FROM Table");
objQT = createObject("java","coldfusion.sql.QueryTable");

<!--- Try init() or populate() --->
objQT.init(rs);
<!--- or --->
objQT.init();
objQT.populate(rs);

Then do a dump of objQT and see what you get?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

ik1hsr,

I haven't tried it, but functionally insuractive's suggestion sounds right.

>> this is the reason for which i can't use CFQUERY
>>-- I have cfc component that implements my Business logic (some sql query)
>> -- I need to create a java class to replace this cfc.

Why switch from cfc's to a java class? Is it a business requirement or some other reason? I ask because: at a basic level cfqueries _may_ be faster than creating your own resultsets. ColdFusion can utilize connection pooling. If you create your own resultsets, I don't think you have that advantage, unless you implement it yourself (or find a way to use CF's pooling). So I think you still have the overhead of opening a connection for each query, which can slow things down.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 12, 2007 Jun 12, 2007

Copy link to clipboard

Copied

it's a business requirement;

Java class implements some method such as search(...),update(...),delete(...),insert(....),new(....)

In coldfusion i use this java class to replace a cfc that implements the same operation.

Someone knows a valid method to pass a ResultSet from JAVA to ColdFusion Query?

I have tried to use the code of this site:

http://www.petefreitag.com/item/73.cfm

But the problem is the same one of the previous post

--------------------------------------------------------------------
An exception occurred when instantiating a java object. The cause of this exception was that: coldfusion/sql/QueryTable.
-----------------------------------------------------------------------

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

stack trace

java.lang.NoClassDefFoundError: coldfusion/sql/QueryTable at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:1655) at java.lang.Class.privateGetPublicMethods(Class.java:1778) at java.lang.Class.getMethods(Class.java:832) at coldfusion.runtime.java.ObjectHandler.Initialize(ObjectHandler.java:40)

...
..
..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

import java.sql.SQLException;
import coldfusion.sql.QueryTable;

public class QueryUtil {

public static coldfusion.sql.QueryTable getColdFusionQuery(java.sql.ResultSet rs)
throws SQLException {

return new coldfusion.sql.QueryTable(rs);
}
}

... (create resultset code) ...
<cfset newQuery = createObject("java","QueryUtil").getColdFusionQuery(rs) />
<!--- must close resulset when finished --->
<cfset rs.close()>
<cfdump var="#newQuery#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

ok

i print all my code

pratica.java execute a query to db and return a querytable


java class
-----------------------------------------------------------------------------------------------------------
import java.util.*;
import java.sql.*;
import coldfusion.sql.*;

public class Pratica {
private HashMap my;
private String URI,LOGIN,PWD,DRIVER;
private Connection conn=null;



/////////////////////////////////////////////////
//funzione init
//
//riceve due strutture converite in hashmap
// globals
// dbprop
////////////////////////////////////////////////

public Pratica(HashMap globals,HashMap dbprop) {

my = new HashMap();
my.put("GLOBALS",globals);
my.put("DBPROP",dbprop);
URI = "jdbc:sqlserver://it-bra-s0016;databaseName=nmobl";
LOGIN = "usr_dev";
PWD = "developer";
DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
try{
// Carico il driver JDBC per la connessione con il database MySQL
Class.forName(DRIVER);
/* Connessione alla base di dati */
conn=DriverManager.getConnection(URI,LOGIN,PWD);
if(conn!=null) System.out.println("Connection Successful!");
} catch (ClassNotFoundException e) {
// Could not find the database driver
System.out.print("\ndriver non trovato "+e.getMessage());
System.out.flush();
}
catch (SQLException e) {
// Could not connect to the database
System.out.print("\nConnessione fallita "+e.getMessage());
System.out.flush();
}
}


//////////////////////////////////////////////////
//funzione search
//
//riceve un hash map con i filtri di ricerca
/////////////////////////////////////////////////

public QueryTable search(/*HashMap arg*/) {
ResultSet rs=null;
Statement stmt=null;
QueryTable ret=null;
String query="SELECT * FROM TAN100pratiche";
try{
stmt = conn.createStatement();// Creo lo Statement per l'esecuzione della query
rs=stmt.executeQuery(query);
// while (rs.next()) {
// System.out.println(rs.getString("descrizione"));
// }
}
catch (Exception e) {
e.printStackTrace();
}

try {
ret = Pratica.RsToQueryTable(rs);
} catch (SQLException e) {
e.printStackTrace();
}
this.close();
return(ret);
// ret=this.RsToQuery(rs);

// this.close(); //chiude le connessioni,recordset e statament


}

//////////////////////////////////////////////////
//retstruct CF vede HashMap come struct
//METODO DI TEST
///////////////////////////////////////////////////
public HashMap retstruct(){
return(my);
}
//////////////////////////////////////////////////
//conversione resultset to querytable
//
//////////////////////////////////////////////////

private static QueryTable RsToQueryTable(ResultSet rs) throws SQLException{
return new QueryTable(rs);
}

/////////////////////////////////////////////
//chiura resultset statament e connessione
////////////////////////////////////////////
private void close(){
try{
conn.close();
conn=null;
}
catch (Exception e) {
e.printStackTrace();
}

}

}
----------------------------------------------------------------------------------------------

coldfusion code

--------------------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Test JDBC CFML Using CFScript</title>
</head>

<body>
<cftry>
<cfset glb_map = createObject("java","java.util.HashMap")>
<cfset dbprop_map = createObject("java","java.util.HashMap")>
<cfset glb_map.init(glb)> <!---are passed from another page--->
<cfset dbprop_map.init(glb["DBPROP"])>
<cfset pra = createObject("java","Pratica").init(glb_map,dbprop_map)>
<cfset ourQuery =createObject("java","coldfusion.sql.QueryTable").init(pra.search())>
<cfcatch>
<h2>Error - info below</h2>
<cfdump var="#cfcatch#"><cfabort>
</cfcatch>
</cftry>
<h2>Success - statement dumped below</h2>
<cfdump var="#ourQuery#">
</body>
</html>
---------------------------------------------------------------------------------------------------

error at line <cfset pra = createObject("java","Pratica").init(glb_map,dbprop_map)>

--------------------------------------------------------------------
An exception occurred when instantiating a java object. The cause of this exception was that: coldfusion/sql/QueryTable.
-----------------------------------------------------------------------

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

What about the stack trace error?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied


Message
coldfusion/sql/QueryTable
StackTrace
java.lang.NoClassDefFoundError: coldfusion/sql/QueryTable at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:1655) at java.lang.Class.privateGetPublicMethods(Class.java:1778) at java.lang.Class.getMethods(Class.java:832) at coldfusion.runtime.java.ObjectHandler.Initialize(ObjectHandler.java:40) at coldfusion.runtime.java.ObjectHandler.<init>(ObjectHandler.java:26) at coldfusion.runtime.java.ReflectionCache$1.fetch(ReflectionCache.java:27) at coldfusion.util.WeakKeyCache.get(WeakKeyCache.java:40) at coldfusion.runtime.java.ReflectionCache.get(ReflectionCache.java:33) at coldfusion.runtime.java.JavaProxy.<init>(JavaProxy.java:27) at coldfusion.runtime.CFPage.createObject(CFPage.java:5783) at coldfusion.runtime.CFPage.CreateObject(CFPage.java:5677) at coldfusion.runtime.CFPage.CreateObject(CFPage.java:5642) at cftest_java_22ecfm1357198390.runPage(C:\CFusionMX7\wwwroot\gruppotesi\goldenring\airone\core\framework\util\test_java_2.cfm:14) at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:152) at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:349) at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65) at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:225) at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:51) at coldfusion.filter.PathFilter.invoke(PathFilter.java:86) at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:27) at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:69) at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:52) at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28) at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38) at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38) at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22) at coldfusion.filter.RequestThrottleFilter.invoke(RequestThrottleFilter.java:115) at coldfusion.CfmServlet.service(CfmServlet.java:107) at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:78) at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91) at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42) at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:257) at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:541) at jrun.servlet.http.WebService.invokeRunnable(WebService.java:172) at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:318) at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:426) at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:264) at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

I think you forgot to import the jdbc driver class. There are different versions, so check the package path may not be correct for your version.

import java.util.*;
import java.sql.*;
import coldfusion.sql.*;
import com.microsoft.sqlserver.jdbc.*; // <!---- import jdbc driver

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 13, 2007 Jun 13, 2007

Copy link to clipboard

Copied

Test the class in your java IDE first - before trying it with ColdFusion:

import java.util.*;
import java.sql.*;
import coldfusion.sql.*;
import com.microsoft.sqlserver.jdbc.*; // <!---- import jdbc driver

public class Pratica {
//.... declarations, other methods ... etcetera

public static void main(String[] args) {
//test java class
Pratica p = new Pratica(new HashMap(), new HashMap());
p.close();
}
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2007 Jun 14, 2007

Copy link to clipboard

Copied

ok i'll try...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 20, 2007 Jun 20, 2007

Copy link to clipboard

Copied

I have tried to import jdbc driver but occurs the same error.......

in my java IDE it works......

????????????????????????????????????

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 21, 2007 Jun 21, 2007

Copy link to clipboard

Copied

import com.microsoft.sqlserver.jdbc.*;

Are the sql server jdbc jars in the CF classpath?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 21, 2007 Jun 21, 2007

Copy link to clipboard

Copied

yes

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 21, 2007 Jun 21, 2007

Copy link to clipboard

Copied

Did you restart CF after replacing the .class file with the newer version? It may still be using the old class file.

If not, can you post the error, with the cf code line.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation