1 Reply Latest reply: Dec 6, 2007 7:57 AM by 807603 RSS

    Name of an int ( e.g. java.sql.Types )

    807603
      I have the sql.Types code as an int number and want to display the name. Is there a straightforward method or do I have to search it in a loop like here ?
      String getName(int i) {
      for (Field f: java.sql.Types.class.getFields()) {
           if (f.getType().equals(int.class)) {
                if (f.getInt(f.getType()) == i) {
                System.out.println( i + " = "+f.getName() );
                // e.g.               8 = DOUBLE
                return f.getName();
                }
           }
      }
      }
      This question is not specific to java.sql.Types I guess (that's why I post here).
      A dedicated hint for sql is appreciated as well, however ;)
        • 1. Re: Name of an int ( e.g. java.sql.Types )
          807603
          Seems the loop is required and it works (in no time, there aren't too many Fields)
               if (f.getInt(f.getType()) == i) {
          Having a closer look, I do not understand the inner f.getType() , however.
          f.getInt() requires an object, but anything, including null, works ?!

          Any hint, or link where to learn about java.lang.reflect.Field, please ?