📄 resultrecord.java~2~
字号:
package cn.js.fan.db;
import java.util.*;
import org.apache.log4j.Logger;
import java.sql.Timestamp;
/**
* 存储一行数据
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ResultRecord implements java.io.Serializable {
Vector row;
HashMap mapIndex;
public ResultRecord(Vector row, HashMap mapIndex) {
this.row = row;
this.mapIndex = mapIndex;
}
public Object get(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
return get(itg.intValue());
}
public String getString(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
// System.out.println(this.getClass().getName() + " colname=" + colname + " mapIndex=" + mapIndex);
return getString(itg.intValue());
}
public void set(String colname, Object value) {
// 类型检查
Object col = get(colname);
if (col!=null) {
Class cls = col.getClass();
if (!cls.isInstance(value)) {
if (col instanceof Long) {
if (!(value instanceof Integer))
throw new IllegalArgumentException(value.getClass() + ":" +
value + " is not of " + cls);
}
else
throw new IllegalArgumentException(value.getClass() + ":" +
value + " is not of " + cls);
}
}
colname = colname.toUpperCase();
set(((Integer)mapIndex.get(colname)).intValue(), value);
}
public int getInt(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
return getInt(itg.intValue());
}
public long getLong(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
return getLong(itg.intValue());
}
public double getDouble(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
return getDouble(itg.intValue());
}
public Date getDate(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
Date d = null;
try {
d = getDate(itg.intValue());
}
catch (Exception e) {
e.printStackTrace();
}
return d;
}
public Timestamp getTimestamp(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
Timestamp d = null;
try {
d = getTimestamp(itg.intValue());
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
public boolean getBoolean(String colname) {
colname = colname.toUpperCase();
Integer itg = (Integer)mapIndex.get(colname);
if (itg==null) {
throw new IllegalArgumentException(colname + " is not exist.");
}
return getBoolean(itg.intValue());
}
/**
*
* @param i int i从1开始
* @return Object
*/
public Object get(int i) {
return row.elementAt(i-1);
}
public boolean getBoolean(int i) {
Boolean r = new Boolean(false);
Object obj = row.elementAt(i - 1);
try {
// logger.info("obj=" + obj);
r = new Boolean(((String) obj).equals("1") || ((String) obj).equals("true"));
} catch (ClassCastException e) {
// if (e.getMessage().equals("java.lang.Integer"))
// return ((Integer) obj).intValue()>0?true:false;
if (obj instanceof Integer) {
return ((Integer)obj).intValue()==1;
}
else if (obj instanceof Long) {
return ((Long)obj).longValue()==1;
}
else if (obj instanceof Boolean) {
return ((Boolean) obj).booleanValue();
} else {
throw new ClassCastException("obj = " + obj + " class=" + obj.getClass());
}
}
return r.booleanValue();
}
public double getDouble(int i) {
Object obj = row.elementAt(i - 1);
double k = 0;
try {
k = ((Double) obj).doubleValue();
} catch (ClassCastException e) {
if (obj instanceof String) {
try {
return Double.parseDouble((String) obj);
} catch (NumberFormatException e1) {
System.out.println(this.getClass().getName() + " getInt:" +
e1.getMessage());
e.printStackTrace();
}
}
else {
throw new ClassCastException("obj = " + obj + " class=" + obj.getClass());
}
}
return k;
}
public void set(int i, Object value) {
row.set(i-1, value);
}
public String getString(int i) {
String str = "";
Object obj = row.elementAt(i - 1);
try {
str = (String)obj;
}
catch (ClassCastException e) {
str = obj.toString();
}
return str;
}
public int getInt(int i) {
Object obj = row.elementAt(i - 1);
int k = -1;
if (obj==null)
return k;
try {
k = ((Integer) obj).intValue();
} catch (ClassCastException e) {
if (obj instanceof Long) {
return ((Long) obj).intValue();
} else if (obj instanceof String) {
try {
return Integer.parseInt((String) obj);
} catch (NumberFormatException e1) {
System.out.println(this.getClass().getName() + " getInt:" +
e1.getMessage());
e1.printStackTrace();
}
} else {
throw new ClassCastException("obj = " + obj + " class=" + obj.getClass());
}
}
return k;
}
public long getLong(int i) {
Object obj = row.elementAt(i - 1);
long k = -1;
if (obj==null)
return k;
try {
k = ((Long) obj).longValue();
} catch (ClassCastException e) {
if (obj instanceof Integer) {
return ((Integer) obj).intValue();
} else if (obj instanceof String) {
try {
return Long.parseLong((String) obj);
} catch (NumberFormatException e1) {
throw new ClassCastException("obj = " + obj + " class=" + obj.getClass());
}
} else {
throw new ClassCastException("obj = " + obj + " class=" + obj.getClass());
}
}
return k;
}
public Date getDate(int i) {
Object obj = row.elementAt(i - 1);
Date d = null;
try {
d = (Date) obj;
} catch (ClassCastException e) {
System.out.println(this.getClass().getName() + " getDate:" +
e.getMessage());
e.printStackTrace();
}
return d;
}
public Timestamp getTimestamp(int i) {
Object obj = row.elementAt(i - 1);
Timestamp d = null;
try {
d = (Timestamp) obj;
} catch (ClassCastException e) {
System.out.println(this.getClass().getName() + " getTimestamp:" +
e.getMessage());
e.printStackTrace();
}
return d;
}
public Vector getRow() {
return row;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -