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

📄 jmdboresultsetapc.java

📁 梦界家园程序开发基底框架
💻 JAVA
字号:
package jm.dbo;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import jm.util.JM2DArray;
import jm.util.JMVal;
import java.util.ArrayList;
import jm.framework.gui.JMEntity;
import jm.util.JMVector;
import java.lang.reflect.Method;
import jm.util.JMUtility;
import jm.util.JMMap;

/**
 * DBApc
 * @author fu_changyong(1shome)
 * @see jdk1.5.2
 * @version 1.5.2.2
 */
public class JMDBOResultSetApc {
    public static final String CLASS_NAME = "ResultSetApc";
    /**
     * 俢俛専嶕寢壥傪庢摼偟 JM2DArray 僆僽僕僃僋僩偵奿擺偟偰曉偡丅<BR>
     * 専嶕寢壥偺奺儗僐乕僪偼 JM2DArray 偺侾峴偵奿擺偝傟傞丅奺僇儔儉偺抣偼
     * 俽俻俴僨乕僞宆偵懳墳偟偨崁栚抣偵曄姺偟偰 JM2DArray 偵奿擺偝傟傞丅
     * @param ret ResultSet
     * @return JM2DArray
     * @throws Exception
     */
    public static synchronized JM2DArray getResultData (ResultSet ret) throws Exception {

        // 専嶕寢壥傪庢傝崬傓
        ResultSetMetaData oMeta = ret.getMetaData();
        int iColCount = oMeta.getColumnCount();
        JM2DArray oResult = new JM2DArray("DBResult", true);
        if (ret == null)
            return oResult;
        while (ret.next()) {
            for (int iCol = 1; iCol <= iColCount; iCol++) {
                // 僇儔儉柤傪庢摼
                String sColName = oMeta.getColumnName(iCol);
                int iType = oMeta.getColumnType(iCol);
                if (iType == Types.LONGVARCHAR ||
                    iType == Types.VARCHAR ||
                    iType == Types.CHAR) {
                    String sValue = ret.getString(iCol);
                    oResult.addItem(sColName, sValue);
                } else if (iType == Types.NUMERIC ||
                           iType == Types.DECIMAL) {
                    String sValue = ret.getString(iCol);
                    oResult.addItem(sColName, new BigDecimal(sValue));
                } else if (iType == Types.BOOLEAN) {
                    boolean bValue = ret.getBoolean(iCol);
                    if (bValue || !ret.wasNull())
                        oResult.addItem(sColName, bValue);
                    else
                        oResult.addItem(sColName, JMVal.BOOLEAN_NULL);
                } else if (iType == Types.TINYINT) {
                    byte yValue = ret.getByte(iCol);
                    if (yValue != (byte) 0 || !ret.wasNull())
                        oResult.addItem(sColName, yValue);
                    else
                        oResult.addItem(sColName, JMVal.BYTE_NULL);
                } else if (iType == Types.SMALLINT) {
                    short hValue = ret.getShort(iCol);
                    if (hValue != (short) 0 || !ret.wasNull())
                        oResult.addItem(sColName, hValue);
                    else
                        oResult.addItem(sColName, JMVal.SHORT_NULL);
                } else if (iType == Types.INTEGER) {
                    int iValue = ret.getInt(iCol);
                    if (iValue != 0 || !ret.wasNull())
                        oResult.addItem(sColName, iValue);
                    else
                        oResult.addItem(sColName, JMVal.INT_NULL);
                } else if (iType == Types.BIGINT) {
                    long lValue = ret.getLong(iCol);
                    if (lValue != 0L || !ret.wasNull())
                        oResult.addItem(sColName, lValue);
                    else
                        oResult.addItem(sColName, JMVal.LONG_NULL);
                } else if (iType == Types.DOUBLE) {
                    double dValue = ret.getDouble(iCol);
                    if (dValue != 0.0D || !ret.wasNull())
                        oResult.addItem(sColName, dValue);
                    else
                        oResult.addItem(sColName, JMVal.DOUBLE_NULL);
                } else if (iType == Types.FLOAT || iType == Types.REAL) {
                    float fValue = ret.getFloat(iCol);
                    if (fValue != 0.0F || !ret.wasNull())
                        oResult.addItem(sColName, fValue);
                    else
                        oResult.addItem(sColName, JMVal.FLOAT_NULL);
                } else if (iType == Types.TIMESTAMP) {
                    Timestamp oTstmp = ret.getTimestamp(iCol);
                    if (oTstmp != null)
                        oResult.addItem(sColName,
                                        new java.util.Date(oTstmp.getTime()));
                    else
                        oResult.addItem(sColName, (java.sql.Timestamp)null);
                } else if (iType == Types.DATE) {
                    java.util.Date oDate = ret.getDate(iCol);
                    if (oDate != null)
                        oResult.addItem(sColName,
                                        new java.util.Date(oDate.getTime()));
                    else
                        oResult.addItem(sColName, (java.util.Date)null);
                } else if (iType == Types.TIME) {
                    Time oTime = ret.getTime(iCol);
                    if (oTime != null)
                        oResult.addItem(sColName,
                                        new java.util.Date(oTime.getTime()));
                    else
                        oResult.addItem(sColName, (Time)null);
                } else if (iType == Types.BINARY ||
                           iType == Types.VARBINARY) {
                    byte[] yValues = ret.getBytes(iCol);
                    oResult.addItem(sColName, yValues);
                } else {
                    String sValue = ret.getString(iCol);
                    oResult.addItem(sColName, sValue);
                }
            }
            oResult.nextRow();
        }
        return oResult;
    }

    public static synchronized ArrayList<JMEntity> getResultListData (ResultSet ret,String tableName) throws Exception {

        JMEntity inClass = null;
        JMMap<String,String> data = null;
        // 専嶕寢壥傪庢傝崬傓
        ResultSetMetaData oMeta = ret.getMetaData();
        int iColCount = oMeta.getColumnCount();
        ArrayList<JMEntity> oResult = new ArrayList<JMEntity>(50);
        if (ret == null)
            return oResult;
        while (ret.next()) {
            inClass = (JMEntity)Class.forName(tableName).newInstance();
            data = new JMMap<String,String>();
            //获得单行数据
            for (int iCol = 1; iCol <= iColCount; iCol++) {
                data.put(oMeta.getColumnName(iCol),ret.getString(iCol));
            }
            //保存到Entity
            {
                JMUtility.reflectIntoClass(inClass,data);
                oResult.add(inClass);
            }
        }
        return oResult;
    }

}

⌨️ 快捷键说明

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