⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stresultset.java

📁 一套完整的档案管理系统
💻 JAVA
字号:


package com.stsc.util;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Hashtable;
import java.util.Vector;

// Referenced classes of package com.stsc.util:
//            STLog

public class STResultSet
{

    Vector result;
    int colType[];
    String colName[];
    protected int columnCount;
    protected int current;
    protected int recCount;

    public int getColumnType(int i)
    {
        if(i > 0 && i <= columnCount)
            return colType[i - 1];
        else
            return -1;
    }

    public String getColumnName(int i)
    {
        if(i > 0 && i <= columnCount)
            return colName[i - 1];
        else
            return "";
    }

    public int getColumnCount()
    {
        return columnCount;
    }

    private void log(String s)
    {
        STLog.debug(s);
    }

    public STResultSet(ResultSet resultset)
    {
        result = new Vector();
        recCount = 0;
        current = -1;
        if(resultset == null)
            return;
        try
        {
            ResultSetMetaData resultsetmetadata = resultset.getMetaData();
            columnCount = resultsetmetadata.getColumnCount();
            colType = new int[columnCount];
            colName = new String[columnCount];
            for(int i = 1; i <= columnCount; i++)
            {
                colType[i - 1] = resultsetmetadata.getColumnType(i);
                colName[i - 1] = resultsetmetadata.getColumnName(i).toUpperCase();
            }

            Hashtable hashtable;
            for(; resultset != null && resultset.next(); result.addElement(hashtable))
            {
                recCount++;
                hashtable = new Hashtable();
                for(int j = 1; j <= columnCount; j++)
                {
                    int k = colType[j - 1];
                    if(k == 5 || k == 4 || k == -6 || k == -5)
                        hashtable.put(colName[j - 1], new Integer(resultset.getInt(j)));
                    else
                    if(k == 6 || k == 2 || k == 8 || k == 3 || k == 7)
                    {
                        BigDecimal bigdecimal;
                        if(System.getProperty("java.version").compareTo("1.2") < 0)
                            bigdecimal = resultset.getBigDecimal(j, 0);
                        else
                            bigdecimal = resultset.getBigDecimal(j);
                        if(bigdecimal != null)
                            hashtable.put(colName[j - 1], bigdecimal);
                    } else
                    if(k == 91 || k == 92 || k == 93)
                    {
                        Date date = resultset.getDate(j);
                        if(date != null)
                            hashtable.put(colName[j - 1], date);
                    } else
                    if(k == 1 || k == 12 || k == -1)
                    {
                        String s = resultset.getString(j);
                        if(s != null)
                            hashtable.put(colName[j - 1], s);
                    } else
                    if(k == 2004)
                    {
                        Blob blob = resultset.getBlob(j);
                        if(blob != null)
                            hashtable.put(colName[j - 1], blob);
                    } else
                    if(k == 2005)
                    {
                        Clob clob = resultset.getClob(j);
                        if(clob != null)
                            hashtable.put(colName[j - 1], clob);
                    } else
                    {
                        throw new Exception("不可用的字段格式.\n");
                    }
                }

            }

        }
        catch(Exception exception)
        {
            recCount = 0;
            log("STResultSet.STResultSet(ResultSet) error:" + exception);
        }
    }

    public boolean isEmpty()
    {
        return recCount == 0;
    }

    public boolean next()
    {
        if(current + 1 >= recCount)
        {
            return false;
        } else
        {
            current++;
            return true;
        }
    }

    public boolean previous()
    {
        if(current - 1 <= -1)
        {
            return false;
        } else
        {
            current--;
            return true;
        }
    }

    public boolean first()
    {
        if(recCount == 0)
        {
            return false;
        } else
        {
            current = 0;
            return true;
        }
    }

    public boolean top()
    {
        if(recCount == 0)
        {
            return false;
        } else
        {
            current = -1;
            return true;
        }
    }

    public boolean last()
    {
        if(recCount == 0)
        {
            return false;
        } else
        {
            current = recCount;
            return true;
        }
    }

    public boolean go(int i)
    {
        if(i >= 0 && i < recCount)
        {
            current = i;
            return true;
        } else
        {
            return false;
        }
    }

    public int findColumn(String s)
    {
        int i;
        for(i = 0; i < colName.length; i++)
            if(colName[i].equals(s.toUpperCase()))
                break;

        if(i == colName.length)
            return -1;
        else
            return i + 1;
    }

    public int getRecCount()
    {
        if(recCount >= 0)
            return recCount;
        else
            return 0;
    }

    public Object getObject(int i)
    {
        if(i > 0 && i <= colName.length)
            return getObject(colName[i - 1]);
        else
            return null;
    }

    public Object getObject(String s)
    {
        try
        {
            if(recCount == 0)
                throw new Exception("数据集为空。");
            if(current == -1)
                throw new Exception("未调用next()");
            if(findColumn(s) < 0)
            {
                throw new Exception("无效列名" + s);
            } else
            {
                Hashtable hashtable = (Hashtable)result.elementAt(current);
                return hashtable.get(s.toUpperCase());
            }
        }
        catch(Exception exception)
        {
            log("STResultSet.getObject(String) error:" + exception);
        }
        return null;
    }

    public String getString(int i)
    {
        return getString(colName[i - 1]);
    }

    public String getString(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            if(obj instanceof String)
            {
                return (String)obj;
            } else
            {
                log("STResultSet.getString() error: 字段类型不匹配。");
                return "";
            }
        } else
        {
            return "";
        }
    }

    public Date getDate(int i)
    {
        return getDate(colName[i - 1]);
    }

    public Date getDate(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            if(obj instanceof Date)
            {
                return (Date)obj;
            } else
            {
                log("STResultSet.getDate() error: 字段类型不匹配。");
                return null;
            }
        } else
        {
            return null;
        }
    }

    public BigDecimal getBigDecimal(int i)
    {
        return getBigDecimal(colName[i - 1]);
    }

    public BigDecimal getBigDecimal(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            if(obj instanceof BigDecimal)
            {
                return (BigDecimal)obj;
            } else
            {
                log("STResultSet.getBigDecimal() error: 字段类型不匹配。");
                return null;
            }
        } else
        {
            return null;
        }
    }

    public Blob getBlob(int i)
    {
        return getBlob(colName[i - 1]);
    }

    public Blob getBlob(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            if(obj instanceof Blob)
            {
                return (Blob)obj;
            } else
            {
                log("STResultSet.getBlob() error: 字段类型不匹配。");
                return null;
            }
        } else
        {
            return null;
        }
    }

    public Clob getClob(int i)
    {
        return getClob(colName[i - 1]);
    }

    public Clob getClob(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            if(obj instanceof Clob)
            {
                return (Clob)obj;
            } else
            {
                log("STResultSet.getClob() error: 字段类型不匹配。");
                return null;
            }
        } else
        {
            return null;
        }
    }

    public int getInt(int i)
    {
        return getInt(colName[i - 1]);
    }

    public int getInt(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            int i;
            if(obj instanceof BigDecimal)
                i = ((BigDecimal)obj).intValue();
            else
            if(obj instanceof Double)
                i = ((Double)obj).intValue();
            else
            if(obj instanceof Integer)
            {
                i = ((Integer)obj).intValue();
            } else
            {
                log("STResultSet.getInt() error: 字段类型不匹配。");
                return 0;
            }
            return i;
        } else
        {
            return 0;
        }
    }

    public double getDouble(int i)
    {
        return getDouble(colName[i - 1]);
    }

    public double getDouble(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            double d;
            if(obj instanceof BigDecimal)
                d = ((BigDecimal)obj).doubleValue();
            else
            if(obj instanceof Double)
                d = ((Double)obj).doubleValue();
            else
            if(obj instanceof Integer)
            {
                d = ((Integer)obj).doubleValue();
            } else
            {
                log("STResultSet.getDouble() error: 字段类型不匹配。");
                return 0.0D;
            }
            return d;
        } else
        {
            return 0.0D;
        }
    }

    public float getFloat(int i)
    {
        return getFloat(colName[i - 1]);
    }

    public float getFloat(String s)
    {
        Object obj = getObject(s);
        if(obj != null)
        {
            float f;
            if(obj instanceof BigDecimal)
                f = ((BigDecimal)obj).floatValue();
            else
            if(obj instanceof Double)
                f = ((Double)obj).floatValue();
            else
            if(obj instanceof Integer)
            {
                f = ((Integer)obj).floatValue();
            } else
            {
                log("STResultSet.getFloat() error: 字段类型不匹配。");
                return 0.0F;
            }
            return f;
        } else
        {
            return 0.0F;
        }
    }

    public boolean locate(Vector vector, Vector vector1)
    {
        if(vector == null || vector.size() == 0)
        {
            log("STResultSet::locate()错误 : 提供的字段列表为空。");
            return false;
        }
        if(vector1 == null || vector1.size() == 0)
        {
            log("STResultSet::locate()错误 : 提供的值列表为空。");
            return false;
        }
        if(vector.size() != vector1.size())
        {
            log("STResultSet::locate()错误 : 提供的字段列表和值列表不匹配。");
            return false;
        }
        int ai[] = new int[vector.size()];
        for(int i = 0; i < vector.size(); i++)
        {
            int j = findColumn((String)vector.elementAt(i));
            if(j < 0)
            {
                log("STResultSet::locate()错误 : 在数据集中未找到指定的字段名" + vector.elementAt(i));
                return false;
            }
            ai[i] = getColumnType(j);
            if(ai[i] == 5 || ai[i] == 4 || ai[i] == -6 || ai[i] == -5)
            {
                if(!(vector1.elementAt(i) instanceof Integer))
                {
                    log("STResultSet::locate()错误 : 给出的值类型与数据集的字段类型不匹配。");
                    return false;
                }
            } else
            if(ai[i] == 6 || ai[i] == 2 || ai[i] == 8 || ai[i] == 3 || ai[i] == 7)
            {
                if(!(vector1.elementAt(i) instanceof Double))
                {
                    log("STResultSet::locate()错误 : 给出的值类型与数据集的字段类型不匹配。");
                    return false;
                }
            } else
            if(ai[i] == 1 || ai[i] == 12 || ai[i] == -1)
            {
                if(!(vector1.elementAt(i) instanceof String))
                {
                    log("STResultSet::locate()错误 : 给出的值类型与数据集的字段类型不匹配。");
                    return false;
                }
            } else
            {
                log("STResultSet::locate()错误 : 指定的字段类型无法执行搜索。");
                return false;
            }
        }

        top();
        while(next()) 
        {
            int k;
            for(k = 0; k < vector.size(); k++)
                if(ai[k] != 5 && ai[k] != 4 && ai[k] != -6 && ai[k] != -5 ? ai[k] != 6 && ai[k] != 2 && ai[k] != 8 && ai[k] != 3 && ai[k] != 7 ? ai[k] != 1 && ai[k] != 12 && ai[k] != -1 || ((String)vector1.elementAt(k)).compareTo(getString(vector.elementAt(k).toString())) != 0 : ((BigDecimal)vector1.elementAt(k)).floatValue() != getFloat(vector.elementAt(k).toString()) : ((Integer)vector1.elementAt(k)).intValue() != getInt(vector.elementAt(k).toString()))
                    break;

            if(k == vector.size())
                return true;
        }
        return false;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -