Java SQL Type writeObject(Object obj, int sqlType, DataOutput out)

Here you can find the source of writeObject(Object obj, int sqlType, DataOutput out)

Description

write Object

License

Apache License

Declaration

public static void writeObject(Object obj, int sqlType, DataOutput out) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w  ww  . j a v a2s  .  com
 * 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.DataOutput;
import java.io.IOException;
import java.sql.Timestamp;
import java.sql.Types;

import java.util.List;

public class Main {
    public static void writeObject(Object obj, int sqlType, DataOutput out) throws IOException {
        switch (sqlType) {
        case Types.VARCHAR: {
            String s = obj.toString();
            out.writeUTF(s);
            return;
        }
        case Types.FLOAT: {
            Float f = (Float) obj;
            out.writeFloat(f.floatValue());
            return;
        }
        case Types.DOUBLE: {
            Double d = (Double) obj;
            out.writeDouble(d.doubleValue());
            return;
        }
        case Types.BOOLEAN: {
            Boolean b = (Boolean) obj;
            out.writeBoolean(b.booleanValue());
            return;
        }
        case Types.TINYINT: {
            Byte b = (Byte) obj;
            out.writeByte(b.intValue());
            return;
        }
        case Types.SMALLINT: {
            Short s = (Short) obj;
            out.writeShort(s.shortValue());
            return;
        }
        case Types.INTEGER: {
            Integer i = (Integer) obj;
            out.writeInt(i.intValue());
            return;
        }
        case Types.BIGINT: {
            Long l = (Long) obj;
            out.writeLong(l.longValue());
            return;
        }
        case Types.TIMESTAMP: {
            Timestamp time = (Timestamp) obj;
            out.writeLong(time.getTime());
            return;
        }
        case Types.BINARY: {
            byte[] b = (byte[]) obj;
            out.writeInt(b.length);
            out.write(b);
            return;
        }
        case Types.ARRAY: {
            List<?> list = (List<?>) obj;
            int size = list.size();
            out.writeInt(size);
            if (size > 0) {
                Object firstElem = list.get(0);
                Class<?> clazz = firstElem.getClass();
                int elemSqlType = toSqlType(clazz);
                out.writeInt(elemSqlType);
                for (Object e : list) {
                    writeObject(e, elemSqlType, out);
                }
            }
            return;
        }
        default:
            throw new IOException(
                    "Cannot write Object '" + obj.getClass().getSimpleName() + "' as type: " + sqlType);
        }
    }

    public static int toSqlType(Class<?> clazz) throws IOException {
        if (clazz == String.class) {
            return Types.VARCHAR;
        }
        if (clazz == Float.class) {
            return Types.FLOAT;
        }
        if (clazz == Double.class) {
            return Types.DOUBLE;
        }
        if (clazz == Boolean.class) {
            return Types.BOOLEAN;
        }
        if (clazz == Byte.class) {
            return Types.TINYINT;
        }
        if (clazz == Short.class) {
            return Types.SMALLINT;
        }
        if (clazz == Integer.class) {
            return Types.INTEGER;
        }
        if (clazz == Long.class) {
            return Types.BIGINT;
        }
        if (clazz == Timestamp.class) {
            return Types.TIMESTAMP;
        }
        if (clazz == byte[].class) {
            return Types.BINARY;
        }
        if (clazz.isArray()) {
            return Types.ARRAY;
        }
        throw new IOException("Cannot resolve SqlType for + " + clazz.getName());
    }
}

Related

  1. translateType(int sqlType)
  2. typeName(int type)
  3. typeNameToValueType(String typeName)
  4. typeToClass(int type)
  5. typeToString(int sqlType)