共計(jì) 4821 個(gè)字符,預(yù)計(jì)需要花費(fèi) 13 分鐘才能閱讀完成。
這篇文章主要介紹“Java 怎么連接程序數(shù)據(jù)源”,在日常操作中,相信很多人在 Java 怎么連接程序數(shù)據(jù)源問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”Java 怎么連接程序數(shù)據(jù)源”的疑惑有所幫助!接下來,請(qǐng)跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
在實(shí)際應(yīng)用中,可能需要根據(jù)表名動(dòng)態(tài)地改變數(shù)據(jù)源,比如在程序數(shù)據(jù)集中,通過傳進(jìn)的表名參數(shù),到數(shù)據(jù)庫取出對(duì)應(yīng)的表作為數(shù)據(jù)源。例如,F(xiàn)ineReport 是通過 AbstractTableData 抽象類來讀取數(shù)據(jù)源的,而上述所有的數(shù)據(jù)來源都繼承實(shí)現(xiàn)其抽象方法,因此用戶只要實(shí)現(xiàn)了 AbstractTableData 抽象類,也就可以用自定義類型的數(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ù)庫表名
String tableName = parameters[0].getValue().toString();
// 構(gòu)造 SQL 語句, 并打印出來
String sql = select * from + tableName +
FRContext.getLogger().info( Query SQL of ParamTableDataDemo: \n + sql);
// 保存得到的結(jié)果集
valueList = new ArrayList();
// 下面開始建立數(shù)據(jù)庫連接,按照剛才的 SQL 語句進(jì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ù)庫資源
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;
// 保存查詢表的實(shí)際列數(shù)量
private int colNum = 0;
// 保存查詢得到列值
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ù)庫表名
String tableName = parameters[0].getValue().toString();
// 構(gòu)造 SQL 語句, 并打印出來
String sql = select * from + tableName +
FRContext.getLogger().info( Query SQL of ParamTableDataDemo: \n + sql);
// 保存得到的結(jié)果集
valueList = new ArrayList();
// 下面開始建立數(shù)據(jù)庫連接,按照剛才的 SQL 語句進(jì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ù)庫資源
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ù)庫連接 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,將上次查詢的結(jié)果釋放掉
public void release() throws Exception {super.release();
this.valueList = null;
}
編譯 ParamTableDataDemo.java,將生成的 ParamTableDataDemo.class 類文件拷貝到報(bào)表工程 /WEB-INF/classes 目錄下。由于該類是在 com.fr.data 包中的,因此最終應(yīng)該將該類放在 /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ù)庫連接時(shí) URL 的地址是否正確。
可以看到,STSCORE 表中的數(shù)據(jù)已經(jīng)提取至程序數(shù)據(jù)集表中,像其他類型的數(shù)據(jù)集一樣,可以通過拖拽方法實(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)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!