forked from demo/example
Browse Source
Merge in PG/plugin-report-doc-demo from ~ZACK/plugin-report-doc-demo:release/10.0 to release/10.0 * commit '8ec9a4a4ee0b129b0223b216e3a48745e95767f7': 补一下简单程序数据集demo REPORT-50963 优化程序数据集上层设计(abstracttabledata),提交新demorelease/10.0
zack
4 years ago
2 changed files with 106 additions and 0 deletions
@ -0,0 +1,29 @@
|
||||
package com.fr.data; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class SimpleArrayTableDataDemo extends SimpleTableData { |
||||
|
||||
/** |
||||
* 初始化列头 |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String[] initColumnNames() { |
||||
return new String[]{"Name", "Score"}; |
||||
} |
||||
|
||||
/** |
||||
* 加载行列数据 |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public List<Object[]> loadData() { |
||||
ArrayList<Object[]> valueList = new ArrayList(); |
||||
valueList.add(new Object[]{"Alex", 15}); |
||||
valueList.add(new Object[]{"Helly", 22}); |
||||
valueList.add(new Object[]{"Bobby", 99}); |
||||
return valueList; |
||||
} |
||||
} |
@ -0,0 +1,77 @@
|
||||
package com.fr.data; |
||||
|
||||
import com.fr.file.DatasourceManager; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.ParameterProvider; |
||||
|
||||
import java.sql.Connection; |
||||
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(); |
||||
// 下面开始建立数据库连接,按照刚才的SQL语句进行查询
|
||||
com.fr.data.impl.Connection conn = DatasourceManager.getInstance().getConnection("FRDemo"); |
||||
Connection con = null; |
||||
try { |
||||
con = conn.createConnection(); |
||||
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; |
||||
} |
||||
} |
Loading…
Reference in new issue