Java SQL Type getSQLTypePrecision(int sqlType)

Here you can find the source of getSQLTypePrecision(int sqlType)

Description

get SQL Type Precision

License

Open Source License

Declaration

public static int getSQLTypePrecision(int sqlType) 

Method Source Code

//package com.java2s;

public class Main {
    public static int getSQLTypePrecision(int sqlType) {
        // returns the number of digits in a number
        // For datetime data types, the precision is the length in characters of the string representation (assuming
        // the maximum allowed precision of the fractional seconds component.
        // For binary data, the precision is the length in bytes.
        int resultType = 10; // default to integer precision, no real reason why
        switch (sqlType) {
        case java.sql.Types.BIT: {
            // corresponds to AS FieldType.BOOLEAN, 1 digit is used to represent true(1) or false(0)
            resultType = 1;/*w  ww. ja  v  a 2  s.c om*/
        }
        case java.sql.Types.BIGINT: {
            // corresponds to AS FieldType.LONG which takes 8 bytes of storage and can have a
            // max value of 9,223,372,036,854,775,807 for a precision of 19
            resultType = 19;
            break;
        }
        case java.sql.Types.CHAR: {
            // corresponds to AS FieldType.CHAR, 1 character is used to represent the value
            resultType = 1;
            break;
        }
        case java.sql.Types.INTEGER: {
            // corresponds to AS FieldType.INTEGER which uses 4 bytes for storage and can
            // have a max value of 2,147,483,647 for a precision of 10
            resultType = 10;
            break;
        }
        case java.sql.Types.SMALLINT: {
            // corresponds to AS FieldType.SHORT which uses 2 bytes for storage and can
            // have a max value of 32767
            resultType = 5;
            break;
        }
        case java.sql.Types.VARCHAR: {
            // corresponds to AS FieldType.STRING which stores data into a byte array which is
            // indexed in the Java layer by an int. So the precision is determined by the int index
            // which can have a maximum value of 2,147,483,647
            resultType = Integer.MAX_VALUE;
            break;
        }
        case java.sql.Types.REAL: {
            // for floating point numbers, the storage size of the value indicates the number
            // of bits that are used to store the mantissa of the float number in scientific
            // notation and, therefore, dictates the precision.
            // number of bytes is 4
            resultType = 7;
            break;
        }
        case java.sql.Types.DOUBLE: {
            // number of bytes is 8
            resultType = 15;
            break;
        }
        case java.sql.Types.DATE: {
            // 'yyyy-mm-dd'
            resultType = 10;
            break;
        }
        case java.sql.Types.TIME: {
            // 'hh:mm:ss'
            resultType = 8;
            break;
        }
        case java.sql.Types.TIMESTAMP: {
            // 'yyyy-mm-dd hh:mm:ss.nnnnnnnnn' java.sql.Timestamp.toString
            resultType = 29;
            break;
        }
        case java.sql.Types.BLOB: {
            // corresponds to AS FieldType.STRING which stores data into a byte array which is
            // indexed in the Java layer by an int. So the precision is determined by the int index
            // which can have a maximum value of 2,147,483,647
            resultType = Integer.MAX_VALUE;
            break;
        }
        default:
            resultType = 10;
        }
        return resultType;
    }
}

Related

  1. getSqlTypeName(int sqlType)
  2. getSQLTypeName(int sqlType)
  3. getSQLTypeName(int sqlType)
  4. getSqlTypeName(int type)
  5. getSQLTypeName(Integer sqltypecode)
  6. getSQLTypeRadix(int sqlType)
  7. getSqlValue(Object o)
  8. getStringFormat(int colType)
  9. getTableTypes(DatabaseMetaData dbmd)