📄 jrcresultsetdatasource.java
字号:
/**
* Applies to: XI Release 2.
* Date Created: October 2005.
* Description: This sample demonstrates how to push a runtime Java Resultset to a
* report as a datasource.
* Author: CW.
*/
//Crystal Java Reporting Component (JRC) imports.
import com.crystaldecisions.reports.sdk.*;
import com.crystaldecisions.sdk.occa.report.lib.*;
//Java Imports.
import javax.swing.*;
import java.sql.*;
public class JRCResultsetDatasource {
private static final String REPORT_NAME = "JRCResultsetDatasource.rpt";
public static void launchApplication() {
try {
//Open report.
ReportClientDocument reportClientDoc = new ReportClientDocument();
reportClientDoc.open(REPORT_NAME, 0);
//Create SQL query.
String query = "SELECT \"RecordSetSchema\".\"Last Name\", \"RecordSetSchema\".\"Birth Date\", \"RecordSetSchema\".\"Salary\", \"RecordSetSchema\".\"Employee ID\", \"RecordSetSchema\".\"Photo\", \"RecordSetSchema\".\"Notes\" " +
"FROM \"Xtreme1\".\"dbo\".\"Employee\" \"RecordSetSchema\"";
//Query database and obtain the Resultset that will be pushed into the report.
ResultSet resultSet = getResultSetFromQuery(query, ResultSet.TYPE_SCROLL_INSENSITIVE);
//Look up existing table in the report to set the datasource for and obtain its alias. This table must
//have the same schema as the Resultset that is being pushed in at runtime. The table could be created
//from a Field Definition File, a Command Object, or regular database table. 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 = reportClientDoc.getDatabaseController().getDatabase().getTables().getTable(0).getAlias();
//Push the Java ResultSet into the report. This will then be the datasource of the report.
reportClientDoc.getDatabaseController().setDataSource(resultSet, tableAlias , "resultsetTable");
//Launch JFrame that contains the report viewer.
new ReportViewerFrame(reportClientDoc);
}
catch(ReportSDKException ex) {
System.out.println(ex);
}
catch(Exception ex) {
System.out.println(ex);
}
}
/**
* Simple utility function for obtaining result sets that will be pushed into the report.
* This is just standard querying of a Java result set and does NOT involve any
* Crystal JRC SDK functions.
*/
private static ResultSet getResultSetFromQuery(String query, int scrollType) throws SQLException, ClassNotFoundException {
//Load JDBC driver for the database that will be queried.
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//Construct JDBC connection.
final String DBUSERNAME = "vantech";
final String DBPASSWORD = "vantech";
final String CONNECTION_URL = "jdbc:microsoft:sqlserver://vancsdb01.crystald.net:1433";
java.sql.Connection connection = DriverManager.getConnection(CONNECTION_URL, DBUSERNAME, DBPASSWORD);
Statement statement = connection.createStatement(scrollType, ResultSet.CONCUR_READ_ONLY);
//Execute query and return result sets.
return statement.executeQuery(query);
}
public static void main(String [] args) {
//Event-dispatching thread to run Swing GUI. This is good practice for Swing applications
//to help ensure that events are dispatched in a predicatable order.
//For more information on using this method, refer to the SUN site below for more details:
//http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Hand-off to worker function to start application.
launchApplication();
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -