您的位置:首页 > 编程语言 > Java开发

在java中实现类似于.net中的DataTable,请各位看看,这种方法可行吗?

2011-07-10 02:04 543 查看
由于项目



需求,最近必须用java做一个项目。












说句实话,我做起来非常之不爽快,各种不爽快,随便什么地方都想和.net靠近,

所以晚上就写了这个模拟DataTable的简单功能,初步测试是通过了,由于太晚了也不能优化。

现在贴出代码请各位大哥,帮物品看看,此种方法是否可行???

在java中实现.net的datatable功能!!!













package cdu.yas.zfkp.util;

import java.util.List;

public class DataTable {

List<DataRow> row;

public DataTable(List<DataRow> r) {

row = r;

}

public List<DataRow> getRow() {

return row;

}

public void setRow(List<DataRow> row) {

this.row = row;

}

}

package cdu.yas.zfkp.util;

import java.util.List;

public class DataRow {

List<DataColumn> col;

public DataRow(List<DataColumn> c) {

col = c;

}

public List<DataColumn> getCol() {

return col;

}

public void setCol(List<DataColumn> col) {

this.col = col;

}

}

package cdu.yas.zfkp.util;

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class DataColumn {

String key;

Object value;

public DataColumn(String k, Object v) {

key = k;

value = v;

}

public String getKey() {

return key;

}

public Object getValue() {

return value;

}

public void setKey(String key) {

this.key = key;

}

public void setValue(Object value) {

this.value = value;

}

}

package cdu.yas.zfkp.util;

public class SqlParameter {

public SqlParameter(String type, String value) {

this.type = type;

this.value = value;

}

String type;

String value;

public String getType() {

return type;

}

public String getValue() {

return value;

}

public void setType(String type) {

this.type = type;

}

public void setValue(String value) {

this.value = value;

}

}

public DataTable executeValue(String sql, SqlParameter[] p) {

Connection conn = DB.createConn();

PreparedStatement ps = DB.prepare(conn, sql);

List<List<HashMap<String, Object>>> table = new ArrayList<List<HashMap<String, Object>>>();

DataTable t = null;

try {

for (int j = 0; j < p.length; j++) {

// wl(p[j].getValue() + "--" + p[j].getType() + "--" + j);

if (p[j].getType().equals("int")) {

ps.setInt(j + 1, Integer.parseInt(p[j].getValue()));

}

if (p[j].type.equals("String")) {

ps.setString(j + 1, p[j].getValue());

}

if (p[j].type.equals("Date")) {

ps.setDate(j + 1, Date.valueOf(p[j].getValue()));

}

}

ResultSet rs = ps.executeQuery();

ResultSetMetaData rsmd = rs.getMetaData();

List<DataRow> row = new ArrayList<DataRow>();// 表所有行集合

List<DataColumn> col = null;// 行所有列集合

DataRow r = null; // 单独一行

DataColumn c = null;// 单独一列

// 此处开始循环读数据,每次往表格中插入一行记录

while (rs.next()) {

// 初始化行集合,

// 初始化列集合

col = new ArrayList<DataColumn>();

// 此处开始列循环,每次向一行对象插入一列

for (int i = 1; i <= rsmd.getColumnCount(); i++) {

String columnName = rsmd.getColumnName(i);

Object value = rs.getObject(columnName);

// 初始化单元列

c = new DataColumn(columnName, value);

// 将列信息加入列集合

col.add(c);

}

// 初始化单元行

r = new DataRow(col);

// 将行信息降入行结合

row.add(r);

}

// 得到数据表

t = new DataTable(row);

} catch (SQLException e) {

e.printStackTrace();

} finally {

DB.close(ps);

DB.close(conn);

}

return t;

}

最后的测试方法

private void wl(String s) {

System.out.println(s);

}

@Test

public void testSql() throws SQLException {

DB db = new DB();

SqlParameter[] p = new SqlParameter[1];

p[0] = new SqlParameter("int", "1");

String sql = "select * from kpxz where kpxzbh!=?";

DataTable table = db.executeValue(sql, p);

for (DataRow row : table.getRow()) {

for (DataColumn col : row.getCol()) {

System.out.print(col.getKey() + ":" + col.getValue() + "--");

}

wl("");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: