FileDocCategorySizeDatePackage
CommonValue.javaAPI DocExample5570Sat Feb 03 11:43:40 GMT 2001jdbc.SimpleText

CommonValue

public class CommonValue extends Object

Fields Summary
protected Object
data
protected int
internalType
Constructors Summary
public CommonValue()

        data = null;
    
public CommonValue(String s)

        data = (Object) s;
        internalType = Types.VARCHAR;
    
public CommonValue(int i)

        data = (Object) new Integer(i);
        internalType = Types.INTEGER;
    
public CommonValue(Integer i)

        data = (Object) i;
        internalType = Types.INTEGER;
    
public CommonValue(byte[] b)

        data = (Object) b;
        internalType = Types.VARBINARY;
    
Methods Summary
public byte[]getBytes()

        byte b[] = null;

        // A null value always returns null

        if (data == null) {
            return null;
        }

        switch(internalType) {

        case Types.VARCHAR:
            {
                // Convert the String into a byte array.  The String must
                // contain an even number of hex digits

                String s = ((String) data).toUpperCase();
                String digits = "0123456789ABCDEF";
                int len = s.length();
                int index;

                if ((len % 2) != 0) {
                    throw new SQLException(
                            "Data must have a even number of hex digits");
                }

                b = new byte[len / 2];

                for (int i = 0; i < (len / 2); i++) {
                    index = digits.indexOf(s.charAt(i * 2));

                    if (index < 0) {
                        throw new SQLException("Invalid hex digit");
                    }

                    b[i] = (byte) (index << 4);

                    index = digits.indexOf(s.charAt((i * 2) + 1));

                    if (index < 0) {
                        throw new SQLException("Invalid hex digit");
                    }

                    b[i] += (byte) index;

                }
            }
            break;

        case Types.VARBINARY:
            b = (byte[]) data;
            break;

        default:
            throw new SQLException("Unable to convert data type to byte[]: " +
                                internalType);
        }

        return b;
    
public intgetInt()

        int i = 0;

        // A null value always returns zero

        if (data == null) {
            return 0;
        }

        switch(internalType) {

        case Types.VARCHAR:
            i = (Integer.valueOf((String) data)).intValue();
            break;

        case Types.INTEGER:
            i = ((Integer) data).intValue();
            break;

        default:
            throw new SQLException("Unable to convert data type to String: " +
                                internalType);
        }

        return i;
    
public java.lang.StringgetString()

        String s;

        // A null value always returns null

        if (data == null) {
            return null;
        }

        switch(internalType) {

        case Types.VARCHAR:
            s = (String) data;
            break;

        case Types.INTEGER:
            s = ((Integer) data).toString();
            break;

        case Types.VARBINARY:
            {
                // Convert a byte array into a String of hex digits

                byte b[] = (byte[]) data;
                int len = b.length;
                String digits = "0123456789ABCDEF";
                char c[] = new char[len * 2];

                for (int i = 0; i < len; i++) {
                    c[i * 2] = digits.charAt((b[i] >> 4) & 0x0F);
                    c[(i * 2) + 1] = digits.charAt(b[i] & 0x0F);
                }
                s = new String(c);
            }


            break;

        default:
            throw new SQLException("Unable to convert data type to String: " +
                                internalType);
        }

        return s;
    
public booleanisNull()

        return (data == null);