forked from demo/example
zack
4 years ago
1 changed files with 77 additions and 0 deletions
@ -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