|
|
|
package com.fr.data;
|
|
|
|
|
|
|
|
import com.fr.log.FineLoggerFactory;
|
|
|
|
import com.fr.stable.ParameterProvider;
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.ResultSetMetaData;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 带参数的程序数据集Demo 跟ParamTableDataDemo功能一样,但实现更简单
|
|
|
|
*
|
|
|
|
* @author fanruan
|
|
|
|
*/
|
|
|
|
public class SimpleParamTableDataDemo extends SimpleTableData {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] initColumnNames() {
|
|
|
|
String[] columnNames = new String[10];
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
columnNames[i] = "column#" + i;
|
|
|
|
}
|
|
|
|
return columnNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Object[]> loadData() {
|
|
|
|
// 保存得到的数据库表名
|
|
|
|
String tableName = ((ParameterProvider) (parameters.get().toArray())[0]).getValue().toString();
|
|
|
|
|
|
|
|
// 构造SQL语句,并打印出来
|
|
|
|
String sql = "select * from " + tableName;
|
|
|
|
FineLoggerFactory.getLogger().info("Query SQL of ParamTableDataDemo: \n" + sql);
|
|
|
|
// 保存得到的结果集
|
|
|
|
ArrayList<Object[]> valueList = new ArrayList();
|
|
|
|
Connection con = null;
|
|
|
|
try {
|
|
|
|
con = getConnection();
|
|
|
|
Statement stmt = con.createStatement();
|
|
|
|
ResultSet rs = stmt.executeQuery(sql);
|
|
|
|
// 获得记录的详细信息,然后获得总列数
|
|
|
|
ResultSetMetaData rsmd = rs.getMetaData();
|
|
|
|
int colNum = rsmd.getColumnCount();
|
|
|
|
// 用对象保存数据
|
|
|
|
Object[] objArray = null;
|
|
|
|
while (rs.next()) {
|
|
|
|
objArray = new Object[colNum];
|
|
|
|
for (int i = 0; i < colNum; i++) {
|
|
|
|
objArray[i] = rs.getObject(i + 1);
|
|
|
|
}
|
|
|
|
// 在valueList中加入这一行数据
|
|
|
|
valueList.add(objArray);
|
|
|
|
}
|
|
|
|
// 释放数据库资源
|
|
|
|
rs.close();
|
|
|
|
stmt.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (con != null) {
|
|
|
|
con.close();
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valueList;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 获取数据库连接 driverName和 url 可以换成您需要的
|
|
|
|
*
|
|
|
|
* @return Connection
|
|
|
|
*/
|
|
|
|
private Connection getConnection() {
|
|
|
|
|
|
|
|
String driverName = "org.sqlite.JDBC";
|
|
|
|
String url = "jdbc:sqlite:////Applications//FineReport10_325//webapps//webroot//help//FRDemo.db";
|
|
|
|
String username = "";
|
|
|
|
String password = "";
|
|
|
|
Connection con;
|
|
|
|
try {
|
|
|
|
Class.forName(driverName);
|
|
|
|
con = DriverManager.getConnection(url, username, password);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return con;
|
|
|
|
}
|
|
|
|
}
|