package com.product.data.sync.util;
|
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 自定义查询结果
|
*/
|
public class CustomResultSet {
|
/**
|
* 结果集
|
*/
|
private ResultSet resultSet;
|
/**
|
* 是否自定义结果集
|
*/
|
private boolean isCustom;
|
/**
|
* next 下表
|
*/
|
private int currentIndex = -1;
|
/**
|
* 结果集中的列名
|
*/
|
private String[] columnNames;
|
/**
|
* 自定义结果集时存储每条数据的集合
|
*/
|
private List<Map<String, Object>> values;
|
|
/**
|
* 构造函数
|
* java.sql.ResultSet
|
*
|
* @param resultSet
|
*/
|
public CustomResultSet(ResultSet resultSet) {
|
this.resultSet = resultSet;
|
this.isCustom = false;
|
}
|
|
/**
|
* 自定义结果集构造
|
*
|
* @param resultSet
|
* @param columnNames
|
* @throws SQLException
|
*/
|
public CustomResultSet(ResultSet resultSet, String[] columnNames) throws SQLException {
|
this.resultSet = resultSet;
|
this.columnNames = columnNames;
|
if (this.columnNames == null || this.columnNames.length <= 0) {
|
this.columnNames = BatchData.getColumnNames(resultSet);
|
}
|
this.isCustom = true;
|
values = new ArrayList<>();
|
while (resultSet.next()) {
|
Map<String, Object> value = new HashMap<>();
|
for (String columnName : this.columnNames) {
|
value.put(columnName, resultSet.getObject(columnName));
|
}
|
values.add(value);
|
}
|
this.resultSet.close();
|
this.resultSet=null;
|
}
|
|
|
public boolean next() throws SQLException {
|
if (isCustom && currentIndex >= 0 && this.values.get(currentIndex) != null) {
|
this.values.get(currentIndex).clear();
|
}
|
currentIndex++;
|
return !isCustom ? resultSet.next() : this.values.size() - 1 >= currentIndex;
|
}
|
|
public Object getObject(String columnLabel) throws SQLException {
|
if (isCustom && this.values.get(this.currentIndex) != null) {
|
return this.values.get(this.currentIndex).get(columnLabel);
|
} else if (!isCustom) {
|
return resultSet.getObject(columnLabel);
|
}
|
return null;
|
}
|
|
public ResultSet getResultSet() {
|
return isCustom ? null : resultSet;
|
}
|
|
public String[] getColumnNames() {
|
return columnNames;
|
}
|
}
|