iReport連接Mysql創(chuàng)建圖表報表
Jun 07, 2016 pm 04:02 PM列舉一下需要的資源: 1、mySql數(shù)據(jù)庫安裝好的 2、iReport+jasperreport配置好 3、我用的是Myeclipse,MySQL的驅(qū)動jar包不要忘記 第一部分:創(chuàng)建數(shù)據(jù)庫連接 package com.mySqlsource;import java.sql.Connection;public class Database {private String dbUr
列舉一下需要的資源:
1、mySql數(shù)據(jù)庫安裝好的
2、iReport+jasperreport配置好
3、我用的是Myeclipse,MySQL的驅(qū)動jar包不要忘記
第一部分:創(chuàng)建數(shù)據(jù)庫連接
package com.mySqlsource; import java.sql.Connection; public class Database { private String dbUrl = "jdbc:mysql://localhost:3306/bookdb"; private String dbUser="root"; private String dbPwd="123456"; public Database () throws Exception{ Class.forName("com.mysql.jdbc.Driver"); } public Connection getConnection()throws Exception{ return java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd); } public void closeConnection(Connection con){ try{ if(con!=null) con.close(); }catch(Exception e){ e.printStackTrace(); } } }這個格式基本都一樣的,其中bookdb是我新建數(shù)據(jù)庫的名稱,getConnection和closeConnection是兩個操作開關(guān),在使用的時候直接新建Database對象調(diào)用就好了。
第二部分:先看看我的數(shù)據(jù)庫books表
打開iReport新建一張表不懂的話去看其他人的博客,很多的。在界面上找到數(shù)據(jù)源打開進行如下選擇
這個應(yīng)該很簡單的,設(shè)置完之后點擊Test會提示測試成功,否則就是你的某些設(shè)置沒做好,再重新檢查一遍
打開組件面板找到拖動到任意bands中,在iReport4.6.0中有一部分表格是有想到的,一部分沒有,這個倒沒關(guān)系了有的話一路next下去
沒有的話直接完成后在圖表上面右鍵選擇chart data;點擊Details,
這里是有默認名稱的,雙擊默認名稱打開屬性設(shè)置界面
關(guān)于各個字段的意思及作用,我之前的文章有寫到過,這里就不在贅述,有需要的話就翻翻前面的好了。
設(shè)置好之后,點擊預(yù)覽會出現(xiàn)如下情況
原因是在設(shè)計面板右側(cè)圖表的屬性一欄,有一個屬性Evaluation Time,大致意思就是什么時候進行更新數(shù)值,它默認是NOW
在這種情況下,只有你放在detail bands才會出現(xiàn),但是它會出現(xiàn)很多次,不是我們想要的。將它設(shè)置為report就是在報表數(shù)據(jù)配置好之后進行更新,再次預(yù)覽
第三部分:做好了這一步之后,對于可以連接數(shù)據(jù)庫的人來說已經(jīng)夠了,但是如果想要通過web動態(tài)生成客戶想要的報表呢,那么我們還是要通過網(wǎng)絡(luò)連接數(shù)據(jù)庫,之后再動態(tài)我們需要的模板
我使用Myeclipse+tomcat做的網(wǎng)站
package com.mySqlsource; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.sql.Connection; import javax.servlet.ServletException; import javax.servlet.http.*; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; public class MySqlSource extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub try{ String root_path=this.getServletContext().getRealPath("/"); root_path=root_path.replace("\\", "/"); String file_path=root_path+"chart/test_char.jasper"; Database data=new Database(); Connection con=data.getConnection(); JasperReport report= (JasperReport)JRLoader.loadObject(file_path); JasperPrint print=JasperFillManager.fillReport(report, null, con); data.closeConnection(con); OutputStream ouputStream = resp.getOutputStream(); resp.setContentType("application/pdf"); resp.setCharacterEncoding("UTF-8"); resp.setHeader("Content-Disposition", "attachment; filename=\""+ URLEncoder.encode("PDF報表", "UTF-8") + ".pdf\""); // 使用JRPdfExproter導(dǎo)出器導(dǎo)出pdf JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); exporter.exportReport(); ouputStream.close(); }catch(Exception e){ e.printStackTrace(); } } }文件列表
test_char.jasper(本來想是test_chart.jasper后來發(fā)現(xiàn)創(chuàng)建時候少打了一個字母)是iReport預(yù)覽編譯之后生成的,在你的創(chuàng)建目錄中找得到
代碼很簡單,關(guān)鍵的幾個函數(shù):
JasperReport report= (JasperReport)JRLoader.loadObject(file_path); JasperPrint print=JasperFillManager.fillReport(report, null, con); <pre name="code" class="html"> JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);這幾個函數(shù)在之前的文章中也提到過,所以不再啰嗦。
特別注意:要將jasperreport的lib文件最好是都放到web項目的WEB-INF/lib目錄下,生的報錯
如果需要源碼,留郵箱,希望能共同探討

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

1. PHP ?? ?? ? ?? ?????? Laravel MySQL VUE/React ??? ? ?? ??? ???? ??? ?? ?? ??? ?? Laravel MySQL VUE/React ??? ? ?? ?????. 2. ???? ?? (REDIS), ?????? ???, CDN ? ??? ?? ???????. 3. ?? ???, CSRF ??, HTTPS, ???? ??? ? ?? ??? ??? ???????. 4. ? ??? ??, ?? ??, ??, ???, ?? ?? ? ?? ??? ??? ???? ? ? ??? ??? ?? ????.

PHP?? ?? ??? ???? ? ?? ?? ??? ????. 1. php.ini? ?? ??? ??; 2. ? ?? (? : Apache? Setenv ?? nginx? FastCGI_Param)? ??????. 3. PHP ?????? putenv () ??? ??????. ? ??? Php.ini? ????? ??? ???? ??? ???? ? ?? ??? ?? ???? ????? ???? Putenv ()? ?? ??? ?????. ?? ???? ?? ?? (? : php.ini ?? ? ?? ??)? ???? ????. ?? ?? ??? ??? ?? ??? ????? ???? ?? ????.

SSL/TLS ??? MySQL ??? ??? ??? ?????? ????? ?? ??? ?? ??? ???? ?? ??? ? ???? SSL/TLS? ????? ?? ??? ???? ?? ?? ?? ??? ???? ? ????. 2. MySQL? SSL/TLS? ???? ??? ?????? ???? ?? ?? ???? ssl-ca, ssl-cert ? ssl-key ??? ???? ???? ?? ????? ?? ??? ???????. 3. ?????? ??? ? SSL? ???? ??? ?????? ???? ?? ? ? ?? ??? ???? ??; 4. SSL ???? ?? ???? ?? ???? ??? ?? ??, ??? ?? ?? ? ????? ?? ?? ??? ?????.

??? ?? ???? ????? PHP? ?? ??, ??, ?? ? ?? ??? ??????? ?????? ???? ???? ?? ???? ???????. 2. ?? ???? ??? ??? ??? ?? ????????. ???, ?? ???, ?? ?? ?? ?? ??? ??; 3. ?? ?? ???? PHP?? ???? ??? ??? ???? ???? ?? ??? ??? ???? ?? ?? ??? ???? ?? ??? ?????. 4. ?? ??? ???, ??, F1 ? ? CTR, ???? ???? A/B ???? ?? ??? ?????. 5. ?? ??? ??? ?? ??, ??? ?? ??, ?? ?? ?? ? ??? ??? ?? ?? ? ? ????. 6. ?? ??? ???? ?? ? ?? ??, ??? ??, ?? ??? ? SQL ?? ???? ???? ?? ??? ? ??? ??? ?????.

??? PHP ??? ??? ??? ?? ???? ??? ?? ????? ???????. Laravel? ?? ??? ???? ??? ? ? ???? ??? ??? ???? ?????? ?? ? ?? ?? ???? ?????. Symfony? ? ???? ??? ???? ?????. Codeigniter? ??? ??? ?? ??? ?? ??? ?? ????? ?????. 2. AI ??? ???? ????? ??? ??? ??, ???? ?? ?? (? : ???, ??, F1 ?), ??? ? ?? ?? ? ?? ??? ?? ???? ?? ??? ???? ???? ?? ??? ? ?? ???? ?? ?? ??? ????? ?? ???? ????? ?????? ??? ????? ???????. 3. ??? ?? ?? ????? ?? ??? ?????. AES? ?? ??? ???? ????? ?????.

PHP? ??? ?? ??, ?????? ???? ? ?? AI ???? ???? ? ????? ??? ?? ????? ??? ? ??? ??? ??? ?????. 2.?? ??? ?? ?? ??? ????? ???????. ??? ??? ??? ???, PHP ??? ??? ? ?? ??? ???? ?? ?? ?? ??? ???? ??? OpenAI ?? DialogFlow? ?? ?? AI ???? ???? ???? ??? ????. 3. ?? ??? ???? ???? ???? ?? PHP? ?? MySQL ? ?? ??????? ?????. 4. ?? AI ???? guzzle? ???? HTTP ??? ??? Apikeys? ???? ???? ?? ?? ? ?? ??? ? ???????. 5. ?????? ????? ??, ???, ?? ?? ? ??? ???? ???????, ???? ????? ???? ?? ? ??? ???? ?? ???? ???????.

PHP ????? ?? ??? ??? ? ??? ??? CI (Continuous Integration) ????? ???? ? ????. 1. DockerFile? ???? ?? ???, ?? ??, ??? ?? ? ?? ??? ???? PHP ??? ?????. 2. Gitlabci? ?? CI/CD ??? ???? .gitlab-ci.yml ??? ?? ??, ??? ? ?? ??? ???? ?? ??, ??? ? ??? ?????. 3. PHPUNIT? ?? ??? ??? ??? ???? ?? ?? ? ???? ???? ????????. 4. Kubernetes? ?? ?? ?? ??? ???? ?? .yaml ??? ?? ?? ??? ?????. 5. Dockerfile ??? ? ??? ??? ??????

1. PHP? ?? ??? ?? ??? ?? ???? ?? AI ??? ?? ????? ??? ??, API ??, ???? ?? ??, ?? ??? ? ?? ?????? ?????. 2. ???? PHP? ?? ??? ?? ? ??? ???? ????, ??? AI ??? (? : Python ??)? ???? ?? ??? ?? Redis ??? ???? ??? ??????. 3. ?? ??? ?? ??? ???? ?? ?? ?? ????? PHP?? ??? ??? ??? ? ??? ??? ???? ??? ?? AI ???? ?? ????. 4. ???? ???, ?? ???, ??? ? ??? ?? ?????? ??????, ?? ??? ??, ?? ???? ???, ??? ?? ? ?? ?? ???? ?????. PHP? ???? ??, ?????? ? ??? ??? ???? ?? ?? ???????.
