此示例项目显示如何使用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"%t.setName("DataText" + nameIndex.toString()); dataText.getBorder().setSide(StiBorderSides.All); dataBand.getComponents().add(dataText); pos = pos + columnWidth; nameIndex++; } ...
最后,使用报表对象的Render()方法呈现创建的报表,并返回结果。
... 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); }
示例代码的结果如下图所示: