Java SQL Type getLength(String format, String columnTypeName)

Here you can find the source of getLength(String format, String columnTypeName)

Description

get Length

License

Apache License

Declaration

public static int getLength(String format, String columnTypeName) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.SQLException;

public class Main {
    public static int getLength(String format, String columnTypeName) throws SQLException {
        //format = format.replace("%", "%1$");
        String col = columnTypeName.substring(0, 3);
        try {//from   w w  w . ja va2  s .c o  m
            if (col.equals("TIM") || col.equals("DAT")) {
                return String.format(format, new java.sql.Timestamp(0)).length();
            } else if (col.equals("NUM") || col.equals("DEC")) {
                return String.format(format, new java.math.BigDecimal(0)).length();
            } else if (col.equals("VAR") || col.equals("CHA") || col.equals("STR")) {
                return String.format(format, " ").length();
            } else if (col.equals("INT") || col.equals("SMA") || col.equals("BIN") || col.equals("POS")
                    || col.equals("SIG") || col.equals("NAT") || col.equals("PLS")) {
                return String.format(format, 0).length();
            } else if (col.equals("DOU") || col.equals("FLO")) {
                return String.format(format, 0.0).length();
            } else if (col.equals("REA")) {
                return String.format(format, 0.0f).length();
            } else if (col.equals("BOO") || col.equals("")) {
                return 4;
            } else {
                throw new SQLException(
                        "Can't format the SQL datatype: (" + col + ") " + columnTypeName + " format :" + format);
            }

            /*
             * Oracle data types currently not handled.... case "NCH": //NCHAR
             * case "NVA": //NVARCHAR return String.format(format, new
             * oracle.sql.NString )).length(); case "ROW": //ROWID, case "URO":
             * //UROWID return String.format(format, new
             * oracle.sql.ROWID()).length(); case "INT": //INTERVAL YEAR TO
             * MONTH, INTERVAL DAY TO SECOND -- conflict
             */
        } catch (Exception e) {
            System.err.println(
                    "Failed to format the SQL datatype: (" + col + ") " + columnTypeName + " format :" + format);
            throw new SQLException(e);
        }

    }
}

Related

  1. getJDBCTypeForNamedType(String aTypeName)
  2. getJdbcTypeName(int jdbcType)
  3. getJDBCTypeName(int value)
  4. getJdbcTypeNames()
  5. getJDBCTypes()
  6. getLong(Object object)
  7. getMBTileData(int column, int row, int zoomLevel, String jdbcConnectionString)
  8. getMBTileData(int column, int row, int zoomLevel, String jdbcConnectionString)
  9. getMessages(Array arr)