
因为我本来就是搞net的,突然来了两个项目就是要用java以及oracle,便从各种方向上寻求net的影子,便有了今天这些工具类。不知道好不好,bug测试了一下了,性能上也只有经过项目来验证吧。其中一部分涉及到双数据库操作,所以也提了一种解决方案,写的不好,大家看看吧!
我们先看效果,再说说怎么弄!
我们来试试如何用,我也写了一个测试数据。
然后我们在Junit中测试:
@Test
publicvoidjoinTable() {
JingZongDB jzdb=newJingZongDB();
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);
}
privatevoidwl(String s) {
System.out.println(s);
}
最后结果为:
新闻类型表:4
id:1typeName:学生工作
id:2typeName:通知公告
id:3typeName:招生简章
id:4typeName:教务信息
新闻表:16
id:1typeid:1newsName:学生工作1
id:2typeid:1newsName:学生工作2
id:3typeid:1newsName:学生工作3
id:4typeid:1newsName:学生工作4
id:5typeid:2newsName:通知公告1
id:6typeid:2newsName:通知公告2
id:7typeid:2newsName:通知公告3
id:8typeid:2newsName:通知公告4
id:9typeid:3newsName:招生简章1
id:10typeid:3newsName:招生简章2
id:11typeid:3newsName:招生简章3
id:12typeid:3newsName:招生简章4
id:13typeid:4newsName:教务信息1
id:14typeid:4newsName:教务信息2
id:15typeid:4newsName:教务信息3
id:16typeid:4newsName:教务信息4
合并后:16
id:1typeName:学生工作 id:1typeid:1newsName:学生工作1
id:1typeName:学生工作 id:2typeid:1newsName:学生工作2
id:1typeName:学生工作 id:3typeid:1newsName:学生工作3
id:1typeName:学生工作 id:4typeid:1newsName:学生工作4
id:2typeName:通知公告 id:5typeid:2newsName:通知公告1
id:2typeName:通知公告 id:6typeid:2newsName:通知公告2
id:2typeName:通知公告 id:7typeid:2newsName:通知公告3
id:2typeName:通知公告 id:8typeid:2newsName:通知公告4
id:3typeName:招生简章 id:9typeid:3newsName:招生简章1
id:3typeName:招生简章 id:10typeid:3newsName:招生简章2
id:3typeName:招生简章 id:11typeid:3newsName:招生简章3
id:3typeName:招生简章 id:12typeid:3newsName:招生简章4
id:4typeName:教务信息 id:13typeid:4newsName:教务信息1
id:4typeName:教务信息 id:14typeid:4newsName:教务信息2
id:4typeName:教务信息 id:15typeid:4newsName:教务信息3
id:4typeName:教务信息 id:16typeid:4newsName:教务信息4
现在说如何实现
1)实现.net数据库参数化。
packagecdu.yas.xykps.util;
importjava.sql.Blob;
importjava.sql.Date;
publicclassSqlParameter {
publicSqlParameter(String type, String value) {
this.type = type;
this.value = value;
}
publicSqlParameter(String type,intintValue) {
this.type = type;
this.intValue = intValue;
}
publicSqlParameter(String type,booleanboolValue) {
this.type = type;
this.boolValue = boolValue;
}
publicSqlParameter(String type, Date dateValue) {
this.type = type;
this.dateValue = dateValue;
}
publicSqlParameter(String type, Blob blobValue) {
this.type = type;
this.blobValue = blobValue;
}
String type;
String value;
intintValue;
booleanboolValue;
Date dateValue;
Blob blobValue;
publicString getType() {
returntype;
}
publicString getValue() {
returnvalue;
}
publicintgetIntValue() {
returnintValue;
}
publicbooleangetBoolValue() {
returnboolValue;
}
publicDate getDateValue() {
returndateValue;
}
publicBlob getBlobValue() {
returnblobValue;
}
publicvoidsetType(String type) {
this.type = type;
}
publicvoidsetValue(String value) {
this.value = value;
}
publicvoidsetIntValue(intintValue) {
this.intValue = intValue;
}
publicvoidsetBoolValue(booleanboolValue) {
this.boolValue = boolValue;
}
publicvoidsetDateValue(Date dateValue) {
this.dateValue = dateValue;
}
publicvoidsetBlobValue(Blob blobValue) {
this.blobValue = blobValue;
}
}
2)后台参数执行:
publicDataTable 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 = newArrayList();// 表所有行集合
List col = null;// 行所有列集合
DataRow r = null;// 单独一行
DataColumn c = null;// 单独一列
// 此处开始循环读数据,每次往表格中插入一行记录
while(rs.next()) {
// 初始化行集合,
// 初始化列集合
col = newArrayList();
// 此处开始列循环,每次向一行对象插入一列
for(inti =1; i <= rsmd.getColumnCount(); i++) {
String columnName = rsmd.getColumnName(i);
Object value = rs.getObject(columnName);
// 初始化单元列
c = newDataColumn(columnName, value);
// 将列信息加入列集合
col.add(c);
}
// 初始化单元行
r = newDataRow(col);
// 将行信息降入行结合
row.add(r);
}
// 得到数据表
t = newDataTable(row);
rs.close();
rs = null;
} catch(SQLException e) {
e.printStackTrace();
} finally{
DB.close(ps);
DB.close(conn);
}
returnt;
}
3)增加参数的方法:
privatevoidaddSqlParameter(PreparedStatement ps, SqlParameter[] p)
throwsSQLException {
for(intj =0; 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());
}
}
}
----------///
publicDataTable getByParentId(intpId) {
String sql = "select * from kpxz where fbh=? order by kpxzsx asc";
SqlParameter[] p = newSqlParameter[1];
p[0] =newSqlParameter("int", pId);
returndb.getDataTable(sql, p);
}
4)看看我们如何调用方法:
publicDataTable getByParentId(intpId) {
String sql = "select * from kpxz where fbh=? order by kpxzsx asc";
SqlParameter[] p = newSqlParameter[1];
p[0] =newSqlParameter("int", pId);
returndb.getDataTable(sql, p);
}
上面就是调用函数的具体方法,现在我们来详细说下DataTable,具体实现如下:
packagecdu.yas.xykps.util;
importjava.util.ArrayList;
importjava.util.List;
publicclassDataTable {
List row;
publicDataTable() {
}
publicDataTable(List r) {
row = r;
}
publicList getRow() {
returnrow;
}
publicvoidsetRow(List row) {
this.row = row;
}
publicstaticDataTable joinTable(DataTable dt1, DataTable dt2, String colName1,
String colName2) {
List newRows = newArrayList();
List rows1 = dt1.getRow();
List rows2 = dt2.getRow();
inti1 = rows1.size();
inti2 = rows2.size();
List temp = newArrayList();
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 cols = newArrayList();
for(DataColumn c : r1.getCol()) {
cols.add(c);
}
for(DataColumn c : r2.getCol()) {
cols.add(c);
}
DataRow rr = newDataRow(cols);
newRows.add(rr);
}
}
}
DataTable dt = newDataTable(newRows);
returndt;
}
publicstaticvoidoutTable(DataTable dt) {
for(DataRow r : dt.getRow()) {
for(DataColumn c : r.getCol()) {
System.out.print(c.getKey() + ":"+ c.getValue() +" ");
}
wl("");
}
}
publicstaticvoidwl(String s) {
System.out.println(s);
}
}
对应DataRow:
packagecdu.yas.xykps.util;
importjava.sql.Blob;
importjava.sql.Date;
importjava.util.List;
publicclassDataRow {
List col;
publicDataColumn getColumnObject(String colName) {
for(DataColumn c : col) {
if(c.getKey().toUpperCase().equals(colName.toUpperCase())) {
try{
returnc;
} catch(Exception e) {
System.out.println("错误描述:"+ e.toString());
}
}
}
returnnull;
}
publicObject getColumn(String colName) {
for(DataColumn c : col) {
if(c.getKey().toUpperCase().equals(colName.toUpperCase())) {
try{
returnc.getValue();
} catch(Exception e) {
System.out.println("错误描述:"+ e.toString());
}
}
}
returnnull;
}
publicintgetIntColumn(String colName) {
for(DataColumn c : col) {
if(c.getKey().toUpperCase().equals(colName.toUpperCase())) {
try{
returnInteger.parseInt(c.getValue().toString());
} catch(Exception e) {
System.out.println("错误描述:"+ e.toString());
}
}
}
return0;
}
publicString getStringColumn(String colName) {
for(DataColumn c : col) {
if(c.getKey().toUpperCase().equals(colName.toUpperCase())) {
try{
returnc.getValue().toString();
} catch(Exception e) {
System.out.println("错误描述:"+ e.toString());
}
}
}
returnnull;
}
publicString eval(String colName) {
for(DataColumn c : col) {
if(c.getKey().toUpperCase().equals(colName.toUpperCase())) {
try{
returnc.getValue() +"";// 此方法将屏蔽错误!!!
} catch(Exception e) {
System.out.println("错误描述:"+ e.toString());
}
}
}
returnnull;
}
publicDate getDateColumn(String colName) {
for(DataColumn c : col) {
if(c.getKey().toUpperCase().equals(colName.toUpperCase())) {
try{
returnDate.valueOf(c.getValue().toString());
} catch(Exception e) {
System.out.println("错误描述:"+ e.toString());
}
}
}
returnnull;
}
publicBlob 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("错误描述:"+ e.toString());
}
}
}
returnnull;
}
publicfloatgetFloatColumn(String colName) {
for(DataColumn c : col) {
if(c.getKey().toUpperCase().equals(colName.toUpperCase())) {
try{
returnFloat.parseFloat(c.getValue().toString());
} catch(Exception e) {
System.out.println("错误描述:"+ e.toString());
}
}
}
return0;
}
publicDataRow(List c) {
col = c;
}
publicList getCol() {
returncol;
}
publicvoidsetCol(List col) {
this.col = col;
}
}
然后数据列:
packagecdu.yas.xykps.util;
importjava.util.Collection;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Set;
publicclassDataColumn {
String key;
Object value;
publicDataColumn(String k, Object v) {
key = k;
value = v;
}
publicString getKey() {
returnkey;
}
publicObject getValue() {
returnvalue;
}
publicvoidsetKey(String key) {
this.key = key;
}
publicvoidsetValue(Object value) {
this.value = value;
}
}
如此一来便实现了java中的DataTable了。好了暂时这个样子了,我还封装了一个分页控件,也有点类似于net里面的分页控件,等下次大家一起看看吧。
【编辑推荐】
【责任编辑:艾婧 TEL:(010)68476606】