📄 dbdatabox.java
字号:
package dev.trade.common.db;
import java.util.*;
import java.sql.Date;
import java.sql.*;
/**
* <p>Title: 数据库表数据封装对象</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author Lucas
* @version 1.0
*/
public class DBDataBox
{
Map colIdxMap = null;
List colList = null;
List dataList = null;
public DBDataBox(){
colIdxMap = new HashMap();
colList = new ArrayList();
dataList = new ArrayList();
}
public DBDataBox(int initColCount){
colIdxMap = new HashMap(initColCount);
colList = new ArrayList(initColCount);
dataList = new ArrayList();
}
public DBDataBox(int initColCount, int initRowCount){
colIdxMap = new HashMap(initColCount);
colList = new ArrayList(initColCount);
dataList = new ArrayList(initRowCount);
}
public boolean isDataNull(int row, int col){
return getData(row, col) == null;
}
public boolean isDataNull(int row, String colName){
return getData(row, colName) == null;
}
public Object getData(int row, int col){
if(row < 0 || col < 0 || row >= dataList.size() || col >= colList.size()){
throw new IndexOutOfBoundsException("无效的参数,row=" + row + ",col=" + col);
} else{
List rowList = (List)dataList.get(row);
return rowList.get(col);
}
}
public Object getData(int row, String colName){
if(row < 0 || row >= dataList.size()){
throw new ArrayIndexOutOfBoundsException("无效的参数,row=" + row);
} else{
int col = getColumnIndex(colName);
List rowList = (List)dataList.get(row);
return rowList.get(col);
}
}
public short getDataShort(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return 0;
if(obj instanceof Number)
return((Number)obj).shortValue();
else
return Short.parseShort(obj.toString());
}
public short getDataShort(int row, String colName){
int col = getColumnIndex(colName);
return getDataShort(row, col);
}
public byte getDataByte(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return 0;
if(obj instanceof Number)
return((Number)obj).byteValue();
else
return Byte.parseByte(obj.toString());
}
public byte getDataByte(int row, String colName){
int col = getColumnIndex(colName);
return getDataByte(row, col);
}
public int getDataInt(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return 0;
if(obj instanceof Number)
return((Number)obj).intValue();
else
return Integer.parseInt(obj.toString());
}
public int getDataInt(int row, String colName){
int col = getColumnIndex(colName);
return getDataInt(row, col);
}
public long getDataLong(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return 0L;
if(obj instanceof Number)
return((Number)obj).longValue();
else
return Long.parseLong(obj.toString());
}
public long getDataLong(int row, String colName){
int col = getColumnIndex(colName);
return getDataLong(row, col);
}
public float getDataFloat(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return 0.0F;
if(obj instanceof Number)
return((Number)obj).floatValue();
else
return Float.parseFloat(obj.toString());
}
public float getDataFloat(int row, String colName){
int col = getColumnIndex(colName);
return getDataFloat(row, col);
}
public double getDataDouble(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return 0.0D;
if(obj instanceof Number)
return((Number)obj).doubleValue();
else
return Double.parseDouble(obj.toString());
}
public double getDataDouble(int row, String colName){
int col = getColumnIndex(colName);
return getDataDouble(row, col);
}
public String getDataString(int row, int col){
Object obj = getData(row, col);
if(obj != null)
return obj.toString();
else
return null;
}
public String getDataString(int row, String colName){
int col = getColumnIndex(colName);
return getDataString(row, col);
}
public boolean getDataBoolean(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return false;
if(obj instanceof Boolean){
return((Boolean)obj).booleanValue();
} else{
String tmp = obj.toString().toLowerCase();
return "1".equals(tmp) || "on".equals(tmp) || "true".equals(tmp) || "yes".equals(tmp);
}
}
public boolean getDataBoolean(int row, String colName){
int col = getColumnIndex(colName);
return getDataBoolean(row, col);
}
public Date getDataDate(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return null;
if(obj instanceof Date)
return(Date)obj;
else
return Date.valueOf(obj.toString());
}
public Date getDataDate(int row, String colName){
int col = getColumnIndex(colName);
return getDataDate(row, col);
}
public Time getDataTime(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return null;
if(obj instanceof Time)
return(Time)obj;
else
return Time.valueOf(obj.toString());
}
public Time getDataTime(int row, String colName){
int col = getColumnIndex(colName);
return getDataTime(row, col);
}
public Timestamp getDataTimestamp(int row, int col){
Object obj = getData(row, col);
if(obj == null)
return null;
if(obj instanceof Timestamp)
return(Timestamp)obj;
else
return Timestamp.valueOf(obj.toString());
}
public Timestamp getDataTimestamp(int row, String colName){
int col = getColumnIndex(colName);
return getDataTimestamp(row, col);
}
public int getRowSize(){
return dataList.size();
}
public int getColumnSize(){
return colList.size();
}
public boolean isEmpty(){
return colList.size() == 0 || dataList.size() == 0;
}
public boolean containsColumn(String colName){
return colIdxMap.containsKey(colName.toUpperCase());
}
public int getColumnIndex(String colName){
if(colName == null || colName.length() == 0)
throw new IllegalArgumentException("无效的参数");
Integer col = (Integer)colIdxMap.get(colName.toUpperCase());
if(col == null)
throw new IllegalArgumentException("无效的字段名:" + colName);
else
return col.intValue();
}
public DBColumn getDBColumn(int col){
if(col < 0 || col >= colList.size()){
throw new IndexOutOfBoundsException("无效的参数,col=" + col);
} else{
DBColumn dbCol = (DBColumn)colList.get(col);
return dbCol;
}
}
public DBColumn getDBColumn(String colName){
int col = getColumnIndex(colName);
DBColumn dbCol = (DBColumn)colList.get(col);
return dbCol;
}
public List getRowList(int row){
if(row < 0 || row >= dataList.size())
throw new ArrayIndexOutOfBoundsException("无效的参数,row=" + row);
else
return(List)dataList.get(row);
}
public List getDBColumnList(){
return colList;
}
public List getDataList(){
return dataList;
}
void addColumn(DBColumn col){
if(col == null)
return;
String key = col.getKey();
if(key == null)
key = col.getName();
int idx = colList.size();
colList.add(col);
colIdxMap.put(key.toUpperCase(), new Integer(idx));
}
void addRowList(List list){
dataList.add(list);
}
void addData(int row, Object data){
if(row < 0 || row > dataList.size())
throw new IndexOutOfBoundsException("无效的参数,row=" + row);
List rowList = null;
if(row == dataList.size()){
rowList = new ArrayList();
dataList.add(rowList);
} else{
rowList = (List)dataList.get(row);
}
rowList.add(data);
}
void setColumn(int col, DBColumn dbCol){
if(col < 0 || col >= colList.size())
throw new IndexOutOfBoundsException("无效的参数,col=" + col);
if(dbCol == null){
throw new NullPointerException("DBColumn不能为空");
} else{
colList.set(col, dbCol);
return;
}
}
void setColumnList(List list){
colList = list;
}
void setDataList(List list){
dataList = list;
}
void setRowList(int row, List rowList){
if(row < 0 || row >= dataList.size())
throw new IndexOutOfBoundsException("无效的参数,row=" + row);
if(rowList == null){
throw new NullPointerException("RowList不能为空");
} else{
dataList.set(row, rowList);
return;
}
}
void setData(int row, int col, Object data){
if(row < 0 || col < 0 || row >= dataList.size() || col >= colList.size()){
throw new IndexOutOfBoundsException("无效的参数,row=" + row + ",col=" + col);
} else{
List rowList = (List)dataList.get(row);
rowList.set(col, data);
return;
}
}
void setData(int row, String colName, Object data){
if(row < 0 || row >= dataList.size()){
throw new ArrayIndexOutOfBoundsException("无效的参数,row=" + row);
} else{
int col = getColumnIndex(colName);
List rowList = (List)dataList.get(row);
rowList.set(col, data);
return;
}
}
void clearData(){
dataList.clear();
}
void clearColumn(){
colList.clear();
colIdxMap.clear();
}
void clearAll(){
colList.clear();
dataList.clear();
colIdxMap.clear();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -