Java SQL Type readObject(DataInput in, int sqlType)

Here you can find the source of readObject(DataInput in, int sqlType)

Description

read Object

License

Apache License

Declaration

public static Object readObject(DataInput in, int sqlType) throws IOException 

Method Source Code


//package com.java2s;
/*//  w  ww. j a va  2  s.c o  m
 * Copyright 2013-2015 Makoto YUI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.DataInput;

import java.io.IOException;
import java.sql.Timestamp;
import java.sql.Types;

import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static Object readObject(DataInput in, int sqlType) throws IOException {
        switch (sqlType) {
        case Types.VARCHAR:
            return in.readUTF();
        case Types.FLOAT:
            return Float.valueOf(in.readFloat());
        case Types.DOUBLE:
            return Double.valueOf(in.readDouble());
        case Types.BOOLEAN:
            return Boolean.valueOf(in.readBoolean());
        case Types.TINYINT:
            return Byte.valueOf(in.readByte());
        case Types.SMALLINT:
            return Short.valueOf(in.readShort());
        case Types.INTEGER:
            return Integer.valueOf(in.readInt());
        case Types.BIGINT:
            return Long.valueOf(in.readLong());
        case Types.TIMESTAMP: {
            long time = in.readLong();
            return new Timestamp(time);
        }
        case Types.BINARY: {
            int size = in.readInt();
            byte[] b = new byte[size];
            in.readFully(b);
            return b;
        }
        case Types.ARRAY: {
            int size = in.readInt();
            if (size == 0) {
                return Collections.emptyList();
            }
            int elemType = in.readInt();
            Object[] a = new Object[size];
            for (int i = 0; i < size; i++) {
                Object o = readObject(in, elemType);
                a[i] = o;
            }
            return Arrays.asList(a);
        }
        default:
            throw new IOException("Cannot read Object for type: " + sqlType);
        }
    }
}

Related

  1. newInstance(Class clazz, Object... params)
  2. parse(String txt, int level)
  3. prettyPrint(Object obj)
  4. readAscii(Clob clob, String defaultValue)
  5. readMapListBySQL(String fileDir, String sql, List headers, List headerTypes)
  6. resolveEscapes(String escaped, boolean noBackslashEscapes)
  7. setFieldValue(Object oBean, String sFieldName, Object oValue)
  8. setProperty(Object object, String name, String value)
  9. sqlType2Name(int sqlType)