Java SQL Type getSqlValue(Object o)

Here you can find the source of getSqlValue(Object o)

Description

get Sql Value

License

Open Source License

Declaration

@SuppressWarnings("rawtypes")
    protected static String getSqlValue(Object o) 

Method Source Code

//package com.java2s;
/* /*from ww  w  . j a v a2 s.  co m*/
 * CapDbUtil.java
 * 
 * Copyright (c) 2009-2012 International Integrated System, Inc. 
 * All Rights Reserved.
 * 
 * Licensed Materials - Property of International Integrated System, Inc.
 * 
 * This software is confidential and proprietary information of 
 * International Integrated System, Inc. ("Confidential Information").
 */

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collection;

public class Main {
    @SuppressWarnings("rawtypes")
    protected static String getSqlValue(Object o) {
        String rtn = null;
        if (o == null) {
            rtn = null;
        } else {
            if (o instanceof String) {
                rtn = (String) o;
            } else if (o instanceof Number || o instanceof BigDecimal
                    || o instanceof StringBuffer) {
                rtn = o.toString();
            } else if (o instanceof Timestamp) {
                rtn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS")
                        .format(o);
            } else if (o instanceof Time) {
                rtn = new SimpleDateFormat("HH:mm:ss").format(o);
            } else if (o instanceof Date) {
                rtn = new SimpleDateFormat("yyyy-MM-dd").format(o);
            } else if (o instanceof Collection) {
                rtn = getSqlValue(((Collection) o).toArray());
            } else if (o.getClass().isArray()) {
                if (Array.getLength(o) > 0) {
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < Array.getLength(o); ++i) {
                        sb.append(i == 0 ? "" : "'")
                                .append(Array.get(o, i)).append("',");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    sb.deleteCharAt(sb.length() - 1);
                    rtn = sb.toString();
                } else {
                    rtn = "empty?";
                }
            } else {
                rtn = "?";
            }
        }
        return rtn;
    }
}

Related

  1. getSQLTypeName(int sqlType)
  2. getSqlTypeName(int type)
  3. getSQLTypeName(Integer sqltypecode)
  4. getSQLTypePrecision(int sqlType)
  5. getSQLTypeRadix(int sqlType)
  6. getStringFormat(int colType)
  7. getTableTypes(DatabaseMetaData dbmd)
  8. getTinyIntTypeString(Connection conn)
  9. getType(int csqltype)