resultrecord.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 397 行
JAVA
397 行
package cn.js.fan.db;import java.util.*;import java.sql.Timestamp;import java.math.BigDecimal;public class ResultRecord implements java.io.Serializable { Vector row; HashMap mapIndex; public ResultRecord(Vector row, HashMap mapIndex) { this.row = row; this.mapIndex = mapIndex; } public HashMap getMapIndex() { return 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."); } return getString(itg.intValue()); } public void put(String colname, Object value) { colname = colname.toUpperCase(); Integer index = (Integer) mapIndex.get(colname); if (index!=null) { set(index.intValue(), value); } else throw new IllegalArgumentException(colname + " is not exist."); } public void set(String colname, Object value) { Object col = get(colname); if (col!=null) { Class cls = col.getClass(); if (value!=null) { if (!cls.isInstance(value)) { if (col instanceof Timestamp) { if (!(value instanceof java.util.Date)) { throw new IllegalArgumentException("colname=" + colname + " class=" + value.getClass().getName() + " value=" + value + " is not of " + cls); } } else if (col instanceof Integer) { if (!(value instanceof Long)) { throw new IllegalArgumentException("colname=" + colname + " class=" + value.getClass().getName() + " value=" + value + " is not of " + cls); } } else if (col instanceof Long) { if (!(value instanceof Integer)) throw new IllegalArgumentException("colname=" + colname + " class=" + value.getClass().getName() + " value=" + value + " is not of " + cls); } else if (col instanceof java.sql.Date) { if (!(value instanceof java.util.Date)) { throw new IllegalArgumentException(colname + ":" + value.getClass() + ":" + value + " is not of " + cls); } } else if (col instanceof Double) { if (value instanceof Float || value instanceof Long || value instanceof Integer) ; else { throw new IllegalArgumentException(colname + ":" + value.getClass() + ":" + value + " is not of " + cls); } } else if (col instanceof Float) { if (value instanceof Double || value instanceof Long || value instanceof Integer) ; else { throw new IllegalArgumentException(colname + ":" + value.getClass() + ":" + value + " is not of " + cls); } } else { throw new IllegalArgumentException("colname=" + colname + " class=" + value.getClass().getName() + " value=" + 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 float getFloat(String colname) { colname = colname.toUpperCase(); Integer itg = (Integer)mapIndex.get(colname); if (itg==null) { throw new IllegalArgumentException(colname + " is not exist."); } return getFloat(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()); } public Object get(int i) { return row.elementAt(i-1); } public boolean getBoolean(int i) { boolean r = false; Object obj = row.elementAt(i - 1); try { r = ((String) obj).equals("1") || ((String) obj).equals("true"); } catch (ClassCastException e) { 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; } 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 Float) { k = ((Float)obj).doubleValue(); } else if (obj instanceof Integer) { k = ((Integer)obj).doubleValue(); } else if (obj instanceof Long) { k = ((Long)obj).doubleValue(); } else if (obj instanceof BigDecimal) { k = ((BigDecimal)obj).doubleValue(); } else if (obj instanceof String) { try { return Double.parseDouble((String) obj); } catch (NumberFormatException e1) { System.out.println(getClass().getName() + " getInt:" + e1.getMessage()); e.printStackTrace(); } } else { throw new ClassCastException("obj=" + obj + " class=" + obj.getClass()); } } return k; } public float getFloat(int i) { Object obj = row.elementAt(i - 1); float k = 0; try { k = ((Float) obj).floatValue(); } catch (ClassCastException e) { if (obj instanceof Double) { k = ((Double)obj).floatValue(); } else if (obj instanceof Integer) { k = ((Integer)obj).floatValue(); } else if (obj instanceof Long) { k = ((Long)obj).floatValue(); } else if (obj instanceof BigDecimal) { k = ((BigDecimal)obj).floatValue(); } else if (obj instanceof String) { try { return Float.parseFloat((String) obj); } catch (NumberFormatException e1) { System.out.println(this.getClass().getName() + " getFloat:" + e1.getMessage()); e.printStackTrace(); } } else { throw new ClassCastException("getFloat: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 Double) { return ((Double) 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 Float) { return ((Float) obj).longValue(); } else if (obj instanceof Double) { return ((Double) obj).longValue(); } 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 + =
减小字号Ctrl + -
显示快捷键?