Util { public static Class getJavaType(int sqlType) { switch (sqlType) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return String.class; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: return byte[].class; case Types.BIT: return Boolean.class; case Types.TINYINT: case Types.SMALLINT: return Short.class; case Types.INTEGER: return Integer.class; case Types.BIGINT: return Long.class; case Types.REAL: return Float.class; case Types.DOUBLE: case Types.FLOAT: return Double.class; case Types.DATE: return Date.class; case Types.TIME: return Time.class; case Types.TIMESTAMP: return Timestamp.class; default: throw new RuntimeException("We do not support tables with SqlType: " + getSqlTypeName(sqlType)); } }  static Class getJavaType(int sqlType);  }
@Test public void testBasic() { assertEquals(String.class, Util.getJavaType(Types.CHAR)); assertEquals(String.class, Util.getJavaType(Types.VARCHAR)); assertEquals(String.class, Util.getJavaType(Types.LONGVARCHAR)); assertEquals(byte[].class, Util.getJavaType(Types.BINARY)); assertEquals(byte[].class, Util.getJavaType(Types.VARBINARY)); assertEquals(byte[].class, Util.getJavaType(Types.LONGVARBINARY)); assertEquals(Boolean.class, Util.getJavaType(Types.BIT)); assertEquals(Short.class, Util.getJavaType(Types.TINYINT)); assertEquals(Short.class, Util.getJavaType(Types.SMALLINT)); assertEquals(Integer.class, Util.getJavaType(Types.INTEGER)); assertEquals(Long.class, Util.getJavaType(Types.BIGINT)); assertEquals(Float.class, Util.getJavaType(Types.REAL)); assertEquals(Double.class, Util.getJavaType(Types.DOUBLE)); assertEquals(Double.class, Util.getJavaType(Types.FLOAT)); assertEquals(Date.class, Util.getJavaType(Types.DATE)); assertEquals(Time.class, Util.getJavaType(Types.TIME)); assertEquals(Timestamp.class, Util.getJavaType(Types.TIMESTAMP)); }
@Test public void testError() { try { Util.getJavaType(Types.REF); fail("didn't throw like expected"); } catch (Exception e) { assertEquals("We do not support tables with SqlType: REF", e.getMessage()); } try { Util.getJavaType(-1000); fail("didn't throw like expected"); } catch (Exception e) { assertEquals("Unknown sqlType -1000", e.getMessage()); } }