java db smallint / short

Posted on 2007-08-01
Last Modified: 2012-05-05
As far as I know, a smallint is supposed to be translated into a short by the JDBC drivers; however, it is coming out as an integer.

Is there a way to fix this?

Or any suggestions on how I could store a boolean in java db and know at time of result set that it is supposed to be a boolean?
Question by:modsiw
    LVL 6

    Accepted Solution

    This may be over-simplifying the solution, but could you just use a CHAR(1) or VARCHAR2(1) and assume that anything of that data type and length is a boolean? It may not scale appropriately - for instance if you have a list of one character status codes, then you might have problems, but you could code for that if necessary. But that might be a suitable answer for your needs.
    LVL 20

    Expert Comment

    >>As far as I know, a smallint is supposed to be translated into a short by the JDBC drivers; however, it is coming out as an integer.

    Is there a way to fix this?

    you should probably provide the sample code that shows this behavior, as well as which jdbc driver you use.
    LVL 86

    Expert Comment

    >>however, it is coming out as an integer.

    If that´s the case, why is it a problem?
    LVL 3

    Author Comment


    The problem is that I can't do an instanceof Short to determine if I should replace the value with a Boolean.
    LVL 3

    Author Comment

    Pretty standard db access:

        List<Map<String,Object>>  lstData    = new ArrayList<Map<String,Object>>();
        Statement    statement = null;
        ResultSet    resultSet = null;
          Map                       mapResult  = null;
          Object                    curValue   = null;
          String                    curKey     = null;
          statement  = connection.createStatement();
          resultSet = statement.getResultSet();
          if (resultSet != null) {
            ResultSetMetaData metaData = resultSet.getMetaData();
            int               count    = metaData.getColumnCount();
            while ( {
              mapResult = new HashMap();
              for (int j = 1; j <= count; j++) {
                curKey   = metaData.getColumnName(j).toLowerCase();
                curValue = resultSet.getObject(curKey);
          else throw new IllegalStateException(ERR_UNEXPECTED);
        catch (Exception e) {
          System.out.println("query: " + query);
          throw new IllegalStateException(ERR_UNEXPECTED);
        // release resources
        try { resultSet.close(); } catch (Exception e) { e.printStackTrace(); }
        try { statement.close(); } catch (Exception e) { e.printStackTrace(); }
    LVL 3

    Author Comment

    It later finds it's way to this method where the values are replaced with something more appropriate.

      public synchronized static Map<String,Object> normalizeValues(Map<String,Object> mapIn)
        mapIn = new HashMap(mapIn);
        Map       <String,Object>      mapLoadTmp   = new HashMap<String,Object>();
        Iterator  <String>             iterator     = mapIn.keySet().iterator();
        Iterator  <String>             iteratorLoad = null;
        List      <Map<String,Object>> lstLoad      = null;
        Map       <String,Object>      mapLoad      = null;
        String curKey     = null;
        Object curVal     = null;
        String curKeyLoad = null;
        Object curValLoad = null;
        while (iterator.hasNext()) {
          curKey =;
          curVal = mapIn.get(curKey);
          if (curKey.startsWith(".") && curVal instanceof List) {
            lstLoad = (List<Map<String,Object>>)curVal;
            for (int i = 0, ii = lstLoad.size(); i < ii; i++) {
              mapLoad    = lstLoad.get(i);
              mapLoadTmp = new HashMap<String,Object>();
              iteratorLoad = mapLoad.keySet().iterator();
              while (iteratorLoad.hasNext()) {
                curKeyLoad =;
                curValLoad = mapLoad.get(curKeyLoad);
                if (curValLoad instanceof BigDecimal) curValLoad = ((BigDecimal)curValLoad).doubleValue();
                if (curValLoad instanceof Short)      curValLoad = !curValLoad.equals(Short.valueOf((short)0));
        return mapIn;
    LVL 3

    Author Comment


    The 1 length varchar is a good idea. There are already some 1 length fields with hold things where there's 10 options or something. I believe I'll change such fields to ints, and take that route if a better suggestion doesn't pop up by tomorrow morning. This is a good deal of work. =(
    LVL 4

    Expert Comment

    The JDBC driver maps the smallint to different types as follows:

    When retrieve resultset using
    ResultSet.getObject and CallableStatement.getObject methods

    It maps smallint to Integer object (note: not primitive int)

    When retrieve data using other ways, it would map it to short (note: primitive short, not an object)

    int, short (lower case) is primitive type (java types)

    Integer, Short (upper case) is Java Object Types

    Please refer to this site for complete mapping details:

    :) Hope this help

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    powerN  challenge 3 36
    countX 22 50
    where is session ID cache stored 1 26
    select query - oracle 16 59
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now