您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java怎么連接程序數(shù)據(jù)源”,在日常操作中,相信很多人在Java怎么連接程序數(shù)據(jù)源問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java怎么連接程序數(shù)據(jù)源”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
在實(shí)際應(yīng)用中,可能需要根據(jù)表名動(dòng)態(tài)地改變數(shù)據(jù)源,比如在程序數(shù)據(jù)集中,通過(guò)傳進(jìn)的表名參數(shù),到數(shù)據(jù)庫(kù)取出對(duì)應(yīng)的表作為數(shù)據(jù)源。例如,F(xiàn)ineReport是通過(guò)AbstractTableData抽象類(lèi)來(lái)讀取數(shù)據(jù)源的,而上述所有的數(shù)據(jù)來(lái)源都繼承實(shí)現(xiàn)其抽象方法,因此用戶(hù)只要實(shí)現(xiàn)了AbstractTableData抽象類(lèi),也就可以用自定義類(lèi)型的數(shù)據(jù)源了(程序數(shù)據(jù)集),這是帶參程序數(shù)據(jù)集連接的方法。
FineReport報(bào)表引擎就能夠讀取定義的數(shù)據(jù)源作為報(bào)表數(shù)據(jù)源使用,原理就是繼承AbstractTableData。
1、定義參數(shù)
定義一個(gè)參數(shù),并定義數(shù)據(jù)表結(jié)構(gòu),代碼如下:
public ParamTableDataDemo() { // 定義tableName參數(shù) this.parameters = new Parameter[] { new Parameter("tableName") }; // 定義程序數(shù)據(jù)集列名 columnNames = new String[columnNum]; for (int i = 0; i < columnNum; i++) { columnNames[i] = "column#" + String.valueOf(i); } }
2、設(shè)置數(shù)據(jù)
將數(shù)據(jù)放入到定義的表中,代碼如下:
public void init() { // 確保只被執(zhí)行一次 if (valueList != null) { return; } // 保存得到的數(shù)據(jù)庫(kù)表名 String tableName = parameters[0].getValue().toString(); // 構(gòu)造SQL語(yǔ)句,并打印出來(lái) String sql = "select * from " + tableName + ";"; FRContext.getLogger().info("Query SQL of ParamTableDataDemo: \n" + sql); // 保存得到的結(jié)果集 valueList = new ArrayList(); // 下面開(kāi)始建立數(shù)據(jù)庫(kù)連接,按照剛才的SQL語(yǔ)句進(jìn)行查詢(xún) Connection conn = this.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // 獲得記錄的詳細(xì)信息,然后獲得總列數(shù) ResultSetMetaData rsmd = rs.getMetaData(); colNum = rsmd.getColumnCount(); // 用對(duì)象保存數(shù)據(jù) Object[] objArray = null; while (rs.next()) { objArray = new Object[colNum]; for (int i = 0; i < colNum; i++) { objArray[i] = rs.getObject(i + 1); } // 在valueList中加入這一行數(shù)據(jù) valueList.add(objArray); } // 釋放數(shù)據(jù)庫(kù)資源 rs.close(); stmt.close(); conn.close(); // 打印一共取到的數(shù)據(jù)行數(shù)量 FRContext.getLogger().info( "Query SQL of ParamTableDataDemo: \n" + valueList.size() + " rows selected"); } catch (Exception e) { e.printStackTrace(); } }
3、完整的數(shù)據(jù)集代碼
整的帶參程序數(shù)據(jù)集的代碼如下
package com.fr.data; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.ArrayList; import com.fr.base.Env; import com.fr.base.FRContext; import com.fr.data.AbstractTableData; import com.fr.base.Parameter; public class ParamTableDataDemo extends AbstractTableData { // 列名數(shù)組,保存程序數(shù)據(jù)集所有列名 private String[] columnNames = null; // 定義程序數(shù)據(jù)集的列數(shù)量 private int columnNum = 10; // 保存查詢(xún)表的實(shí)際列數(shù)量 private int colNum = 0; // 保存查詢(xún)得到列值 private ArrayList valueList = null; // 構(gòu)造函數(shù),定義表結(jié)構(gòu),該表有10個(gè)數(shù)據(jù)列,列名為column#0,column#1,。。。。。。column#9 public ParamTableDataDemo() { // 定義tableName參數(shù) setDefaultParameters(new Parameter[] { new Parameter("tableName") }); // 定義程序數(shù)據(jù)集列名 columnNames = new String[columnNum]; for (int i = 0; i < columnNum; i++) { columnNames[i] = "column#" + String.valueOf(i); } } // 實(shí)現(xiàn)其他四個(gè)方法 public int getColumnCount() { return columnNum; } public String getColumnName(int columnIndex) { return columnNames[columnIndex]; } public int getRowCount() { init(); return valueList.size(); } public Object getValueAt(int rowIndex, int columnIndex) { init(); if (columnIndex >= colNum) { return null; } return ((Object[]) valueList.get(rowIndex))[columnIndex]; } // 準(zhǔn)備數(shù)據(jù) public void init() { // 確保只被執(zhí)行一次 if (valueList != null) { return; } // 保存得到的數(shù)據(jù)庫(kù)表名 String tableName = parameters[0].getValue().toString(); // 構(gòu)造SQL語(yǔ)句,并打印出來(lái) String sql = "select * from " + tableName + ";"; FRContext.getLogger().info("Query SQL of ParamTableDataDemo: \n" + sql); // 保存得到的結(jié)果集 valueList = new ArrayList(); // 下面開(kāi)始建立數(shù)據(jù)庫(kù)連接,按照剛才的SQL語(yǔ)句進(jìn)行查詢(xún) Connection conn = this.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // 獲得記錄的詳細(xì)信息,然后獲得總列數(shù) ResultSetMetaData rsmd = rs.getMetaData(); colNum = rsmd.getColumnCount(); // 用對(duì)象保存數(shù)據(jù) Object[] objArray = null; while (rs.next()) { objArray = new Object[colNum]; for (int i = 0; i < colNum; i++) { objArray[i] = rs.getObject(i + 1); } // 在valueList中加入這一行數(shù)據(jù) valueList.add(objArray); } // 釋放數(shù)據(jù)庫(kù)資源 rs.close(); stmt.close(); conn.close(); // 打印一共取到的數(shù)據(jù)行數(shù)量 FRContext.getLogger().info( "Query SQL of ParamTableDataDemo: \n" + valueList.size() + " rows selected"); } catch (Exception e) { e.printStackTrace(); } } // 獲取數(shù)據(jù)庫(kù)連接 driverName和 url 可以換成您需要的 public Connection getConnection() { String driverName = "org.sqlite.JDBC"; String url = "jdbc:sqlite://D:\\FineReport_8.0\\WebReport\\FRDemo.db"; String username = ""; String password = ""; Connection con = null; try { Class.forName(driverName); con = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); return null; } return con; } // 釋放一些資源,因?yàn)榭赡軙?huì)有重復(fù)調(diào)用,所以需釋放valueList,將上次查詢(xún)的結(jié)果釋放掉 public void release() throws Exception { super.release(); this.valueList = null; } }
編譯ParamTableDataDemo.java ,將生成的ParamTableDataDemo.class類(lèi)文件拷貝到報(bào)表工程/WEB-INF/classes目錄下。由于該類(lèi)是在com.fr.data包中的,因此最終應(yīng)該將該類(lèi)放在/WEB-INF/classes/com/fr/data下面。此時(shí)該程序數(shù)據(jù)源便定義好了。
4、配置程序數(shù)據(jù)集
新建報(bào)表,在報(bào)表數(shù)據(jù)集中新建程序數(shù)據(jù)源,選擇我們定義好的程序數(shù)據(jù)集,如下圖,名字可以自定義,如divtable
5、使用程序數(shù)據(jù)集
配置好程序數(shù)據(jù)源后便可以使用定義的程序數(shù)據(jù)集了,選中該數(shù)據(jù)集點(diǎn)擊預(yù)覽
按鈕,即可以輸入表名動(dòng)態(tài)地獲取相應(yīng)的數(shù)據(jù)表,并制作模板,如下圖
如果預(yù)覽不出數(shù)據(jù),請(qǐng)確認(rèn)代碼段里面定義數(shù)據(jù)庫(kù)連接時(shí)URL的地址是否正確。
可以看到,STSCORE表中的數(shù)據(jù)已經(jīng)提取至程序數(shù)據(jù)集表中,像其他類(lèi)型的數(shù)據(jù)集一樣,可以通過(guò)拖拽方法實(shí)現(xiàn)單元格數(shù)據(jù)列綁定。
到此,關(guān)于“Java怎么連接程序數(shù)據(jù)源”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。