diff --git a/src/main/java/com/fr/data/SimpleArrayTableDataDemo.java b/src/main/java/com/fr/data/SimpleArrayTableDataDemo.java new file mode 100644 index 0000000..47eddf2 --- /dev/null +++ b/src/main/java/com/fr/data/SimpleArrayTableDataDemo.java @@ -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 loadData() { + ArrayList valueList = new ArrayList(); + valueList.add(new Object[]{"Alex", 15}); + valueList.add(new Object[]{"Helly", 22}); + valueList.add(new Object[]{"Bobby", 99}); + return valueList; + } +} \ No newline at end of file diff --git a/src/main/java/com/fr/data/SimpleParamTableDataDemo.java b/src/main/java/com/fr/data/SimpleParamTableDataDemo.java new file mode 100644 index 0000000..dc1b7fd --- /dev/null +++ b/src/main/java/com/fr/data/SimpleParamTableDataDemo.java @@ -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 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 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; + } +} \ No newline at end of file