Java SQL Type prettyPrint(Object obj)

Here you can find the source of prettyPrint(Object obj)

Description

Helper method

License

Apache License

Parameter

Parameter Description
obj item to print

Return

String for pretty printing.

Declaration

protected static String prettyPrint(Object obj) 

Method Source Code

//package com.java2s;
/**//from w  ww  .j  av a2  s . c om
 *  Copyright 2010 Wallace Wadge
 *
 *    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.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Ref;
import java.sql.SQLException;
import java.util.Date;

public class Main {
    /** Helper method
     * @param obj item to print
     * @return String for pretty printing.
     */
    protected static String prettyPrint(Object obj) {
        StringBuilder sb = new StringBuilder();
        if (obj == null) {
            sb.append("NULL");
        } else if (obj instanceof Blob) {
            sb.append(formatLogParam((Blob) obj));
        } else if (obj instanceof Clob) {
            sb.append(formatLogParam((Clob) obj));
        } else if (obj instanceof Ref) {
            sb.append(formatLogParam((Ref) obj));
        } else if (obj instanceof Array) {
            sb.append(formatLogParam((Array) obj));
        } else if (obj instanceof String || obj instanceof Date) {
            sb.append("'" + obj.toString() + "'");
        } else {
            sb.append(obj.toString());
        }
        return sb.toString();
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Blob obj) {
        String result = "";
        try {
            result = "(blob of length " + obj.length() + ")";
        } catch (SQLException e) {
            result = "(blob of unknown length)";
        }
        return result;
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Clob obj) {
        String result = "";
        try {
            result = "(cblob of length " + obj.length() + ")";
        } catch (SQLException e) {
            result = "(cblob of unknown length)";
        }
        return result;
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Array obj) {
        String result = "";
        try {
            result = "(array of type" + obj.getBaseTypeName().length() + ")";
        } catch (SQLException e) {
            result = "(array of unknown type)";
        }
        return result;
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Ref obj) {
        String result = "";
        try {
            result = "(ref of type" + obj.getBaseTypeName().length() + ")";
        } catch (SQLException e) {
            result = "(ref of unknown type)";
        }
        return result;
    }
}

Related

  1. matchMethods(String name, Method[] methods, Class[] spt, int mrs, boolean exact)
  2. nativeSQL(String sql, boolean noBackslashEscapes)
  3. newArrayOfType(int typeCode, int size)
  4. newInstance(Class clazz, Object... params)
  5. parse(String txt, int level)
  6. readAscii(Clob clob, String defaultValue)
  7. readMapListBySQL(String fileDir, String sql, List headers, List headerTypes)
  8. readObject(DataInput in, int sqlType)
  9. resolveEscapes(String escaped, boolean noBackslashEscapes)