datatable java实现_如何用Java实现.NET中DataTable功能

展开全部

Junit中测试:32313133353236313431303231363533e59b9ee7ad9431333361306366

@Test public void joinTable() { JingZongDB jzdb=new JingZongDB(); DataTable dt1=jzdb.getDataTable("select * from newsType"); DataTable dt2=jzdb.getDataTable("select * from news"); DataTable dt=DataTable.joinTable(dt1, dt2, "id", "typeid"); wl("新闻类型表:"+dt1.getRow().size()); DataTable.outTable(dt1); wl("新闻表:"+dt2.getRow().size()); DataTable.outTable(dt2); wl("合并后:"+dt.getRow().size()); DataTable.outTable(dt); } private void wl(String s) { System.out.println(s); }

最后结果为:

新闻类型表:4 id:1 typeName:学生工作 id:2 typeName:通知公告 id:3 typeName:招生简章 id:4 typeName:教务信息 新闻表:16 id:1 typeid:1 newsName:学生工作1 id:2 typeid:1 newsName:学生工作2 id:3 typeid:1 newsName:学生工作3 id:4 typeid:1 newsName:学生工作4 id:5 typeid:2 newsName:通知公告1 id:6 typeid:2 newsName:通知公告2 id:7 typeid:2 newsName:通知公告3 id:8 typeid:2 newsName:通知公告4 id:9 typeid:3 newsName:招生简章1 id:10 typeid:3 newsName:招生简章2 id:11 typeid:3 newsName:招生简章3 id:12 typeid:3 newsName:招生简章4 id:13 typeid:4 newsName:教务信息1 id:14 typeid:4 newsName:教务信息2 id:15 typeid:4 newsName:教务信息3 id:16 typeid:4 newsName:教务信息4 合并后:16 id:1 typeName:学生工作 id:1 typeid:1 newsName:学生工作1 id:1 typeName:学生工作 id:2 typeid:1 newsName:学生工作2 id:1 typeName:学生工作 id:3 typeid:1 newsName:学生工作3 id:1 typeName:学生工作 id:4 typeid:1 newsName:学生工作4 id:2 typeName:通知公告 id:5 typeid:2 newsName:通知公告1 id:2 typeName:通知公告 id:6 typeid:2 newsName:通知公告2 id:2 typeName:通知公告 id:7 typeid:2 newsName:通知公告3 id:2 typeName:通知公告 id:8 typeid:2 newsName:通知公告4 id:3 typeName:招生简章 id:9 typeid:3 newsName:招生简章1 id:3 typeName:招生简章 id:10 typeid:3 newsName:招生简章2 id:3 typeName:招生简章 id:11 typeid:3 newsName:招生简章3 id:3 typeName:招生简章 id:12 typeid:3 newsName:招生简章4 id:4 typeName:教务信息 id:13 typeid:4 newsName:教务信息1 id:4 typeName:教务信息 id:14 typeid:4 newsName:教务信息2 id:4 typeName:教务信息 id:15 typeid:4 newsName:教务信息3 id:4 typeName:教务信息 id:16 typeid:4 newsName:教务信息4

现在说如何实现

1)实现.net数据库参数化。

package cdu.yas.xykps.util; import java.sql.Blob; import java.sql.Date; /** * @功能描述 sql参数,sql执行时传递的参数用此类封装 * @可能的错误 * @作者 王磊 * @修改说明 * @修改人 */ public class SqlParameter { public SqlParameter(String type, String value) { this.type = type; this.value = value; } public SqlParameter(String type, int intValue) { this.type = type; this.intValue = intValue; } public SqlParameter(String type, boolean boolValue) { this.type = type; this.boolValue = boolValue; } public SqlParameter(String type, Date dateValue) { this.type = type; this.dateValue = dateValue; } public SqlParameter(String type, Blob blobValue) { this.type = type; this.blobValue = blobValue; } String type; String value; int intValue; boolean boolValue; Date dateValue; Blob blobValue; public String getType() { return type; } public String getValue() { return value; } public int getIntValue() { return intValue; } public boolean getBoolValue() { return boolValue; } public Date getDateValue() { return dateValue; } public Blob getBlobValue() { return blobValue; } public void setType(String type) { this.type = type; } public void setValue(String value) { this.value = value; } public void setIntValue(int intValue) { this.intValue = intValue; } public void setBoolValue(boolean boolValue) { this.boolValue = boolValue; } public void setDateValue(Date dateValue) { this.dateValue = dateValue; } public void setBlobValue(Blob blobValue) { this.blobValue = blobValue; } }

2)后台参数执行:

/** * @功能描述 执行一条select语句返回一张数据表,支持多表查询 * @可能的错误 * @作者 王磊 * @修改说明 * @修改人 */ public DataTable getDataTable(String sql, SqlParameter[] p) { Connection conn = DB.createConn(); PreparedStatement ps = DB.prepare(conn, sql); DataTable t = null; try { addSqlParameter(ps, p); ResultSet rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); List row = new ArrayList();// 表所有行集合 List col = null;// 行所有列集合 DataRow r = null; // 单独一行 DataColumn c = null;// 单独一列 // 此处开始循环读数据,每次往表格中插入一行记录 while (rs.next()) { // 初始化行集合, // 初始化列集合 col = new ArrayList(); // 此处开始列循环,每次向一行对象插入一列 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); rs.close(); rs = null; } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(ps); DB.close(conn); } return t; }

3)增加参数的方法:

/**

* @功能描述 增加参数方法

* @可能的错误 需要测试全局数据共享问题,以及可能会扩展参数类型

* @作者 王磊

* @修改说明

* @修改人

*/

private void addSqlParameter(PreparedStatement ps, SqlParameter[] p)

throws SQLException {

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, p[j].getIntValue());

}

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

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

}

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

ps.setBoolean(j + 1, p[j].getBoolValue());

}

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

ps.setDate(j + 1, p[j].getDateValue());

}

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

ps.setBlob(j + 1, p[j].getBlobValue());

}

}

}

----------///

public DataTable getByParentId(int pId) {

String sql = "select * from kpxz where fbh=? order by kpxzsx asc";

SqlParameter[] p = new SqlParameter[1];

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

return db.getDataTable(sql, p);

}

本回答由网友推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值