如何用Java實(shí)現(xiàn).NET中DataTable功能
因?yàn)槲冶緛砭褪歉鉵et的,突然來了兩個(gè)項(xiàng)目就是要用java以及oracle,便從各種方向上尋求net的影子,便有了今天這些工具類。不知道好不好,bug測(cè)試了一下了,性能上也只有經(jīng)過項(xiàng)目來驗(yàn)證吧。其中一部分涉及到雙數(shù)據(jù)庫操作,所以也提了一種解決方案,寫的不好,大家看看吧!
我們先看效果,再說說怎么弄!
我們來試試如何用,我也寫了一個(gè)測(cè)試數(shù)據(jù)。
然后我們?cè)贘unit中測(cè)試:
- @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("新聞?lì)愋捅恚?quot;+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);
- }
最后結(jié)果為:
- 新聞?lì)愋捅恚?/span>4
- id:1 typeName:學(xué)生工作
- id:2 typeName:通知公告
- id:3 typeName:招生簡(jiǎn)章
- id:4 typeName:教務(wù)信息
- 新聞表:16
- id:1 typeid:1 newsName:學(xué)生工作1
- id:2 typeid:1 newsName:學(xué)生工作2
- id:3 typeid:1 newsName:學(xué)生工作3
- id:4 typeid:1 newsName:學(xué)生工作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:招生簡(jiǎn)章1
- id:10 typeid:3 newsName:招生簡(jiǎn)章2
- id:11 typeid:3 newsName:招生簡(jiǎn)章3
- id:12 typeid:3 newsName:招生簡(jiǎn)章4
- id:13 typeid:4 newsName:教務(wù)信息1
- id:14 typeid:4 newsName:教務(wù)信息2
- id:15 typeid:4 newsName:教務(wù)信息3
- id:16 typeid:4 newsName:教務(wù)信息4
- 合并后:16
- id:1 typeName:學(xué)生工作 id:1 typeid:1 newsName:學(xué)生工作1
- id:1 typeName:學(xué)生工作 id:2 typeid:1 newsName:學(xué)生工作2
- id:1 typeName:學(xué)生工作 id:3 typeid:1 newsName:學(xué)生工作3
- id:1 typeName:學(xué)生工作 id:4 typeid:1 newsName:學(xué)生工作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:招生簡(jiǎn)章 id:9 typeid:3 newsName:招生簡(jiǎn)章1
- id:3 typeName:招生簡(jiǎn)章 id:10 typeid:3 newsName:招生簡(jiǎn)章2
- id:3 typeName:招生簡(jiǎn)章 id:11 typeid:3 newsName:招生簡(jiǎn)章3
- id:3 typeName:招生簡(jiǎn)章 id:12 typeid:3 newsName:招生簡(jiǎn)章4
- id:4 typeName:教務(wù)信息 id:13 typeid:4 newsName:教務(wù)信息1
- id:4 typeName:教務(wù)信息 id:14 typeid:4 newsName:教務(wù)信息2
- id:4 typeName:教務(wù)信息 id:15 typeid:4 newsName:教務(wù)信息3
- id:4 typeName:教務(wù)信息 id:16 typeid:4 newsName:教務(wù)信息4
#p#
現(xiàn)在說如何實(shí)現(xiàn)
1)實(shí)現(xiàn).net數(shù)據(jù)庫參數(shù)化。
- package cdu.yas.xykps.util;
- import java.sql.Blob;
- import java.sql.Date;
- /**
- * @功能描述 sql參數(shù),sql執(zhí)行時(shí)傳遞的參數(shù)用此類封裝
- * @可能的錯(cuò)誤
- * @作者 王磊
- * @修改說明
- * @修改人
- */
- 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)后臺(tái)參數(shù)執(zhí)行:
- /**
- * @功能描述 執(zhí)行一條select語句返回一張數(shù)據(jù)表,支持多表查詢
- * @可能的錯(cuò)誤
- * @作者 王磊
- * @修改說明
- * @修改人
- */
- 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<DataRow> row = new ArrayList<DataRow>();// 表所有行集合
- List<DataColumn> col = null;// 行所有列集合
- DataRow r = null; // 單獨(dú)一行
- DataColumn c = null;// 單獨(dú)一列
- // 此處開始循環(huán)讀數(shù)據(jù),每次往表格中插入一行記錄
- while (rs.next()) {
- // 初始化行集合,
- // 初始化列集合
- col = new ArrayList<DataColumn>();
- // 此處開始列循環(huán),每次向一行對(duì)象插入一列
- 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);
- // 將行信息降入行結(jié)合
- row.add(r);
- }
- // 得到數(shù)據(jù)表
- t = new DataTable(row);
- rs.close();
- rs = null;
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- DB.close(ps);
- DB.close(conn);
- }
- return t;
- }
#p#
3)增加參數(shù)的方法:
- /**
- * @功能描述 增加參數(shù)方法
- * @可能的錯(cuò)誤 需要測(cè)試全局?jǐn)?shù)據(jù)共享問題,以及可能會(huì)擴(kuò)展參數(shù)類型
- * @作者 王磊
- * @修改說明
- * @修改人
- */
- 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);
- }
4)看看我們?nèi)绾握{(diào)用方法:
- 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);
- }
#p#
上面就是調(diào)用函數(shù)的具體方法,現(xiàn)在我們來詳細(xì)說下DataTable,具體實(shí)現(xiàn)如下:
- package cdu.yas.xykps.util;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @功能描述 數(shù)據(jù)表,即是表格集合,為L(zhǎng)ist類型,其中List中每一個(gè)對(duì)象是一個(gè)DataRow類(List)
- * @可能的錯(cuò)誤
- * @作者 王磊
- * @修改說明
- * @修改人
- */
- public class DataTable {
- List<DataRow> row;
- public DataTable() {
- }
- public DataTable(List<DataRow> r) {
- row = r;
- }
- public List<DataRow> getRow() {
- return row;
- }
- public void setRow(List<DataRow> row) {
- this.row = row;
- }
- /**
- * @功能描述 雙表根據(jù)兩表關(guān)聯(lián)字段連接,要求兩表必須包含公告字段,并且每一行數(shù)據(jù)公共字段相等 。dt1對(duì)應(yīng)colname1 ,dt2
- * 對(duì)應(yīng)colName2
- * @可能的錯(cuò)誤 未完成
- * @作者 王磊
- * @修改說明
- * @修改人
- */
- public static DataTable joinTable(DataTable dt1, DataTable dt2, String colName1,
- String colName2) {
- List<DataRow> newRows = new ArrayList<DataRow>();
- List<DataRow> rows1 = dt1.getRow();
- List<DataRow> rows2 = dt2.getRow();
- int i1 = rows1.size();
- int i2 = rows2.size();
- List<DataRow> temp = new ArrayList<DataRow>();
- String tempC = "";
- if (i1 > i2) {
- temp = rows1;
- rows1 = rows2;
- rows2 = temp;
- tempC = colName1;
- colName1 = colName2;
- colName2 = tempC;
- }
- for (DataRow r1 : rows1) {
- String value1 = r1.eval(colName1);
- for (DataRow r2 : rows2) {
- String value2 = r2.eval(colName2);
- if (value1.equals(value2)) {
- List<DataColumn> cols = new ArrayList<DataColumn>();
- for (DataColumn c : r1.getCol()) {
- cols.add(c);
- }
- for (DataColumn c : r2.getCol()) {
- cols.add(c);
- }
- DataRow rr = new DataRow(cols);
- newRows.add(rr);
- }
- }
- }
- DataTable dt = new DataTable(newRows);
- return dt;
- }
- public static void outTable(DataTable dt) {
- for (DataRow r : dt.getRow()) {
- for (DataColumn c : r.getCol()) {
- System.out.print(c.getKey() + ":" + c.getValue() + " ");
- }
- wl("");
- }
- }
- public static void wl(String s) {
- System.out.println(s);
- }
- }
對(duì)應(yīng)DataRow:
- package cdu.yas.xykps.util;
- import java.sql.Blob;
- import java.sql.Date;
- import java.util.List;
- /**
- * @功能描述 數(shù)據(jù)行,即是表格中的每一行數(shù)據(jù)的集合,為L(zhǎng)ist類型,其中List中每一個(gè)對(duì)象是一個(gè)DataColumn類(鍵值對(duì))
- * @可能的錯(cuò)誤 當(dāng)需要取得行數(shù)據(jù)中某一列時(shí),若是類型轉(zhuǎn)換不匹配會(huì)出錯(cuò)(例:1 若是數(shù)據(jù)項(xiàng)為字符串,我們?nèi)r(shí)候想取整形;);
- * @作者 王磊
- * @修改說明 由于取為空字符串時(shí)候會(huì)報(bào)類型轉(zhuǎn)換的錯(cuò)誤,便使用了異常處理
- * @修改人 王磊
- */
- public class DataRow {
- List<DataColumn> col;
- /**
- * @功能描述 返回指定DataColumn類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public DataColumn getColumnObject(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return c;
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return null;
- }
- /**
- * @功能描述 返回指定Object類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public Object getColumn(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return c.getValue();
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return null;
- }
- /**
- * @功能描述 返回指定int類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public int getIntColumn(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return Integer.parseInt(c.getValue().toString());
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return 0;
- }
- /**
- * @功能描述 返回指定String類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public String getStringColumn(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return c.getValue().toString();
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return null;
- }
- /**
- * @功能描述 返回指定String類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public String eval(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return c.getValue() + "";// 此方法將屏蔽錯(cuò)誤?。?!
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return null;
- }
- /**
- * @功能描述 返回指定Date類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public Date getDateColumn(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return Date.valueOf(c.getValue().toString());
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return null;
- }
- /**
- * @功能描述 返回指定Blob類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public Blob getBlobColumn(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return (Blob) c.getValue();
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return null;
- }
- /**
- * @功能描述 返回指定Blob類型數(shù)據(jù)列對(duì)象
- * @作者 王磊
- */
- public float getFloatColumn(String colName) {
- for (DataColumn c : col) {
- if (c.getKey().toUpperCase().equals(colName.toUpperCase())) {
- try {
- return Float.parseFloat(c.getValue().toString());
- } catch (Exception e) {
- System.out.println("錯(cuò)誤描述:" + e.toString());
- }
- }
- }
- return 0;
- }
- public DataRow(List<DataColumn> c) {
- col = c;
- }
- public List<DataColumn> getCol() {
- return col;
- }
- public void setCol(List<DataColumn> col) {
- this.col = col;
- }
- }
然后數(shù)據(jù)列:
- package cdu.yas.xykps.util;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- /**
- * @功能描述 數(shù)據(jù)列,也是最簡(jiǎn)單的數(shù)據(jù)項(xiàng),相當(dāng)于表格中的一個(gè)單元項(xiàng)目。
- * 采用健值對(duì)思想,key為列名,value對(duì)應(yīng)key值的單元格元素,為Object類型
- * @可能的錯(cuò)誤
- * @作者 王磊
- * @修改說明
- * @修改人
- */
- 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;
- }
- }
如此一來便實(shí)現(xiàn)了java中的DataTable了。好了暫時(shí)這個(gè)樣子了,我還封裝了一個(gè)分頁控件,也有點(diǎn)類似于net里面的分頁控件,等下次大家一起看看吧。
【編輯推薦】