2012-05-21 81 views
5

我有這樣的代碼如何在java swing應用程序中打開水晶報表?

import com.crystaldecisions.reports.sdk.ReportClientDocument; 
... 

ReportClientDocument rpt = new ReportClientDocument(); 
    rpt.open(reportPath+fileName, 0); 
    rpt.getDatabaseController().logon(DBConnect.getUsername(), DBConnect.getPassword()); 
    Tables tables = rpt.getDatabaseController().getDatabase().getTables(); 

    for(int i=0; i< tables.size(); i++){ 
     System.out.print(i); 
     ITable table = tables.getTable(i); 

     IConnectionInfo connInfo = table.getConnectionInfo(); 

     PropertyBag innerProp = connInfo.getAttributes(); 
     innerProp.clear(); 

     PropertyBag propertyBag = new PropertyBag(); 
     propertyBag.put("Server Type", "JDBC (JNDI)"); 
     propertyBag.put("Database DLL", "crdb_jdbc.dll"); 
     propertyBag.put("Connection String", DBConnect.getConnectionString()); 
     propertyBag.put("Database Class Name", "com.mysql.jdbc.Driver"); 
     propertyBag.put("Use JDBC", "true"); 
     propertyBag.put("Server Name", DBConnect.getServer()); 
     propertyBag.put("Generic JDBC Driver Behavior", "No"); 
     propertyBag.put("URI", "!com.mysql.jdbc.Driver!jdbc:mysql://"+DBConnect.getServer()+":"+DBConnect.getPort()+"/"+DBConnect.getDatabase()+"!ServerType=29!QuoteChar=`"); 

     connInfo.setAttributes(propertyBag); 
     connInfo.setKind(ConnectionInfoKind.SQL); 

     table.setConnectionInfo(connInfo); 
     rpt.getDatabaseController().setTableLocation(table, tables.getTable(i)); 

什麼進出口試圖做的就是打開報表,並通過該連接信息,以該報告,這樣我可以動態改變報告的數據庫,但由於某種原因,它不工作並且報告仍然會從最初設置的數據庫中生成信息。有人能告訴我我做錯了什麼嗎?這是一個擺動應用程序,我正在使用水晶報表XI。 Btw我使用com.crystaldecisions.reports.sdk.ReportClientDocument而不是com.crystaldecisions.sdk.occa.report.application.ReportClientDocument,因爲當我使用另一個,我得到一個無法找到服務器錯誤。請幫忙。

+1

我不明白Swing是如何真正扮演這個角色的,除非您可能需要小心地在後臺線程中調用此代碼,否則創建報告的方式與從Swing GUI執行的方式相同一個控制檯程序。 –

+0

我真的沒有得到。抱歉。除了數據庫部分的動態變化以外,一切都很好。 – John

回答

0

要切換在運行時連接,您可以使用這樣的:在上面的代碼片斷

IConnectionInfo oldConnInfo = new ConnectionInfo(); 
IConnectionInfo newConnInfo = new ConnectionInfo(); 

// If this connection needed parameters, we would use this field. 
com.crystaldecisions.sdk.occa.report.data.Fields pFields = null; 

try{ 
    // Assign the old Connection info to the reports current info 
    //DatabaseController dbController = rptClient.getDatabaseController(); 
    oldConnInfo=dbController.getConnectionInfos(null).getConnectionInfo(0); 
    com.crystaldecisions.sdk.occa.report.lib.PropertyBag boPropertyBag1 = new com.crystaldecisions.sdk.occa.report.lib.PropertyBag(); 
    boPropertyBag1.put("JDBC Connection String","..."); 
    boPropertyBag1.put("Server Type","..."); 
    boPropertyBag1.put("Database Type","..."); 
    boPropertyBag1.put("Database Class Name","..."); 
    boPropertyBag1.put("Use JDBC","..."); 
    boPropertyBag1.put("Connection URL","..."); 
    boPropertyBag1.put("Database DLL","..."); 
    // Assign the properties to the connection info 
    newConnInfo.setAttributes(boPropertyBag1); 
    // Set the DB Username and Pwd 
    newConnInfo.setUserName("..."); 
    newConnInfo.setPassword("...");  
    // The Kind of connectionInfos is SQL 
    newConnInfo.setKind(ConnectionInfoKind.SQL); 

    // set the parameters to replace. 
    // The 4 options are: 
    // _doNotVerifyDB 
    // _ignoreCurrentTableQualifiers 
    // _mapFieldByRowsetPosition 
    // _useDefault 
    int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB; 
    // Now replace the connections 
    dbController.replaceConnection(oldConnInfo, newConnInfo, pFields, replaceParams); 
    }catch(ReportSDKException rse){ 
    ... 
    } 

傳遞適當的值。抱歉,這使用com.crystaldecisions.sdk.occa.report API。

希望這有助於...

+0

謝謝你的回答。 oldConnInfo和newConnInfo都是java.sql.Connection對象嗎? – John

+0

都是IConnectionInfo類型..希望你的意思是...... – ria

+0

我是否需要像所做的那樣更改所有表的屬性?如果不是,我如何從reportclientdocument獲取IConnectionInfo? – John

0

您可以使用水晶報表開發here Eclipse版本。從哪裏可以下載eclipse的插件。

你可以找到一個很好的例子,開始用水晶報表開發使用Java here

您可以找到您的相關報道crystel here,其中規定所有關於水晶報表開發使用Java的必要信息的答案。