• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1017
  • Last Modified:

java db smallint / short

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?
0
modsiw
Asked:
modsiw
1 Solution
 
SirCroftyCommented:
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.
0
 
gatorvipCommented:
>>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.
0
 
CEHJCommented:
>>however, it is coming out as an integer.

If that´s the case, why is it a problem?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
modsiwAuthor Commented:
@CEHJ

The problem is that I can't do an instanceof Short to determine if I should replace the value with a Boolean.
0
 
modsiwAuthor Commented:
Pretty standard db access:


    List<Map<String,Object>>  lstData    = new ArrayList<Map<String,Object>>();
    Statement    statement = null;
    ResultSet    resultSet = null;
    try
    {
      Map                       mapResult  = null;
      Object                    curValue   = null;
      String                    curKey     = null;
      statement  = connection.createStatement();
      statement.executeQuery(query);
      resultSet = statement.getResultSet();
      if (resultSet != null) {
        ResultSetMetaData metaData = resultSet.getMetaData();
        int               count    = metaData.getColumnCount();
        while (resultSet.next()) {
          mapResult = new HashMap();
          for (int j = 1; j <= count; j++) {
            curKey   = metaData.getColumnName(j).toLowerCase();
            curValue = resultSet.getObject(curKey);
            mapResult.put(curKey,curValue);
          }
          lstData.add(mapResult);
        }
      }
      else throw new IllegalStateException(ERR_UNEXPECTED);
    }
    catch (Exception e) {
      System.out.println("query: " + query);
      e.printStackTrace();
      throw new IllegalStateException(ERR_UNEXPECTED);
    }
    // release resources
    try { resultSet.close(); } catch (Exception e) { e.printStackTrace(); }
    try { statement.close(); } catch (Exception e) { e.printStackTrace(); }
0
 
modsiwAuthor Commented:
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 = iterator.next();
      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 = iteratorLoad.next();
            curValLoad = mapLoad.get(curKeyLoad);
            if (curValLoad instanceof BigDecimal) curValLoad = ((BigDecimal)curValLoad).doubleValue();
            //curValLoad.
            if (curValLoad instanceof Short)      curValLoad = !curValLoad.equals(Short.valueOf((short)0));
            mapLoadTmp.put(curKeyLoad,curValLoad);
          }
          lstLoad.set(i,mapLoadTmp);
        }
      }
    }
    return mapIn;
  }
0
 
modsiwAuthor Commented:
@SirCrofty

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. =(
0
 
doraemon-nolifeCommented:
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)

Note:
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:
http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html

:) Hope this help
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now