Tuesday, April 1, 2014

Launch Crystal Report in Java with Oracle database

1. Launch Crystal Report in JFrame

//Set report source
ReportClientDocument rcd = new ReportClientDocument();
rcd.open(reportPath + reportName + ".rpt", 0);

DatabaseController databaseController = rcd.getDatabaseController();
Tables tables = databaseController.getDatabase().getTables();

setConnectionInfo(tables, userName, password, host, port, databaseName);

//If there are subreports, need to do the above operation on subreports
IStrings subreportNames = rcd.getSubreportController().getSubreportNames();

SubreportController src = rcd.getSubreportController();

for (int s = 0; s < subreportNames.size(); s++ ) {
      ISubreportClientDocument subreportClientDoc = src.getSubreport(subreportNames.getString(s));
      DatabaseContoller dbc = subreportClientDoc.getDatabaseController();
      Tables subreportTables = dbc.getDatabase().getTables();

      setConnectionInfo(subreportTables, userName, password, host, port, databaseName);
}

//Set parameters
ParameterFieldController pfc = rcd.getDataDefController().getParameterFieldController();
pfc.setCurrentValue("", paramName, paramValue);

//Display report
ReportViewerBean rvb = new ReportViewerBean();
rvb.init();
rvb.start();
rvb.setReportSource(rcd.getReportSource());

//Add the ReportVieverBean to your JFrame content pane
getContentPane.add(rvb);
setVisible(true);

//Close report
rvb.stop();
rvb.destroy();
rcd.close();

private void setConnectionInfo(Tables tables, String userName, String password, String host, String port, String databaseName) {
      for (int i = 0; i < tables.size(); i++) {
             ITable table = tables.getTable(i);

             table.setName(table.getName());
             table.setAlias(table.getAlias());

             IConnectionInfo connInfo = table.getConnectionInfo();
             PropertyBag propertyBag = new PropertyBag();
             propertyBag.put("URI", "!oracle.jdbc.OracleDriver!jdbc:oracle:thin:{userName}/{password}@" + host + ":"+ port +":" + databaseName);
             propertyBag.put("Database DLL", "crdb_jdbc.dll");

             connInfo.setAttributes(propertyBag);
             connInfo.setUserName(userName);
             connInfo.setPassword(password);

             table.setConnectionInfo(connInfo);
             databaseController.setTableLocation(table, tables.getTable(i));
      }
}

2. Launch Crystal Report in web browser

//Construct the URL
String url = new StringBuilder();
url.append("http://)
.append(<your crystal report server>).append(":")
.appned(<your crystal report port>).append("/")
.append(<the file path of your report>)
.append("?reportname=")
.append(<your report name>).append("&")
.append(<parameter name>).append("=")
.append(<parameter value>)
.toString();

url = url.replaceAll(" ", "%20");      
url = url.replaceAll("\\$", "%24");

//Run the command to launch the browser
ProcessBuilder pb = new ProcessBuilder("\"/Program Files/Internet Explorer/iexplore.exe\"", url);
p = pb.start();
p.waitFor();
p.destroy();

---------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment