1. /**
  2. * Copyright: Copyright (c) 2005-2005
  3. * Company: JavaResearch(http://www.javaresearch.org)
  4. */
  5. package org.javaresearch.jerch;
  6. import java.sql.Timestamp;
  7. import java.sql.Types;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * SQL和Java类型转换器类定义。
  13. * 最后更新日期:2005年3月29日
  14. * @author cherami
  15. */
  16. public class SQLJavaTypeConverter {
  17. /**
  18. * 转换映射。
  19. */
  20. private static Map convertMap=new HashMap();
  21. /**
  22. * 初始化。
  23. */
  24. static {
  25. setDefaultJavaType();
  26. }
  27. /**
  28. * 根据数据库的类型常量定义得到对应的Java类型。
  29. * @param sqlType 数据库的类型常量定义
  30. * @return 对应的Java类型
  31. */
  32. public static Class getJavaType(int sqlType) {
  33. Class result=(Class)convertMap.get(new Integer(sqlType));
  34. if (result!=null) {
  35. return result;
  36. } else {
  37. return Object.class;
  38. }
  39. }
  40. /**
  41. * 设置缺省的映射关系。
  42. */
  43. public static void setDefaultJavaType() {
  44. convertMap.put(new Integer(Types.INTEGER),Integer.class);
  45. convertMap.put(new Integer(Types.VARCHAR),String.class);
  46. convertMap.put(new Integer(Types.BIGINT),Long.class);
  47. convertMap.put(new Integer(Types.BOOLEAN),Boolean.class);
  48. convertMap.put(new Integer(Types.DATE),Date.class);
  49. convertMap.put(new Integer(Types.TIMESTAMP),Timestamp.class);
  50. convertMap.put(new Integer(Types.TIME),Timestamp.class);
  51. }
  52. /**
  53. * 设置数据库的类型常量对应的Java类型。
  54. * @param sqlType 数据库的类型常量
  55. * @param javaType Java类型
  56. */
  57. public static void setJavaType(int sqlType,Class javaType) {
  58. convertMap.put(new Integer(sqlType),javaType);
  59. }
  60. }