生成QueryTable在ColdFusion中成功但在Java中失败

时间:2017-06-22 10:38:57

标签: java coldfusion coldfusion-11

我有Java代码片段:

import coldfusion.runtime.Struct;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;

public class CursorTest {
  public static Struct getCursor() throws SQLException {
    OracleCallableStatement statement = null;
    Struct variables = new Struct();
    // prepare statement with a cursor out parameter
    ResultSet results = statement.getCursor( 1 );
    variables.put ( "cursor", results );
    return variables;
  }
}

在构建路径中包含来自ColdFusion cfusion.jar directoy和Oracle lib的{​​{1}}和修补程序jar。

我可以在ColdFusion中运行它:

ojdbc6.jar

这有效,<cfscript> vars = createObject( 'java', 'CursorTest' ).getCursor(); cursor = createObject( 'java', 'coldfusion.sql.QueryTable' ) .init( vars.cursor ) .firstTable(); WriteDump( cursor ); // close the statement and connection. </cfscript> 已创建。

但是,尝试将QueryTable的生成移动到Java代码中:

QueryTable

无法构建:

import coldfusion.runtime.Struct;
import coldfusion.sql.QueryTable;
import coldfusion.sql.Table;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;

public class CursorTest {
  public static Struct getCursor() throws SQLException {
    OracleCallableStatement statement = null;
    Struct variables = new Struct();
    // prepare statement with a cursor out parameter
    ResultSet results = statement.getCursor( 1 );
    Table table = new QueryTable( results ).firstTable();
    variables.put ( "cursor", table );
    return variables;
  }
}

现在,错误是自我解释的,解压缩Jar文件显示该文件不存在...但是,我无法在ColdFusion lib目录,ColdFusion的JRE目录中的任何其他Jar文件中找到它或可能在ColdFusion的类路径上的其他Jars。

有没有人知道这个文件的位置,所以我可以将它包含在构建路径中,或者,ColdFusion如何在没有该包装的情况下使用error: cannot access QueryTableWrapper Table table = new QueryTable( results ).firstTable(); ^ class file for coldfusion.runtime.QueryTableWrapper not found 生成QueryTable

1 个答案:

答案 0 :(得分:1)

["2017-04-18","2017-06-28","2017-07-05"] 有多个构造函数:

  • coldFusion.sql.QueryTable
  • QueryTable()
  • QueryTable( Query cfx )
  • QueryTable( QueryTable rs )
  • QueryTable( QueryTableWrapper qtWrapper )
  • QueryTable( ResultSet rs )
  • QueryTable( int rows )
  • QueryTable( QueryTable rs, int maxrows )
  • QueryTable( int rows, String collist )
  • QueryTable( int rows, String[] cols )
  • QueryTable( int rows, String[] cols, String[] sqlColTypeNames )

在那里列出的前7个构造函数都在表面下使用QueryTable( int rows, String[] cols, String[] sqlColTypeNames, int[] sqlColTypes )并且在Java中失败,因为无法找到类。

但是,QueryTableWrapper不使用此包装器。

因此,虽然您无法传递QueryTable( int rows, String[] cols, String[] sqlColTypeNames, int[] sqlColTypes )ResultSet或现有Query并且已由构造函数自动解析,但您可以生成空QueryTable和然后解析QueryTable并逐行添加到ResultSet,如下所示:

QueryTable

然后可以将final OracleResultSet results = statement.getCursor( 1 ); final ResultSetMetaData metadata = results.getMetadata(); final int columns = metadata.getColumnCount(); final String[] colNames = new String[ columns ]; final String[] sqlColTypeNames = new String[ columns ]; final int[] sqlColTypes = new int[ columns ]; final boolean[] caseSensitive = new boolean[ columns ]; for ( int i = 1; i <= columns; i++ ) { colNames[i-1] = metadata.getColumnName( i ); sqlColTypeNames[i-1] = metadata.getColumnTypeName( i ); sqlColTypes[i-1] = metadata.getColumnType( i ); caseSensitive[i-1] = metadata.isCaseSensitive( i ); } final QueryTable table = new QueryTable( 0, colNames, sqlColTypeNames, sqlColTypes ); table.getMeta().setColumnCase( caseSensitive ); int rows = 0; while ( results.next() ) { table.addRows( 1 ); ++rows; for ( int c = 1; c <= columns; ++c ) { Object value = null; switch( sqlColTypes[c-1] ) { case OracleTypes.ARRAY: value = results.getARRAY( c ).getArray(); break; case OracleTypes.VARCHAR: value = results.getString( c ); break; case OracleTypes.DATE: value = results.getDate( c ); break; case OracleTypes.NUMERIC: value = results.getBigDecimal( c ); break; // Add other types default: throw new UnsupportedOperationException( "Unable to handle: " + sqlColTypes[c-1] ); } if ( results.wasNull() ) { value = null; } table.setField( rows, c, value ); } } 返回到ColdFusion并使用,就像它是由任何内置ColdFusion函数生成的查询结果一样。