www.pudn.com > dbPool.rar > DBColumn.java
package dev.trade.common.db; import java.sql.*; import java.util.*; /** *Title: 数据库表字段的描述对象
* *Description:
* *Copyright: Copyright (c) 2008
* *Company:
* * @author Lucas * @version 1.0 */ public class DBColumn { private String key = null; //--标识 private String name = null; //字段名 private String label = null; //字段标签 private int type = -1; //字段类型 private String typeName = ""; //类型名称 private String className = ""; //类型对应类名 private int displaySize = 20; //显示宽度 private int precision = 0; //精度 private int scale = 0; //标度 private boolean nullable = true; //是否允许为空 private int seqIdx = 0; //--序列值 private boolean bDefault = false; //--默认标记 private boolean constrain = false; //--强制标记 public DBColumn(){ } public DBColumn(String key, String name, String label, int type, int seqIdx){ setKey(key); setName(name); setLabel(label); setType(type); setSeqIdx(seqIdx); } public String getKey(){ return key; } public String getLabel(){ return label; } public int getDisplaySize(){ return displaySize; } public void setClassName(String className){ this.className = className; } public void setKey(String key){ this.key = key; } public void setLabel(String Label){ label = Label; } public void setDisplaySize(int displaySize){ this.displaySize = displaySize; } public void setType(int type){ this.type = type; } public void setName(String name){ this.name = name; } public void setTypeName(String typeName){ this.typeName = typeName; } public void setPrecision(int precision){ this.precision = precision; } public void setScale(int scale){ this.scale = scale; } public void setNullable(boolean nullable){ this.nullable = nullable; } public void setSeqIdx(int seqIdx){ this.seqIdx = seqIdx; } public void setConstrain(boolean constrain){ this.constrain = constrain; } public void setDefault(boolean bDefault){ this.bDefault = bDefault; } public String getClassName(){ return className; } public int getType(){ return type; } public String getName(){ return name; } public String getTypeName(){ return typeName; } public int getPrecision(){ return precision; } public int getScale(){ return scale; } public boolean isNullable(){ return nullable; } public int getSeqIdx(){ return seqIdx; } public boolean isConstrain(){ return constrain; } public boolean isDefault(){ return bDefault; } public String toString(){ return label != null ? label : name != null ? name : ""; } public String toDescString(){ StringBuffer sbRet = new StringBuffer("{"); sbRet.append("key:").append(key == null ? key : "\"" + key + "\"").append(",name:").append(name == null ? name : "\"" + name + "\"").append(",label:").append(label == null ? label : "\"" + label + "\"").append(",typeName:").append(typeName == null ? typeName : "\"" + typeName + "\"").append(",className:").append(className == null ? className : "\"" + className + "\"").append(",type:").append(type).append(",displaySize:").append(displaySize).append( ",precision:").append(precision).append(",scale:").append(scale).append(",seqIdx:"). append( seqIdx).append(",nullable:").append(nullable).append(",default:").append(bDefault). append( ",constrain:").append(constrain).append("}"); return sbRet.toString(); } public boolean equals(Object obj){ if(this == obj) return true; if(obj instanceof DBColumn){ DBColumn col = (DBColumn)obj; return key.equals(col.getKey()) && name.equals(col.getName()) && label.equals(col.getLabel()) && type == col.getType(); } else{ return false; } } public static Comparator getSequenceComparator(){ return seqComparator; } public static DBColumn getDBColumn(ResultSet rs, int i) throws Exception{ if(rs == null || i <= 0){ return null; } else{ ResultSetMetaData meta = rs.getMetaData(); return getDBColumn(meta, i); } } public static DBColumn getDBColumn(ResultSetMetaData meta, int i) throws Exception{ if(meta == null || i <= 0) return null; DBColumn col = new DBColumn(); col.setName(meta.getColumnName(i)); col.setLabel(meta.getColumnLabel(i)); col.setType(meta.getColumnType(i)); col.setTypeName(meta.getColumnTypeName(i)); col.setClassName(meta.getColumnClassName(i)); col.setDisplaySize(meta.getColumnDisplaySize(i)); col.setNullable(meta.isNullable(i) == 1); if(col.getType() != 2005 && col.getType() != 2004) col.setPrecision(meta.getPrecision(i)); col.setScale(meta.getScale(i)); col.setKey(col.getName()); col.setSeqIdx(i); return col; } private static final Comparator seqComparator = new Comparator() { public int compare(Object o1, Object o2){ DBColumn col1 = (DBColumn)o1; DBColumn col2 = (DBColumn)o2; return col1.getSeqIdx() - col2.getSeqIdx(); } }; }