图表生成器Stimulsoft Reports.Java示例:在运行时使用 MySQL 数据库创建 告

此示例项目展示了如何使用 MySQL 字段创建新 告并提取 MySQL 数据库信息。

此示例项目展示了如何使用 MySQL 字段创建新 告并提取 MySQL 数据库信息。

首先,您需要创建一个新 告并添加一个 MySqlDatabase。在StiMySqlDatabase类的构造函数中,您应该设置数据库名称、别名和连接字符串:

public static StiReport createReport() throws ClassNotFoundException, SQLException, StiException, FileNotFoundException {StiReport report = StiReport.newInstance();StiPage page = report.getPages().get(0);report.setDictionary(new StiDictionary(report));StiMySqlDatabase db = new StiMySqlDatabase("test", "test", "url=jdbc:mysql://localhost:3306/sakila;user=root;password=terra2;database=sakila");report.getDictionary().getDatabases().add(db);...

接下来,我们需要创建一个数据源。创建StiMySqlSource对象,提取“演员”数据表字段并填充此数据源:

...StiMySqlSource source = new StiMySqlSource("test.actors", "actors", "actors", "select * from actor");source.setDictionary(report.getDictionary());report.getDictionary().getDataSources().add(source);source.setColumns(new StiDataColumnsCollection());StiMySqlAdapter adapter = new StiMySqlAdapter(db.getConnectionString());Class.forName(adapter.getDriverName());Connection con = com.stimulsoft.webdesigner.helper.StiDictionaryHelper.getConnection(adapter.getJdbcParameters());StiTableFieldsRequest request = StiDataColumnsUtil.getFields(con, source.getQuery(), source);for (StiSqlField field : request.getColunns()) {source.getColumns().add(new StiDataColumn(field.getName(), field.getName(), field.getSystemType()));}...

数据源已创建,现在我们需要在 表页面上显示数据。StiDataBand向 告页面和StiText所有数据列的组件添加一个新组件。还要为标题添加标题:

...StiDataBand dataBand = new StiDataBand();dataBand.setDataSourceName("actors");dataBand.setHeight(0.5);dataBand.setName("DataBand");page.getComponents().add(dataBand);double pos = 0;double columnWidth = page.getWidth() / source.getColumns().size();Integer nameIndex = 1;for (StiDataColumn dataColumn : source.getColumns()) {// Create text on headerStiText hText = new StiText(new StiRectangle(pos, 0, columnWidth, 0.5));hText.setTextInternal(dataColumn.getName());hText.setHorAlignment(StiTextHorAlignment.Center);hText.setName("HeaderText" + nameIndex.toString());hText.setBrush(new StiSolidBrush(StiColorEnum.Orange.color()));hText.getBorder().setSide(StiBorderSides.All);StiText dataText = new StiText(new StiRectangle(pos, 0, columnWidth, 0.5));dataText.setText("{actors." + dataColumn.getName() + "}");dataText.setName("DataText" + nameIndex.toString());dataText.getBorder().setSide(StiBorderSides.All);dataBand.getComponents().add(dataText);pos = pos + columnWidth;nameIndex++;}...

最后,使用Render()report对象的方法渲染创建的 表,并返回结果:

...report.Render();return report;}

为了显示 告,我们使用本机 Java 查看器。我们需要创建查看器对象,添加必要的事件侦听器并分配 告:

public static void showReport(StiReport report) {JFrame frame = new JFrame();JPanel panel = new JPanel();panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));panel.setPreferredSize(new Dimension(1000, 1000));StiViewerFx viewerPanel = new StiViewerFx(frame);panel.add(viewerPanel);frame.add(panel);frame.setSize(new Dimension(1000, 1000));frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);viewerPanel.getStiViewModel().getEventDispatcher().dispatchStiEvent(new StiViewCommonEvent(StiViewCommonEvent.DOCUMENT_FILE_LOADED, new StiDocument(report), null));}public static void main(String[] args) throws ClassNotFoundException, SQLException, StiException, FileNotFoundException {StiReport report = createReport();showReport(report);}

在下面的屏幕截图中,您可以看到示例代码的结果:

 表生成器Stimulsoft Reports.Java示例:在运行时使用 MySQL 数据库创建 告

Aspose、E-iceblue、FastReport、Stimulsoft等文档/ 表图表类开发工具享超低折扣,如有需要可直接联系在线客服。

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论