Search code examples
mysqljspcrystal-reports

Crystal report:change table name at runtime (jsp code)


I am using crystal report for eclipse to generate dynamic reports(mysql database).
Till now i am able to pass parameters to the report,change the datasource connection :

String connectString = s.getDBpath();
String driverName = s.getClassforName();
String JNDIName = "";
String userName = s.getUserNameDB();            
String password = s.getPasswordDB();        
// Switch all tables on the main report and sub reports
CRJavaHelper.changeDataSource(clientDoc, userName, password, connectString, driverName, JNDIName);

the problem is that i want to change the table name , but i don't know how to do it . i mean that i have many tables with same structure ,every user have its own table , i wanna generate the report based on the table specified for the logged in user. is that possible??


Solution

  • for those whom interested, i found the solution ,and i want share it . all you have to do is to generate the report from a query.

    add the following code to your report viewer:

    // ****** BEGIN POPULATE WITH RESULTSET SNIPPET ****************  
                {
                    // **** POPULATE MAIN REPORT ****
                    {
                         // Connection Info for fetching the resultSet
                        String connectStr = s.getDBpath();
                        String driverName = s.getClassforName();
                        String userName =  s.getUserNameDB();       // TODO: Fill in database user
                        String password = s.getPasswordDB();    // TODO: Fill in valid password
    
    
                        String query = "select * from tbl2";//tbl2 is the table that you wanna make it as a report source
    
                        // As long the Resultset schema has the same field names and types,
                        // then the Resultset can be used as the datasource for the table
                        String tableAlias = "tbl1";     // TODO: Change to correct table alias
                       //tbl1 is the old report source,(data sr=ource for your report)
    
                        // Push the Java ResultSet into the report (this will then be the datasource of the report)
                        CRJavaHelper.passResultSet(clientDoc, fetchResultSet(driverName, connectStr, userName, password, query),
                            tableAlias, "");
    

    hope it helps ;)