Java SQL ResultSet Read getResultSetMetaData(ResultSet rs)

Here you can find the source of getResultSetMetaData(ResultSet rs)

Description

get Result Set Meta Data

License

Apache License

Declaration

public static HashMap<String, Class> getResultSetMetaData(ResultSet rs)
            throws Exception 

Method Source Code

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

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.util.Date;
import java.util.HashMap;

public class Main {
    public static HashMap<String, Class> getResultSetMetaData(ResultSet rs)
            throws Exception {
        HashMap lhs = new HashMap(5);
        ResultSetMetaData rsmd = rs.getMetaData();
        int colSize = rsmd.getColumnCount();

        for (int i = 1; i < colSize + 1; i++) {
            lhs.put(rsmd.getColumnName(i).toUpperCase(),
                    getJavaType(rsmd.getColumnName(i),
                            rsmd.getColumnType(i), rsmd.getPrecision(i),
                            rsmd.getScale(i)));
        }/*w w  w.j  a  v  a  2  s.c om*/

        return lhs;
    }

    private static Class getJavaType(String colName, int colType,
            int colPrecision, int colScale) throws Exception {
        Class cls = null;
        String jType = null;

        switch (colType) {
        case 2003:
            jType = "ARRAY";
            break;
        case -5:
            jType = "BIGINT";
            cls = Long.class;
            break;
        case -2:
            jType = "BINARY";
            break;
        case -7:
            jType = "BIT";

            cls = Integer.class;
            break;
        case 2004:
            jType = "BLOB";
            break;
        case 16:
            jType = "BOOLEAN";
            break;
        case 1:
            jType = "CHAR";
            cls = String.class;
            break;
        case 2005:
            jType = "CLOB";
            break;
        case 70:
            jType = "DATALINK";
            break;
        case 91:
            jType = "DATE";
            cls = Date.class;
            break;
        case 3:
            jType = "NUMERIC";

            if (colScale == -127) {
                cls = Double.class;
            } else if ((colPrecision == 0) && (colScale <= 0)) {
                cls = Double.class;
            } else if (colScale == 0) {
                if (colPrecision > 9)
                    cls = Long.class;
                else
                    cls = Integer.class;
            } else {
                if (colScale <= 0)
                    break;
                if (colPrecision > 9)
                    cls = Double.class;
                else {
                    cls = Float.class;
                }
            }
            break;
        case 2001:
            jType = "DISTINCT";
            break;
        case 8:
            jType = "DOUBLE";
            cls = Double.class;
            break;
        case 6:
            jType = "FLOAT";
            cls = Float.class;
            break;
        case 4:
            jType = "INTEGER";
            cls = Integer.class;
            break;
        case 2000:
            jType = "JAVA_OBJECT";
            break;
        case -4:
            jType = "LONGVARBINARY";
            break;
        case -1:
            jType = "LONGVARCHAR";
            break;
        case 0:
            jType = "NULL";
            break;
        case 2:
            jType = "NUMERIC";

            if (colScale == -127) {
                cls = Double.class;
            } else if ((colPrecision == 0) && (colScale <= 0)) {
                cls = Double.class;
            } else if (colScale == 0) {
                if (colPrecision > 9)
                    cls = Long.class;
                else
                    cls = Integer.class;
            } else {
                if (colScale <= 0)
                    break;
                if (colPrecision > 9)
                    cls = Double.class;
                else {
                    cls = Float.class;
                }
            }
            break;
        case 1111:
            jType = "OTHER";
            break;
        case 7:
            jType = "REAL";
            break;
        case 2006:
            jType = "REF";
            break;
        case 5:
            jType = "SMALLINT";
            cls = Integer.class;
            break;
        case 2002:
            jType = "STRUCT";
            break;
        case 92:
            jType = "TIME";
            break;
        case 93:
            jType = "TIMESTAMP";
            cls = Date.class;
            break;
        case -6:
            jType = "TINYINT";
            cls = Integer.class;
            break;
        case -3:
            jType = "VARBINARY";
            break;
        case 12:
            jType = "VARCHAR";
            cls = String.class;
            break;
        default:
            jType = "JDBC_NOT_SUPPORT";
        }
        if (cls != null) {
            return cls;
        }
        throw new Exception("Not supported column type! colName=" + colName
                + " jdbcType=" + jType + " type=" + colType + " precision="
                + colPrecision + " Scale=" + colScale);
    }
}

Related

  1. getResultSetColumns(ResultSet rs)
  2. getResultSetDateValue(ResultSet rs, String sColumnName)
  3. GetResultSetFromQuery(String command)
  4. getResultSetFromQueryAgainstDefaultConnection( String sql)
  5. getResultSetInstance(Connection conn, String sql)
  6. getResultSetRowString(ResultSet rs)
  7. getResultSetStrings(ResultSet rs)
  8. getResultSetValue(ResultSet rs, int index)
  9. getResultSetValue(ResultSet rs, int index)