Crystal报表:在运行时更改表名(jsp代码)

时间:2014-10-14 07:28:13

标签: mysql jsp crystal-reports

我正在使用eclipse的水晶报告来生成动态报告(mysql数据库) 直到现在我能够将参数传递给报告,更改数据源连接:

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

问题是我想更改表名,但我不知道该怎么做。 我的意思是我有很多具有相同结构的表,每个用户都有自己的表,我想根据为登录用户指定的表生成报告。 那可能吗??

1 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,我找到了解决方案,我希望分享它。 您所要做的就是从查询中生成报告。

将以下代码添加到报告查看器中:

// ****** 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, "");

希望它有所帮助;)