实现类似于C#的DataSet功能
1,创建DataSet、DataTable、DataRow、DataColumn、DataColumnCollection和DataRowCollection对象
2,实现DataSet对DataTable的集合操作 DataTable对DataRow及DataColumn的集合操作。
3,实现数据的查询。
实现要求如下:
DataSet myset=new DataSet();
DataTable mytb=new DataTable();
mytb.Columns.Add(new Columns("objname",String.class));
mytb.Columns.Add(new Columns("objjc",String.class));
mytb.Rows.Add(new Object[]{"A000001","testtesttest"});
mytb.Rows.Add(new Object[]{"A000001","testtesttest"});
myset.Tables.add(mytb);
第一步, 创建DataColumn
作用数据字段,包含字段名字段类型及字段数据,支持数据的查找和写入。
public class DataColumn
{
String columnName;//字段名
DataTable table;//对象引用
public Type type;//字段类型
List
第二步,创建 DataColumnCollection集合类对象
作用,列的集合类,支持在集合中添加、移除、查询列。
public final class DataColumnCollection extends ArrayList {
DataTable table;
public DataColumnCollection(DataTable tableCollection) {
this.table = tableCollection;
}
public DataColumn Add(DataColumn column) {
column.SetTable(this.table);
super.add(column);
return column;
}
public DataColumn Add(String columnName, Type type) {
DataColumn column = new DataColumn(columnName, type);
return Add(column);
}
public DataColumn getColumn(String name) throws Exception {
if (name == null) {
throw new Exception("错误!字段名不能为null。");
}
for (Object c : super.toArray()) {
DataColumn column = (DataColumn) c;
if (column.columnName.equals(name))
return column;
}
return null;
}
public boolean Contains(String columnName) {
// 如果当前集合存在明细为value的明细则返回true
for (Object c : super.toArray()) {
DataColumn column = (DataColumn) c;
if (column.columnName.equals(columnName))
return true;
}
return false;
}
public DataColumn getColumn(int i) throws Exception {
DataColumn column;
try {
column = (DataColumn) super.get(i);
} catch (Exception x) {
throw new Exception("此索引处不存在列");
}
return column;
}
public int IndexOf(DataColumn value) {
// 获取指定元素的位置
return super.indexOf(value);
}
public void Remove(DataColumn value) {
// 移除
super.remove(value);
}
public void Remove(String value) {
for (Object c : super.toArray()) {
DataColumn column = (DataColumn) c;
if (column.columnName.equals(value)) {
Remove(column);
break;
}
}
}
}
第三步,创建DataRow
作用,设置和获取某一行,某一列的值
public class DataRow
{
private DataTable table;
private int index;
public DataRow(DataTable table, int index)
{
this.table = table;
this.index = index;
}
public Object getObject(int i) throws Exception
{
Object a=null;
DataColumn column = this.table.Columns.getColumn(i);
return column.getObject(index);
}
public Object SetObject(int i,Object value) throws Exception
{
DataColumn column = this.table.Columns.getColumn(i);
return column.setObject(index, value);
}
public Object getObject(String columnname) throws Exception
{
Object a=null;
DataColumn column = this.table.Columns.getColumn(columnname);
return column.getObject(index);
}
public Object SetObject(String columnname,Object value) throws Exception
{
DataColumn column = this.table.Columns.getColumn(columnname);
return column.setObject(index, value);
}
}
第四步,创建DataRowCollection
作用添加一行数据、根据行号获取行的DataRow
public final class DataRowCollection extends ArrayList
{
private DataTable table;
public DataRowCollection(DataTable table)
{
this.table = table;
}
public DataRow Add(Object[] values) throws Exception
{
int count = table.Columns.size();
if (count < values.length)
{
throw new Exception("输入数组长度大于此表中的列数。");
}
for (int i = 0; i < values.length; i++)
{
if (values[i] != null)
{
this.table.Columns.getColumn(i).setObject(this.size(), values[i]);
}
else
{
this.table.Columns.getColumn(i).setObject(this.size(), null);
}
}
for (int j = values.length; j < count; j++)
{
this.table.Columns.getColumn(j).setObject(this.size(), null);
}
DataRow row = new DataRow(table, this.size());
super.add(row);
return row;
}
public DataRow getRow(int i) throws Exception
{
DataRow datarow;
try
{
datarow = (DataRow)super.get(i);
}
catch (Exception x)
{
throw new Exception("此索引处不存在列");
}
return datarow;
}
}
第五步,创建DataTable
作用创建二维表,支持行、列的查询,新增操作,支持DataTable 结构比较、数据合并操作
public class DataTable {
DataSet dataSet;
public DataColumnCollection Columns;
public DataRowCollection Rows;
String tableName;
public DataTable(DataSet operaBytes)
{
this.dataSet = dataSet;
this.Columns = new DataColumnCollection(this);
this.Rows = new DataRowCollection(this);
}
public DataTable()
{
this(null);
}
///
/// 根据传入字段列表进行数据去重
///
///
///
///
public DataTable ToTable(Boolean distinct, String[] columnNames) throws Exception
{
DataTable table = new DataTable(null);
if (columnNames.length == 0)
{
columnNames = new String[this.Columns.size()];
for (int j = 0; j < columnNames.length; j++)
{
columnNames[j] = this.Columns.getColumn(j).columnName;
}
}
int[] numArray = new int[columnNames.length];
List
for (int i = 0; i < columnNames.length; i++)
{
DataColumn column = this.Columns.getColumn(columnNames[i]);
if (column == null)
{
throw new Exception("去重列不在此table中!");
}
table.Columns.Add(column.Clone());
numArray[i] = this.Columns.IndexOf(column);
}
for (Object c : this.Rows.toArray())
{
DataRow view=(DataRow)c;
Object[] objectArray = new Object[columnNames.length];
for (int k = 0; k < numArray.length; k++)
{
objectArray[k] = view.getObject(numArray[k]);
}
if (!distinct || !this.RowExist(arraylist, objectArray))
{
table.Rows.add(objectArray);
arraylist.add(objectArray);
}
}
return table;
}
///
/// 转换成DataTable
///
///
public DataTable ToDataTable() throws Exception
{
DataTable tb = new DataTable();
int count = this.Columns.size();
for (Object c : this.Columns.toArray())
{
DataColumn column =(DataColumn)c;
tb.Columns.Add(column);
}
for (Object r : this.Rows.toArray())
{
DataRow row=(DataRow)r;
Object[] ob = new Object[count];
for (int i = 0; i < count; i++)
{
ob[i] = row.getObject(i);
}
tb.Rows.Add(ob);
}
return tb;
}
private boolean RowExist(List
{
for (int i = 0; i < arraylist.size(); i++)
{
Object[] objArray = arraylist.get(i);
boolean flag = true;
for (int j = 0; j < objectArray.length; j++)
{
flag &= objArray[j].equals(objectArray[j]);
}
if (flag)
{
return true;
}
}
return false;
}
protected DataTable CreateInstance()
{
return null;
//return (DataTable)Activator.CreateInstance(this.getClass(), true);
}
public DataTable Clone()
{
return null;
//return clone;
}
///
/// 表数据复制
///
///
public DataTable Copy() throws Exception
{
DataTable table = this.Clone();
int count = this.Columns.size();
for (int i = 0; i < count; i++)
{
DataColumn column = this.Columns.getColumn(i);
table.Columns.Add(column.Clone());
}
for (Object c : this.Rows.toArray())
{
DataRow view=(DataRow)c;
Object[] objectArray = new Object[count];
for (int k = 0; k < count; k++)
{
objectArray[k] = view.getObject(k);
}
table.Rows.Add(objectArray);
}
return table;
}
///
/// Table比较,比较提交必须列和类型全部相同
///
///
///
public Boolean Compare(DataTable mydt) throws Exception
{
if (this.Columns.size() != mydt.Columns.size())
return false;
for (int i = 0; i < this.Columns.size(); i++)
{
if (mydt.Columns.Contains(this.Columns.getColumn(i).columnName) == false)
{
return false;
}
else if (this.Columns.getColumn(i).type != mydt.Columns.getColumn(this.Columns.getColumn(i).columnName).type)
{
return false;
}
}
return true;
}
///
/// 数据合并
///
/// 数据合并
///
public DataTable Merge(DataTable mydt) throws Exception
{
if (Compare(mydt))
{
int count = mydt.Rows.size();
int columnCount = mydt.Columns.size();
if (count>0)
{
for (Object c : mydt.Rows.toArray())
{
DataRow view=(DataRow)c;
Object[] objectArray = new Object[columnCount];
for (int k = 0; k < columnCount; k++)
{
objectArray[k] = view.getObject(k);
}
this.Rows.Add(objectArray);
}
}
}
return this;
}
第六部创建DataSet
作用DataTable的集合类
public class DataSet {
DataTable table;
public List
}
小结:
以上既为java端实现简单的DataSet的代码。想实现更复杂的查询、索引、事件操作操作功能如果有业务用到可以自己在进行封装。
下篇章节主要解决,C#和java端序列化和反序列化的问题,欢迎各位拍砖