From cf6488f8cfa4b78b1495f35cb6811e237cba53de Mon Sep 17 00:00:00 2001 From: zack Date: Mon, 26 Apr 2021 19:11:15 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-50963=20=E4=BC=98=E5=8C=96=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E6=8D=AE=E9=9B=86=E4=B8=8A=E5=B1=82=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1(abstracttabledata)=EF=BC=8C=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E6=96=B0demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/data/SimpleParamTableDataDemo.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/main/java/com/fr/data/SimpleParamTableDataDemo.java 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