Wednesday, May 25, 2016

Launch Crystal Report in Java with PostgreSQL database

//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());

           

             PropertyBag propertyBag = new PropertyBag();

            StringBuilder url = new StringBuilder();
                url.append("jdbc:postgresql://").append(host).append(":")
                        .append(port).append("/").append(databaseName);
             
             StringBuilder uri = new StringBuilder();
                uri.append("!org.postgresql.Driver!").append(url.toString())
                        .append("!user={userid}!password={password}");

             propertyBag.put("JDBC Connection String", uri.toString());
             propertyBag.put("Connection URL", url.toString());
             propertyBag.put("Database Classname", "org.postgresql.Driver");
             propertyBag.put("Database", databaseName);
             propertyBag.put("Database Type", "JDBC (JNDI)");
             propertyBag.put("JDBC Connection", "True");
             propertyBag.put("Server", "");
             propertyBag.put("User ID", userName);
             propertyBag.put("Password", password);

            IConnectionInfo connInfo = table.getConnectionInfo();

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

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

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

                        
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