Use eclipse to display the Crystal Report on JSP (3) use SQL statements to filter data

Source: Internet
Author: User
Tags dname

In. net, you can easily use SQL statements to filter report data, but this function is not set in cr4e, but can be completed by writing code.

Here is an example of a simple SQL statement used to filter data.

The project still uses the Tomcat project in (2.

Create a class to implement this function

Jrc_resultset_performance.java

Package com. jrc. util;
Import java. SQL .*;
Import javax. servlet. http .*;

Import com. crystaldecisions. Reports. SDK .*;
Import com. crystaldecisions. SDK. occa. Report. Lib .*;

Public class jrc_resultset_datasource ...{
Private string report_name = "";
Public jrc_resultset_datasource (string report_name )...{
This. report_name = report_name;
}

/***//**
* @ Return report_name
*/
Public String getreport_name ()...{
Return report_name;
}

/***//**
* @ Param report_name the report_name to be set
*/
Public void setreport_name (string report_name )...{
Report_name = report_name;
}


/***//**
* Connect to the database and use SQL query statements to query and return the result set.
*/
Private Static resultset getresultsetfromquery (string query, int scrolltype)
Throws sqlexception, classnotfoundexception ...{
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
Final string dbusername = "username ";
Final string dbpassword = "password ";
Final string connection_url = "JDBC: Microsoft: sqlserver: // localhost: 1433; database = dname ";

Java. SQL. Connection connection = drivermanager. getconnection (connection_url, dbusername, dbpassword );
Statement statement = connection. createstatement (scrolltype, resultset. concur_read_only );

Return statement.exe cutequery (query );

}
/***//**
* Using SQL statements to filter report data does not have to be miserable in. net.
*/
Public Boolean isreportsourceinsession (string session_name, httpsession session) throws reportsdkexception, sqlexception, classnotfoundexception ...{
Boolean flag = false;
Try ...{
// OPEN THE CRYSTAL REPORT
Reportclientdocument reportclientdoc = new reportclientdocument ();
Reportclientdoc. Open (report_name, 0 );
// In an SQL query statement, the number of returned fields must be the same as that in the report. The number of returned fields must be unique or distinct, and the field type must be the same as that in the report. Other fields can be used regardless of the data.
// Fill in the from table, such as the database name. DBO. database table. It is best to create an alias.
String query = "select TT. test_1, TT. test_2, TT. test_3, TT. test_4 from dname. DBO. Test TT ";

Resultset = This. getresultsetfromquery (query, resultset. type_scroll_insensitive );

String tablealias = reportclientdoc. getdatabasecontroller (). getdatabase (). gettables (). gettable (0). getalias ();
// Put the result set into the report. A datasource is automatically generated.
Reportclientdoc. getdatabasecontroller (). setdatasource (resultset, tablealias, "resultsettable ");
Session. setattribute (session_name, reportclientdoc. getreportsource ());
Flag = true;
Return flag;
} Catch (exception e )...{
// Todo: handle exception
E. printstacktrace ();
Return flag;
}

}
}

Note that the number of fields in the database query result set must be the same as the number of fields in the report, and the type must be the same. Otherwise, an error will occur.

The name of the table in the SQL statement must be complete, such as the database name. DBO. database table. It is best to create an alias.

Display page

Result_viewer.jsp

 

<% @ Page import = "com. jrc. util. jrc_resultset_datasource" %>
<% -- Webreporting. jar -- %>
<% @ Page import = "com. crystaldecisions. Report. Web. Viewer. *" %>
<% -- Jrcerom. jar -- %>
<% @ Page import = "com. crystaldecisions. Reports. SDK. *" %>
<%
Jrc_resultset_datasource jrcd = new jrc_resultset_datasource ("resultset. rpt ");
If (! Jrcd. isreportsourceinsession ("reportsource", session)
Response. sendredirect ("error.html ");
Crystalreportviewer crviewer = new crystalreportviewer ();
Crviewer. setownpage (true );
Crviewer. setownform (true );
Crviewer. setprintmode (crprintmode. ActiveX );

Object reportsource = session. getattribute ("reportsource ");
Crviewer. setreportsource (reportsource );

Crviewer. processhttprequest (request, response, this. getservletconfig (). getservletcontext (), null );
%>

Note that the above points should be okay.

 

PS: some practical code segments of Crystal Reports are listed at the bottom of the snippets view.

Cr viewer tags, opens and queries reports, opens reports, views reports, views reports, and sets database logon, exports reports as PDF, and exports reports as RTF. This is easy to use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.